【问题标题】:Creating time lapse images in python在 python 中创建延时图像
【发布时间】:2014-04-02 16:32:50
【问题描述】:

以下代码从网络摄像头捕获图像,并保存到磁盘中。我想编写一个程序,它可以每 30 秒自动捕获一次图像,直到 12 小时。最好的方法是什么?

import cv2     
cap = cv2.VideoCapture(0)
image0 = cap.read()[1]
cv2.imwrite('image0.png', image0)

以下是根据@John Zwinck 的回答进行的修改,因为我还需要将每 30 秒捕获的图像写入带有捕获时间的磁盘命名:

import time, cv2
current_time = time.time()
endtime = current_time + 12*60*60
cap = cv2.VideoCapture(0)
while current_time < endtime:    
    img = cap.read()[1]
    cv2.imwrite('img_{}.png'.format(current_time), img)
    time.sleep(30)

但是,上述代码每次只能将最后一个文件写入前一个文件。期待它的改进。

【问题讨论】:

    标签: python opencv timelapse


    【解决方案1】:

    你的输出图片名称是一样的!!!在 while 循环中,current_time 不会更改,因此它将以相同的名称保存每个帧。将 .format(current_time) 替换为 .format(time.time()) 应该可以。

    替换

    while current_time < endtime:
    img = cap.read()[1] cv2.imwrite('img_{}.png'.format(current_time), img) time.sleep(30)

    cv2.imwrite('img_{}.png'.format(int(time.time())), img)

    【讨论】:

      【解决方案2】:
      import time
      endTime = time.time() + 12*60*60 # 12 hours from now
      while time.time() < endTime:
          captureImage()
          time.sleep(30)
      

      【讨论】:

      • 我还需要将每30秒捕获的图像以捕获的时间命名
      • 太好了,所以尝试实现它。这不是免费的代码出租服务。如果您遇到困难,请务必提出一个新问题。
      • 我看到您已经知道如何将时间放入文件名中。那你现在好了?
      猜你喜欢
      • 2021-02-21
      • 2021-06-01
      • 2021-04-12
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 2012-05-14
      相关资源
      最近更新 更多