我编写了一个代码,该代码将最终捕获图片,并在达到一定时间后终止。
但是一旦脚本结束(即在使用时间终止代码之后),几秒钟后我将如何自动重新运行?
import cv2
import time
import numpy
capture = cv2.VideoCapture(0)
capture.set(3,640)
capture.set(4,480)
img_counter = 0
frame_set = []
start_time = time.time()
while(True):
ret , frame = capture.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame_set.append(gray)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
end_time = time.time()
elapsed = end_time - start_time
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name,gray)
print("{} written!".format(img_counter))
img_counter +=1
if elapsed > 1:
break
capture.release()
cv2.destroyAllWindows()
此代码会将图像保存到文件夹中,并在几秒钟后终止。几分钟后如何设置自动重新运行代码以再次捕获更多图像?
谢谢是提前。
最佳答案
如果您在Windows上,则可以在Task Scheduler中添加.py
脚本,并给它运行时间described here:
但是,如果要在python文件中执行此操作,则有几种方法。
第一种方法:
锁定计时器,而不是break
退出循环。
就像是:
import time
starttime=time.time()
while True:
print("tick every 3 seconds")
time.sleep(3)
因此,您的代码应为:
import cv2
import time
import numpy
capture = cv2.VideoCapture(0)
capture.set(3,640)
capture.set(4,480)
img_counter = 0
frame_set = []
start_time = time.time()
while(True):
ret , frame = capture.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame_set.append(gray)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
end_time = time.time()
elapsed = end_time - start_time
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name,gray)
print("{} written!".format(img_counter))
img_counter +=1
if elapsed > 1:
capture.release()
cv2.destroyAllWindows()
time.sleep(3)
第二种方法:
使用Twisted, which is an event-driven networking engine written in Python.,
from twisted.internet import task, reactor
timeout = 60.0 # Sixty seconds
def doWork():
#do work here
pass
l = task.LoopingCall(doWork)
l.start(timeout) # call every sixty seconds
reactor.run()
在这种情况下,您不需要时间锁定:
from twisted.internet import task, reactor
import cv2
import time
import numpy
capture = cv2.VideoCapture(0)
capture.set(3,640)
capture.set(4,480)
img_counter = 0
frame_set = []
start_time = time.time()
timeout = 60.0 # Sixty seconds
def doWork():
while (True):
ret, frame = capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame_set.append(gray)
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
end_time = time.time()
elapsed = end_time - start_time
img_name = "opencv_frame_{}.png".format(img_counter)
cv2.imwrite(img_name, gray)
print("{} written!".format(img_counter))
img_counter += 1
if elapsed > 1:
break
capture.release()
cv2.destroyAllWindows())
l = task.LoopingCall(doWork)
l.start(timeout) # call every sixty seconds
reactor.run()
Note: Since your are capturing pictures and will terminating the script after it reaches a certain time, you need to sync the
timeout
in this case with yourelapsed
time to make sure it does not collide. i.e. if the script is capturing pictures for 5 minutes then the timeout should be greater than that. In other words, Run the script after uhm, let's say 7 minutes and it will take 5 minutes for capturing the pictures.
关于python - 无休止地打开和关闭网络摄像头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54644562/