【发布时间】:2021-11-18 07:16:47
【问题描述】:
我在执行此操作时遇到问题: 我有一个视频,我想阅读它并在上面实时画圈。我有三个列表,其中包含圆圈的 x、y 坐标和秒数。
x = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
y = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
times = [1,2,3,4,5,6,7,8,9,10]#Seconds
我想在视频上绘制每个坐标与每一秒关联的视频,所以
One second, Draw a circle with 20(x) and 20(y)
Two second, Draw a circle with 30(x) and 30(y)
Three second, Draw a circle with 40(x) and 40(y)...
等等。
我尝试了一些东西,但我真的很糟糕
import cv2
import numpy as np
import time
a = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
b = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
time = [1,2,3,4,5,6,7,8,9,10]#Seconds
#ceate a capture object-------------------------------------------------------------------
cap=cv2.VideoCapture(r'C:/Users/aless/Documents/GitHub/Tobii-Glasses-Thesis/video/scenevideo5.mp4')
i=0
while(cap.isOpened()):
ret, frame = cap.read()
time_passed = int(cap.get(cv2.CAP_PROP_POS_MSEC))
if time_passed % (time[i]*1000) and i<=(len(time)-1):
print(time_passed)
# draw circles
cv2.circle(frame, (int(a[i]),int(b[i])), 10, (255, 0, 0), -1)
cv2.imshow('test', frame) # draw
i+=1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
绘制第一个圆圈后,它不会绘制另一个。有人可以帮忙吗? :C
编辑 1: 我尝试过这种方式,但它给了我错误
错误:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
import cv2
import numpy as np
import time
a = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
b = ['20' , '30' , '40', '50','60' , '70' , '80', '90','100' , '110' ]
times = [1,2,3,4,5,6,7,8,9,10]#Seconds
#ceate a capture object-------------------------------------------------------------------
cap=cv2.VideoCapture(r'C:/Users/aless/Documents/GitHub/Tobii-Glasses-Thesis/video/scenevideo5.mp4')
count = 0
success = True
fps = int(cap.get(cv2.CAP_PROP_FPS))
i=0
while(cap.isOpened()):
ret, frame = cap.read()
time_passed = int(cap.get(cv2.CAP_PROP_POS_MSEC))
for x,y,t in zip(a,b,times):
if count%(t*fps) == 0 :
# draw circles
cv2.circle(frame, (int(x),int(x)), 10, (255, 0, 0), -1)
cv2.imshow('test', frame) # draw
count+=1
cap.release()
cv2.destroyAllWindows()
【问题讨论】:
-
尝试不使用
if cv2.waitKey(1) & 0xFF == ord('q'): break -
@GhostOps 查看编辑,不工作
-
总是检查错误。视频捕捉总是一样的。
assert cap.isOpened()(一次!)并在循环中:if not ret: break就在每个 cap.read 之后 -
下一期:毫秒不是秒。它们之间的系数为 1000。只需使用
int(cap.get(cv2.CAP_PROP_POS_MSEC) / 1000),这对您的代码来说已经足够了。然后将该值用作列表中的索引。 删除count%(t*fps) == 0,没有意义