I want to cut the video automatically, by comparing the video frame with the picture, when scrolling through the video frames until the video frame is 90% similar to the picture, then cut at that time, I combine opencv and ffmpeg, I have the code below and when it comes to comparing the video frame with the image, it gives an error. Please help me edit and add it so that the program works.
Thank you
`import cv2
import subprocess as sp
import numpy as np
import imutils
from skimage.metrics import structural_similarity as compare_ssim
img1 = cv2.imread("F:\\d.png")
img1 = cv2.resize(img1, (480,700))
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
########################################################
ffmpeg = "ffmpeg"
input_file='f:\\tt.mp4'
height, width = 480, 700
command = [ffmpeg,
'-i', input_file,
'-pix_fmt', 'bgr24',
'-codec', 'rawvideo',
'-an',
'-sn',
'-f', 'image2pipe', '-']
#pipe = sp.Popen(command, stdout = sp.PIPE, bufsize=65536)
pipe = sp.Popen(command, stdout = sp.PIPE)
frameCount=0
while True:
nbytes =height*width
#print(f"Frame: {frameCount} - Trying to read {nbytes} bytes")
print(f"Frame: {frameCount} ")
#print(f" {frameCount} ")
frame = pipe.stdout.read(nbytes)
bytesRead = len(frame)
#print(f"Read {bytesRead} bytes")
if bytesRead==0:
break
image = np.frombuffer(frame, dtype='uint8') # convert read bytes to np
img = image.reshape((480,700))
##########################################################
(similar, diff) = compare_ssim(frame , gray1, full=True)
diff = (diff * 255).astype("uint8")
########################################################
cv2.imshow('Video', img)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
frameCount += 1
``