我尝试使用Python中的OpenCV从mp4视频文件中检测对象。我能探测到视频中的物体。在
我想得到视频文件中检测到对象的位置的时间戳,并将其写入文本文件。在
以下是我目前为止的代码:
import cv2
my_cascade = cv2.CascadeClassifier('toy.xml')
def detect(gray, frame):
toys= my_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in toys:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
#Logic to write time stamp to file goes here
return frame
video_capture = cv2.VideoCapture('home.mp4')
cv2.startWindowThread()
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
canvas = detect(gray, frame)
cv2.imshow('Video', canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
我尝试使用如下属性标识符使用VideoCapture的get()方法:
^{pr2}$但得到一个错误,名称“CV_CAP_PROP_POS_MSEC”未定义
似乎cv2没有实现此方法或属性标识符。在
在cv2中有没有其他方法来实现我想要的?在
请帮忙。在
已解决
这将打印每次在视频中检测到的对象位置的时间戳。 工作代码如下:
import cv2
my_cascade = cv2.CascadeClassifier('toy.xml')
def detect(gray, frame):
toys= my_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in toys:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
print("Toy Detected at: "+str(video_capture.get(cv2.CAP_PROP_POS_MSEC)))
return frame
video_capture = cv2.VideoCapture('home.mp4')
cv2.startWindowThread()
while True:
_, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
canvas = detect(gray, frame)
cv2.imshow('Video', canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
cv2确实实现了属性,但需要使用全名。在
这应该行得通
例如,您需要实例}。属性名是
video_capture
的属性,而不是泛型类VideoCapture
,并且属性名的前缀是类{CAP_PROP_POS_MSEC
或CV_CAP_PROP_MSEC
-这取决于OpenCV的版本(请参见Can't get VideoCapture property as the property identifier are not defined)。在相关问题 更多 >
编程相关推荐