在Python中使用OpenCV检测mp4视频中检测到的对象位置的时间戳
我正在尝试使用Python中的OpenCV从mp4视频文件中检测对象。我能够检测到视频中的物体 我想获得视频文件中检测到对象的位置的时间戳,并将其写入文本文件 以下是我目前的代码:在Python中使用OpenCV检测mp4视频中检测到的对象位置的时间戳,python,opencv,Python,Opencv,我正在尝试使用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
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(),使用如下属性标识符:
video_capture.get(CV_CAP_PROP_POS_MSEC)
但获取错误未定义名称“CV\u CAP\u PROP\u POS\u 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确实实现了该属性,但需要使用全名 这应该行得通
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)
print (video_capture.get(cv2.CAP_PROP_POS_MSEC))
cv2.imshow('Video', canvas)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
i、 e.您想要实例
video\u capture
的属性,而不是泛型类VideoCapture
,并且属性名称的前缀是类cv2.
。属性名称是CAP\u PROP\u POS\MSEC
或CV\u CAP\u PROP\u MSEC
——取决于OpenCV版本(请参阅)。是的,您是对的,我更改了它,但仍然得到一个错误。更新问题。谢谢你@Chris Hill。打印需要在打印周围使用大括号(video_capture.get(cv2.CAP_PROP_POS_MSEC))。这管用!