如何正确计算时间

how to calculate time correctly

我在执行此操作时遇到问题: 我有一个视频,我想实时阅读并在上面画圈。我有三个列表,其中包含圆的 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: 我以这种方式尝试过,但它给了我错误

error: (-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()

主要有两个问题:

  • count += 1 应该在 for 循环之外。

  • 条件count % (t*fps) == 0不正确。
    你可能是想 if (count - (t*fps)) == 0
    如果 fps 不是整数,我们还可以检查:if abs(count - (t*fps)) <= 0.001
    如果 fps 是一个整数,我们可以简单地检查:

     if count == (t*fps)
    

调试代码:

与其处理一些任意视频文件,不如使用合成视频文件调试代码更简单。

以下代码创建合成视频,帧计数器 运行:

vid_filename = 'synthetic_video.mp4'
width, height, fps, n_frames = 320, 240, 25, 300

synthetic_out = cv2.VideoWriter(vid_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

for i in range(n_frames):
    img = np.full((height, width, 3), 60, np.uint8)
    cv2.putText(img, str(i+1), (width//2-40*len(str(i+1)), height//2+40), cv2.FONT_HERSHEY_DUPLEX, 4, (30, 255, 30), 8)  # Green number
    synthetic_out.write(img)

synthetic_out.release()

为了测试,在cv2.imshow('test', frame)之后添加cv2.waitKey(1000)
等待一整秒让我们看看圆圈是否绘制在正确的框架上。


下断点,开始调试,在watch中放置相关变量window。

示例(使用 PyCharm IDE):

它也有助于逐步执行代码...


完整代码示例:

import numpy as np
import cv2
import os.path

# Build synthetic video for testing (320x240 at 25fps and 300 frames)
################################################################################
vid_filename = 'synthetic_video.mp4'

if not os.path.isfile(vid_filename):  # Create the file only once (if not exists).
    width, height, fps, n_frames = 320, 240, 25, 300

    synthetic_out = cv2.VideoWriter(vid_filename, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

    for i in range(n_frames):
        img = np.full((height, width, 3), 60, np.uint8)
        cv2.putText(img, str(i+1), (width//2-40*len(str(i+1)), height//2+40), cv2.FONT_HERSHEY_DUPLEX, 4, (30, 255, 30), 8)  # Green number
        synthetic_out.write(img)

    synthetic_out.release()
################################################################################


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

# create a capture object-------------------------------------------------------------------

cap = cv2.VideoCapture(vid_filename)
count = 1  # Start counting from 1 - assume first frame is 1 and not zero (draw a circle on frames 25, 50, 75 and not 26, 51, 76...)
fps = int(cap.get(cv2.CAP_PROP_FPS))
i = 0

while cap.isOpened():
    ret, frame = cap.read()

    if ret is False:
        break  # Break loop in case ret is false (should be false after the last frame)

    for x, y, t in zip(a, b, times):
        if count == (t*fps):
            # draw circles
            cv2.circle(frame, (int(x), int(y)), 10, (255, 0, 0), -1)
            cv2.imshow('test', frame)
            cv2.waitKey(1000)

    count += 1
   
cap.release()
cv2.destroyAllWindows()

输出样本: