Skip to content

Commit

Permalink
bin/idhash
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakilon committed Apr 16, 2024
1 parent 09141f7 commit b3a7aad
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
/.byebug_history
.DS_Store
*.jpg
*.png
/test_images/*.png

/temp.*
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gem "dhash", github: "nakilon/dhash"
gem "phamilie"
gem "mini_magick"
gem "dhashy"
gem "phashion"

gem "get_process_mem"
gem "mll"
Expand Down
8 changes: 8 additions & 0 deletions README.md
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ and visualization of IDHash (`rake compare_images -- image1.jpg image2.jpg`):

Here in each of 64 cells, there are two circles that color the difference between that cell and the neighbor one. If the difference is low the Importance bit is set to zero and the circle is invisible. So there are 128 pairs of corresponding circles and when you take one, if at least one circle is visible and is of different color the line is to be drawn. Here you see 15 lines and so the distance between fingerprints will be equal to 15 (that is pretty low and can be interpreted as "images look similar"). Also, you see here that floor on this photo matters -- classic dHash won't see that it's darker than wall because it's comparing only horizontal neighbors and if one photo had no floor the distance function won't notice that. Also, it sees the Important difference between the very right and left columns because the wall has a slow but visible gradient.

As of version 0.2.4.0 the gem includes a binary that you can call to get similar visualisation in terminal:

```bash
idhash test_images/3f9....jpg test_images/309....jpg
```

![screenshot](doc_bin.png)

### Remaining problems

* Neither dHash nor IDHash can't automatically detect very shifted crops and rotated images but you can make a wrapper that would call the comparison function iteratively.
Expand Down
Empty file modified Rakefile
100755 → 100644
Empty file.
57 changes: 57 additions & 0 deletions bin/idhash
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env ruby
Signal.trap(:INT){ abort "\n(interrupted by SIGINT)" }

unless 2 == ARGV.size
puts "this command is to compare two images"
puts "usage: #{__FILE__} <path to image1> <path to image2>"
exit
end

require_relative "../lib/dhash-vips"
ha, hb = ARGV.map{ |filename| DHashVips::IDHash.fingerprint filename }
puts "distance: #{d1 = DHashVips::IDHash.distance ha, hb}"
size = 2 ** 3
shift = 2 * size * size
ai = ha >> shift
ad = ha - (ai << shift)
bi = hb >> shift
bd = hb - (bi << shift)

_127 = shift - 1
_63 = size * size - 1
# width = 800
# height = 800

d2 = 0
a, b = [[ad, ai, ARGV[0]], [bd, bi, ARGV[1]]].map do |xd, xi, path|
puts File.basename path
hor = Array.new(size){Array.new(size){" "}}
ver = Array.new(size){Array.new(size){" "}}
_127.downto(0).each_with_index do |i, ii|
if i > _63
y, x = (_127 - i).divmod size
else
x, y = (_63 - i).divmod size
end
if xi[i] > 0
target, c = if i > _63
[ver, %w{ v ^ }[xd[i]]]
else
[hor, %w{ > < }[xd[i]]]
end
target[y][x] = c
end
if ai[i] + bi[i] > 0 && ad[i] != bd[i]
d2 += 1
target = if i > _63
ver
else
hor
end
target[y][x] = "\e[7m#{target[y][x]}\e[27m"
end
end
hor.zip(ver).each{ |_| puts _.join " " }
end
abort "something went wrong" unless d1 * 2 == d2
puts "OK"
9 changes: 6 additions & 3 deletions dhash-vips.gemspec
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
Gem::Specification.new do |spec|
spec.name = "dhash-vips"
spec.version = "0.2.3.0"
spec.version = "0.2.4.0"
spec.summary = "dHash and IDHash perceptual image hashing/fingerprinting"
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/dhash-vips"}

spec.author = "Victor Maslov aka Nakilon"
spec.email = "[email protected]"
spec.license = "MIT"
spec.metadata = {"source_code_uri" => "https://github.com/nakilon/dhash-vips"}

spec.add_dependency "ruby-vips", "~> 2.0", "!= 2.1.0", "!= 2.1.1"

spec.require_path = "lib"
spec.extensions = %w{ extconf.rb }
spec.files = %w{ LICENSE.txt dhash-vips.gemspec lib/dhash-vips.rb idhash.c lib/dhash-vips-post-install-test.rb } + spec.extensions
spec.files = %w{ LICENSE.txt dhash-vips.gemspec lib/dhash-vips.rb idhash.c lib/dhash-vips-post-install-test.rb } + spec.extensions +
%w{ bin/idhash }
spec.executables = %w{ idhash }
spec.bindir = "bin"
end
Binary file added doc_bin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b3a7aad

Please sign in to comment.