Created
October 4, 2021 16:08
-
-
Save dvisockas/73332f3df5b35ab9277d4e646123f475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
from PIL import Image | |
import imagehash | |
import numpy as np | |
import os | |
from tqdm import tqdm | |
def bin2hex(arr): | |
h = 0 | |
s = [] | |
for i, v in enumerate(arr.flatten()): | |
if v: | |
h += 2**(i % 8) | |
if (i % 8) == 7: | |
s.append(hex(h)[2:].rjust(2, '0')) | |
h = 0 | |
return "".join(s) | |
def hex2bin(hexstr): | |
l = [] | |
if len(hexstr) != 16: | |
raise ValueError('The hex string has the wrong length') | |
for i in range(8): | |
h = hexstr[i*2:i*2+2] | |
v = int("0x" + h, 16) | |
l.append([v & 2**i > 0 for i in range(8)]) | |
return ImageHash(numpy.array(l)) | |
filenames = [f for f in os.listdir('./') if '.mp4' in f] | |
for filename in filenames: | |
count = 0 | |
frame_hashes = [] | |
cap = cv2.VideoCapture(filename) | |
while(cap.isOpened()): | |
ret, frame = cap.read() | |
if ret: | |
count += 1 | |
if count % 420 == 0: print(count) | |
image = Image.fromarray(frame.astype('uint8'), 'RGB') | |
frame_hashes.append(str(imagehash.average_hash(image, 32))) | |
else: | |
np.save(f'{filename}_template', frame_hashes) | |
break | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment