From 004f063a8e9e16c0d40931da43445e5e14736063 Mon Sep 17 00:00:00 2001 From: Akash Mahanty Date: Tue, 19 Jan 2021 02:48:49 +0530 Subject: [PATCH] give users freedom to change hashing algorithm (#19) * 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 --- README.md | 32 +++++++++++++++++++++++++++----- setup.py | 2 +- tests/test_vhash.py | 15 +++++++++++++-- videohash/__version__.py | 2 +- videohash/vhash.py | 25 +++++++++++++++++++++---- 5 files changed, 63 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 68f2fdb..234f170 100644 --- a/README.md +++ b/README.md @@ -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.
-
-
+ +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") +``` +
Algorithms supported + +

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

+ +
+ +videohash uses 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) diff --git a/setup.py b/setup.py index e86a76f..e933f07 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/test_vhash.py b/tests/test_vhash.py index 642c5df..52b04ee 100644 --- a/tests/test_vhash.py +++ b/tests/test_vhash.py @@ -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" @@ -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" diff --git a/videohash/__version__.py b/videohash/__version__.py index f86a7b1..4aabcd8 100644 --- a/videohash/__version__.py +++ b/videohash/__version__.py @@ -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" diff --git a/videohash/vhash.py b/videohash/vhash.py index 017510a..05b8a0c 100644 --- a/videohash/vhash.py +++ b/videohash/vhash.py @@ -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() @@ -72,7 +89,7 @@ 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) @@ -80,10 +97,10 @@ def from_url(input_url): 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() @@ -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