Skip to content

Commit

Permalink
give users freedom to change hashing algorithm (#19)
Browse files Browse the repository at this point in the history
* added option to choose anyhash user want from imagehash lib

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update setup.py

* Update __version__.py
  • Loading branch information
akamhy authored Jan 18, 2021
1 parent b04e7c2 commit 004f063
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,39 @@ pip install git+https://github.com/akamhy/videohash.git

- The difference of hash1 and hash2 is not 0 as the file in this repository is slightly modified and downscaled.

- A collage of frames is generated and imagehash of this collage is videohash for the full video.


- A collage of frames is generated and imagehash(Average hashing) of this collage is videohash for the full video.

<div align="center">

<img src="https://raw.githubusercontent.com/akamhy/videohash/main/assets/collage.jpeg"><br>

</div>



You can change the algorithm used to generate the hash of the collage via the `image_hash` argument. The default algorithm is `average_hash`.

```python
>>> hash = videohash.from_url("https://www.youtube.com/watch?v=PapBjpzRhnA", image_hash="crop_resistant_hash")
>>> hash = videohash.from_path("/home/akamhy/Downloads/rocket.webm", image_hash="phash")
```
<details><summary>Algorithms supported</summary>

<p>

- `average_hash`
- `phash`
- `dhash`
- `whash`
- `colorhash`
- `crop_resistant_hash`

</p>

</details>

videohash uses <https://github.com/JohannesBuchner/imagehash> to use these hashing algorithms.



## License
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/akamhy/videohash/blob/master/LICENSE)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
author=about["__author__"],
author_email=about["__author_email__"],
url=about["__url__"],
download_url="https://github.com/akamhy/videohash/archive/1.0.1.tar.gz",
download_url="https://github.com/akamhy/videohash/archive/1.0.2.tar.gz",
keywords=[
"videohash",
"video hashing",
Expand Down
15 changes: 13 additions & 2 deletions tests/test_vhash.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import pytest
import os
from videohash.vhash import from_url, from_path
from videohash.vhash import from_url, from_path, hash_manager


def test_all():
this_dir = os.path.dirname(os.path.realpath(__file__))

hash_url = from_url(
"https://raw.githubusercontent.com/akamhy/videohash/main/assets/rocket.webm"
)
assert str(hash_url) == "7c7e7ff9ffff0000"
this_dir = os.path.dirname(os.path.realpath(__file__))

local_video = this_dir + "/../assets/rocket.webm"
hash_path = from_path(local_video)
assert str(hash_path) == "7c7e7ff9ffff0000"
Expand All @@ -17,3 +19,12 @@ def test_all():
assert str(different_file_hash) == "3cffff0000000eff"
diff = different_file_hash - hash_url
assert diff == 37

collage = this_dir + "/../assets/collage.jpeg"

assert str(hash_manager(collage, image_hash="phash")) == "c08257d5df6fb202"
assert str(hash_manager(collage, image_hash="dhash")) == "c8c0cf23339c0070"
assert str(hash_manager(collage, image_hash="whash")) == "2c787ffbffe00000"
assert str(hash_manager(collage, image_hash="colorhash")) == "1ec00000000"
assert str(hash_manager(collage, image_hash="crop_resistant_hash")) == "dc98381819313818,b8f8f4f6ff7cfcfe,c8cbc3e5c5cce8f3,ae9deec68eceeeee,fbf4c48881c48990,dbf5c48881c68890,a929292d15b5d555,c8c0cf23339c0070"
assert str(hash_manager(collage)) == "fc7e7ff9ffff0000"
2 changes: 1 addition & 1 deletion videohash/__version__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__title__ = "videohash"
__description__ = "Video Hashing Library"
__url__ = "https://akamhy.github.io/videohash/"
__version__ = "1.0.1"
__version__ = "1.0.2"
__author__ = "akamhy"
__author_email__ = "akamhy@yahoo.com"
__license__ = "MIT"
Expand Down
25 changes: 21 additions & 4 deletions videohash/vhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def collage_maker(image_dir, task_dir, collage_image_width, images_per_row_in_co
j += 1
collage_image.save(join(task_dir, "collage.jpeg"))

def hash_manager(collage, image_hash=None):
img = Image.open(collage)

if image_hash == "phash":
hash = imagehash.phash(img)
elif image_hash == "dhash":
hash = imagehash.dhash(img)
elif image_hash == "whash":
hash = imagehash.whash(img)
elif image_hash == "colorhash":
hash = imagehash.colorhash(img)
elif image_hash == "crop_resistant_hash":
hash = imagehash.crop_resistant_hash(img)
else:
hash = imagehash.average_hash(img)

return hash

def task_uid_dir():
sys_random = random.SystemRandom()
Expand All @@ -72,18 +89,18 @@ def task_uid_dir():
return (task_uid, task_dir)


def from_url(input_url):
def from_url(input_url, image_hash=None):
task_uid, task_dir = task_uid_dir()
output_file = join(task_dir, task_uid + ".%(ext)s")
download(input_url, output_file)
l = [filename for filename in os.listdir(task_dir) if filename.startswith(task_uid)]
if len(l) == 0:
raise FileNotFoundError("Could Not Find Frames! Failed to generate frames.")
input_file = join(task_dir, l[0])
return from_path(input_file, task_uid=task_uid, task_dir=task_dir)
return from_path(input_file, task_uid=task_uid, task_dir=task_dir, image_hash=image_hash)


def from_path(input_file, task_uid=None, task_dir=None):
def from_path(input_file, task_uid=None, task_dir=None, image_hash=None):

if not task_uid or not task_dir:
task_uid, task_dir = task_uid_dir()
Expand All @@ -93,6 +110,6 @@ def from_path(input_file, task_uid=None, task_dir=None):
frames(input_file, image_prefix)
collage_maker(image_dir, task_dir, 800, 8)
collage = join(task_dir, "collage.jpeg")
hash = imagehash.average_hash(Image.open(collage))
hash = hash_manager(collage, image_hash=image_hash)
shutil.rmtree(dir)
return hash

0 comments on commit 004f063

Please sign in to comment.