Skip to content

Instantly share code, notes, and snippets.

@dvisockas
Created October 4, 2021 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvisockas/42c2c1c13e8c0a3f2ee554e6a77ef2cd to your computer and use it in GitHub Desktop.
Save dvisockas/42c2c1c13e8c0a3f2ee554e6a77ef2cd to your computer and use it in GitHub Desktop.
video.py
import numpy as np
import cv2
from PIL import Image
import imagehash
from imagehash import *
import numpy as np
import os
import matplotlib.pyplot as plt
from tqdm import tqdm
def hex2bin(hexstr):
l = []
# if len(hexstr) != 16:
# raise ValueError('The hex string has the wrong length')
for i in range(32):
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))
sources = np.load('tall.mp4_template.npy')
targets = np.load('short.mp4_template.npy')
scores = []
lds = []
frame_hop = 1
give_grace = 4
last_source = -1
for i, source in enumerate(sources[::frame_hop]):
source_index = frame_hop * i
if source_index < last_source:
continue
src = hex2bin(source)
last_target = -1
for target_index, target in enumerate(targets):
# if target_index < 2350:
# continue
if target_index <= last_target:
continue
new_target_index = target_index
new_source_index = source_index
dist = src - hex2bin(target)
grace = 0
# print(f'Dist: {dist}, grace: {grace}, s: {new_source_index}, t: {new_target_index}')
while dist < 10 or grace > 0 and new_target_index < len(targets) - 1 and new_source_index < len(sources) - 1:
if dist < 10:
grace = give_grace
else:
grace -= 1
new_target_index += 1
new_source_index += 1
dist = hex2bin(sources[new_source_index]) - hex2bin(targets[new_target_index])
# print(f'Dist: {dist}, grace: {grace}, s: {new_source_index}, t: {new_target_index}')
last_source = new_source_index
last_target = new_target_index
if new_target_index - target_index > 25:
ldv = [source_index, new_source_index, target_index, new_target_index]
print(ldv)
lds.append([source_index, new_source_index, target_index, new_target_index])
print(lds)
# src = hex2bin(sources[0])
# for target in targets:
# scores.append(src - hex2bin(target))
# plt.plot(scores)
# plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment