OpenCV不断获取视频的最后一帧

时间:2017-04-08 17:32:06

标签: python opencv

到目前为止,我可以在修改之前的内容之后抓住视频的最后一帧。 现在问题仍然存在,它抓住了最后一帧,从最后一帧开始,但之后没有。图像显示在GUI上,但即使看起来应该也不会更新。 我对python很陌生,所以任何帮助都会很棒。

main.py

import cv2
import numpy as np
import frame_counter

path = 'video.flv'
cap = cv2.VideoCapture(path)

template = cv2.imread('2.png',0)
w, h = template.shape[::-1]

while 1:

length = frame_counter.count(path)
print(length)
cap.set(1,length);

ret, img = cap.read()
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

print("Went through")

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

cv2.imshow('Detected',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
    break

cap.release()
cv2.destroyAllWindows()

frame_counter.py

# import the necessary packages
from imutils.video import count_frames

def count(path):

# count the total number of frames in the video file
total = count_frames(path, False)
return total

0 个答案:

没有答案