2

My objective is to determine similarity between two exact same videos. My approach is a bit naive i.e. to compare them frame by frame and see if two frame exactly matches or not. I am using the following python code for this:

import cv2
import numpy as np

capture = cv2.VideoCapture("video.wmv")
capture2 = cv2.VideoCapture("video.wmv")
counter = 0
while True:
    f, frame = capture.read()
    f2, frame2 = capture2.read()
    frame = cv2.GaussianBlur(frame,(15,15),0)
    frame2 = cv2.GaussianBlur(frame2, (15, 15), 0)
    try:
        res = frame - frame2
        if(np.count_nonzero(res) > 0 ):
            counter += 1
        else: continue
    except:
        print(counter)

The total number of frames in my video is around 600K. The code runs for almost 20K frames with exact matches and the counter remains zero (i.e. exact frame matching) but after 20K frames it started returning the following exception for all frames to come

unsupported operand type(s) for -: 'NoneType' and 'NoneType'

From the exception I understand that it is not reading any frames that is why returning a NonType. Please guide me whether my approach for comparing videos is correct (I know it is not efficient way). Also why I am getting this error ?

Alex Kulinkovich
  • 4,408
  • 15
  • 46
  • 50
muazfaiz
  • 4,611
  • 14
  • 50
  • 88
  • 1
    Why don't you just run a simple file comparison to see if two videos are equal? It would be more efficient as it would only iterate over the compressed data and hence more efficient. This thread may be helpful: http://stackoverflow.com/questions/1072569/see-if-two-files-have-the-same-content-in-python – ZdaR Mar 20 '17 at 11:44
  • Actually it is just the beginning. My later objective is to analyze the difference (or similarity) between two videos. So, I am trying to take my approach and see which frames are not same (i.e. res = frame - frame2 > 0) and determine that there might be some difference in at that particular point in the video. – muazfaiz Mar 20 '17 at 11:52
  • Your question doesn't make sense. If both videos are exactly the same you don't have to compare them... – Piglet Mar 20 '17 at 11:55
  • I have put same videos just in this example. But just comparing the same videos is not the objective. I tried to put the same videos in order to be sure that my code works for same videos at least. – muazfaiz Mar 20 '17 at 11:57
  • 2
    You are into a infinite While Loop. and I guess your input video has only 20K frames thats why you are getting NoneType :) – ZdaR Mar 20 '17 at 12:06
  • The total frames I got was around 600K using `n_frames= int(capture.get(cv2.CAP_PROP_FRAME_COUNT))`. So I think I am not into an infinite loop. – muazfaiz Mar 20 '17 at 12:12
  • Sure, but your While True is an infinite loop. Why not for i < n_frames instead ? – Soltius Mar 20 '17 at 13:13
  • I tried i < n_frames but still getting the same error at the same index – muazfaiz Mar 20 '17 at 15:27

1 Answers1

0

You didn't break your while loop. And it continued reading the frames even when the vidoes had finished, hence returning a NoneType object. You can try:

while True:
f, frame = capture.read()
f2, frame2 = capture2.read()
if not f and not f2:
    break
frame = cv2.GaussianBlur(frame,(15,15),0)
frame2 = cv2.GaussianBlur(frame2, (15, 15), 0)
try:
    res = frame - frame2
    if(np.count_nonzero(res) > 0 ):
        counter += 1
    else: continue
except:
    print(counter)
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 12 '22 at 23:23