akamhy / videohash

Near Duplicate Video Detection (Perceptual Video Hashing) - Get a 64-bit comparable hash-value for any video.
https://pypi.org/project/videohash
MIT License
264 stars 41 forks source link

Hashing speed issue. #96

Open Demmenie opened 1 year ago

Demmenie commented 1 year ago

Describe the bug It takes quite a while to hash a video.

To Reproduce

from videohash import VideoHash
import time

start = time.time()

url = 'https://user-images.githubusercontent.com/47534140/185008752-da1f09c7-a177-4a46-9c64-230744e998c1.mp4'
v1 = VideoHash(url=url, frame_interval=12)

print(f"Finished in {time.time() - start} secs")

Expected behavior It should realistically be doable in under a second

Please complete the following information:

Additional context Currently takes about 3/4 seconds

96jaco96 commented 9 months ago

any updates on this? it's still so slow it's not even funny.

and i don't think that the problem is "unoptimized" code, but i think it's the logic behind it, on how it was conceived in first place...

i mean look at the definition of how it calculates the hashes:

"Every one second, a frame from the input video is extracted, the frames are shrunk to a 144x144 pixel square, a collage is constructed that contains all of the resized frames(square-shaped), the collage's wavelet hash's bit-list is the first bit-list that we use. The frames extracted are now stitched horizontally to each other, and finally divided into 64 equal sized images, the domiant color of these 64 images are detected and compared with a pre-defined pattern of dominant colors, if they match the bit is set else unset. So now we have two bitlist, finally we bitwise XOR these two bitlists. The XOR'ed output is used to generate the final 64 bit hash-value for the video. The bits are joined to form the 64 bit hash-value of the input value."

This process is bound to take a long time, and if this is the way that it calculates hashes then i don't think there's too much we can do to speed it up....

but maybe someone that's smarter than me can answer this better?

Pigglebear commented 4 months ago

I played around with logging when the function calls happen, and the vast majority of the time is taken up in detecting the cropping/black bars.

Edit: I was able to speed it up ~10x for short videos by passing the output for video_duration() to framesextractor() and stopping it from looking for cropping in timestamps past the end of the video.

albertopasqualetto commented 2 months ago

Edit: I was able to speed it up ~10x for short videos by passing the output for video_duration() to framesextractor() and stopping it from looking for cropping in timestamps past the end of the video.

I think you could do PR for it.. even if the author will never merge it.

Demmenie commented 1 week ago

I think you could do PR for it.. even if the author will never merge it.

Please do, if only to let other people do the same. I think the author does update it but only infrequently.

Demmenie commented 3 days ago

Edit: I was able to speed it up ~10x for short videos by passing the output for video_duration() to framesextractor() and stopping it from looking for cropping in timestamps past the end of the video.

I implemented this in my fork and it was about twice as fast on average, counting both long and short videos: https://github.com/Demmenie/videohash2 https://pypi.org/project/videohash2/