average_hash 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZmNkN2ViMWQzMjgyMmE2MTVlNGU1MzMyODU5YWI4M2VkMWZmZDg0NA==
5
+ data.tar.gz: !binary |-
6
+ YjdlODY4NDdmMjMzN2EwN2ZiY2QxOGZjY2E1MjE0MDAwZjRiNTkyMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MTg4NzQyMjJiZDEyNmNjZDJjNjk5N2ZkYzhiMzg3MmFiYWQ1ZmY3NzBhNWIx
10
+ MjhjNzJjMTVhY2VjZDJiZTBmYTY3ZjQ4NmJiMzhhMGY1Mzc0NTdhNjU3Mjhh
11
+ MzBjMTgwYTczYjUyMzU5OTk4MGJlNWNjYTBiZmQwOTIzN2NhMDY=
12
+ data.tar.gz: !binary |-
13
+ ZmI4MTFiODBkZTcxMzJjMDk5ODEyNmZhNzZiODY5NmEyNDQwYjM5Y2UwYzI2
14
+ ZjQxMDQ3OTk3ZTRiMGMwMDEzNGVlMjBmODNjOTMyMmJjNWI4OGY5NmUwYTQw
15
+ OTQ4ZTA1OTk2OTA2ODRhZTE3OTVkNDc0NTRkYWEyODg5NDg5ZDY=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+
7
+ notifications:
8
+ emails:
9
+ - toru.iwashita@gmail.com
10
+ on_failure: always
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in average_hash.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ToruIwashita
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # AverageHash
2
+
3
+ Simple similarity calculation method of image. It is inspired by [Phashion](https://github.com/westonplatter/phashion).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'average_hash'
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install average_hash
18
+
19
+ ## Usage
20
+
21
+ Determine the similarity of image
22
+
23
+ require 'average_hash'
24
+ image_1 = AverageHash::Image.new(file_path_1)
25
+ image_2 = AverageHash::Image.new(file_path_2)
26
+ image_1.duplicate?(image_2)
27
+ => true
28
+
29
+ If you want to set the threshold of hamming distance, you can do the following. (dafault threshold is 5)
30
+
31
+ image_1.duplicate?(image_2, threshold: 3)
32
+ => false
33
+
34
+ Get the hamming distance between two images
35
+
36
+ image_1.distance_from(image_2)
37
+ => 4
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'average_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "average_hash"
8
+ spec.version = AverageHash::VERSION
9
+ spec.authors = ["ToruIwashita"]
10
+ spec.email = ["toru.iwashita@gmail.com"]
11
+ spec.description = %q{Simple similarity calculation method of image}
12
+ spec.summary = %q{Simple similarity calculation method of image}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "rmagick"
25
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+
3
+ module AverageHash
4
+ end
5
+
6
+ require 'average_hash/version'
7
+ require 'average_hash/image'
8
+ require 'average_hash/hashing_strategy'
@@ -0,0 +1,54 @@
1
+ # coding: utf-8
2
+
3
+ require 'RMagick'
4
+
5
+ module AverageHash
6
+ class HashingStrategy
7
+ RESIZNING_SIZE = 8
8
+ PIXEL_ACQUISITION_SIZE = 1
9
+
10
+ INDEX_OF_R = 0
11
+ INDEX_OF_G = 1
12
+ INDEX_OF_B = 2
13
+
14
+ NTSC_COEFFICIENT_OF_R = 0.30
15
+ NTSC_COEFFICIENT_OF_G = 0.59
16
+ NTSC_COEFFICIENT_OF_B = 0.11
17
+
18
+ def initialize(file_path)
19
+ @image = Magick::Image.read(file_path).first.resize(RESIZNING_SIZE, RESIZNING_SIZE)
20
+ end
21
+
22
+ def generate_hash
23
+ (image_pixel_count.times).map { |i| gray_scaled_pixel(i) > average_pixel ? '1' : '0' }.join
24
+ end
25
+
26
+ private
27
+
28
+ def image_columns
29
+ @image.columns
30
+ end
31
+
32
+ def image_rows
33
+ @image.rows
34
+ end
35
+
36
+ def image_pixel_count
37
+ image_columns * image_rows
38
+ end
39
+
40
+ def average_pixel
41
+ gray_scaled_pixels = (image_pixel_count.times).map { |i| gray_scaled_pixel(i) }
42
+ gray_scaled_pixels.reduce(0, :+) / image_pixel_count
43
+ end
44
+
45
+ def gray_scaled_pixel(num)
46
+ px = pixel_at((num % RESIZNING_SIZE), (num / RESIZNING_SIZE))
47
+ px[INDEX_OF_R] * NTSC_COEFFICIENT_OF_R + px[INDEX_OF_G] * NTSC_COEFFICIENT_OF_G + px[INDEX_OF_B] * NTSC_COEFFICIENT_OF_B
48
+ end
49
+
50
+ def pixel_at(offset_of_x, offset_of_y)
51
+ @image.export_pixels(offset_of_x, offset_of_y, PIXEL_ACQUISITION_SIZE, PIXEL_ACQUISITION_SIZE)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+
3
+ module AverageHash
4
+ class Image
5
+ DEFAULT_DUPE_THRESHOLD = 5
6
+
7
+ attr_reader :file_path
8
+
9
+ def initialize(file_path)
10
+ @file_path = file_path
11
+ end
12
+
13
+ def fingerprint
14
+ @average_hash ||= HashingStrategy.new(@file_path).generate_hash
15
+ @average_hash.to_i(2)
16
+ end
17
+
18
+ def distance_from(other)
19
+ (fingerprint ^ other.fingerprint).to_s(2).count('1')
20
+ end
21
+
22
+ def duplicate?(other, opts = {})
23
+ threshold = opts[:threshold] || DEFAULT_DUPE_THRESHOLD
24
+ distance_from(other) <= threshold
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module AverageHash
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe AverageHash::Image do
4
+ context 'fingerprint use sample_1.jpg' do
5
+ it do
6
+ image_1 = AverageHash::Image.new(fixture_file_path('sample_1.jpg'))
7
+ expect(image_1.fingerprint).to eq 13158600607563222924
8
+ end
9
+ end
10
+
11
+ context 'fingerprint use sample_2.jpg' do
12
+ it do
13
+ image_2 = AverageHash::Image.new(fixture_file_path('sample_2.jpg'))
14
+ expect(image_2.fingerprint).to eq 7870530257777914161
15
+ end
16
+ end
17
+
18
+ context 'use two jpg images' do
19
+ before do
20
+ @image_1 = AverageHash::Image.new(fixture_file_path('sample_1.jpg'))
21
+ @image_2 = AverageHash::Image.new(fixture_file_path('sample_2.jpg'))
22
+ end
23
+
24
+ context '"sample_1" distance_from "sample_2"' do
25
+ it { expect(@image_1.distance_from(@image_2)).to eq 36 }
26
+ end
27
+
28
+ context '"sample_1" duplicate? "sample_2"' do
29
+ it { expect(@image_1.duplicate?(@image_2)).to be_falsey }
30
+ end
31
+
32
+ context '"sample_1" duplicate? "sample_2" with threshold 35' do
33
+ it { expect(@image_1.duplicate?(@image_2, threshold: 35)).to be_falsey }
34
+ end
35
+
36
+ context '"sample_1" duplicate? "sample_2" with threshold 36' do
37
+ it { expect(@image_1.duplicate?(@image_2, threshold: 36)).to be_truthy }
38
+ end
39
+ end
40
+
41
+ context 'use duplicate jpg image, "sample_1" duplicate? "sample_1"' do
42
+ it do
43
+ image_1_1 = AverageHash::Image.new(fixture_file_path('sample_1.jpg'))
44
+ image_1_2 = AverageHash::Image.new(fixture_file_path('sample_1.jpg'))
45
+ expect(image_1_1.duplicate?(image_1_2)).to be_truthy
46
+ end
47
+ end
48
+ end
49
+
50
+ describe AverageHash::HashingStrategy do
51
+ context 'generate_hash with argument "smaple_1.jpg"' do
52
+ it do
53
+ hashing_strategy = AverageHash::HashingStrategy.new(fixture_file_path('sample_1.jpg'))
54
+ expect(hashing_strategy.generate_hash).to eq '1011011010011100101111011000100100001011000010111000111110001100'
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'average_hash'
4
+
5
+ def fixture_file_path(file)
6
+ File.join(File.dirname(__FILE__), 'fixtures', file)
7
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: average_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ToruIwashita
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rmagick
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple similarity calculation method of image
70
+ email:
71
+ - toru.iwashita@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - average_hash.gemspec
84
+ - lib/average_hash.rb
85
+ - lib/average_hash/hashing_strategy.rb
86
+ - lib/average_hash/image.rb
87
+ - lib/average_hash/version.rb
88
+ - spec/average_hash_spec.rb
89
+ - spec/fixtures/sample_1.jpg
90
+ - spec/fixtures/sample_2.jpg
91
+ - spec/spec_helper.rb
92
+ homepage: ''
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.0
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Simple similarity calculation method of image
116
+ test_files:
117
+ - spec/average_hash_spec.rb
118
+ - spec/fixtures/sample_1.jpg
119
+ - spec/fixtures/sample_2.jpg
120
+ - spec/spec_helper.rb