我编写了一个代码,该代码将最终捕获图片,并在达到一定时间后终止。 但是一旦脚本结束(即在使用时间终止代码之后),几秒钟后我将如何自动重新运行??
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()
此代码会将图像保存到文件夹中,并在几秒钟后终止。几分钟后如何设置自动重新运行代码以再次捕获更多图像?
谢谢。
答案 0 :(得分:1)
如果您在Windows上,则可以在任务计划程序中添加int Number1 = 0;
int Number12 = 0;
// Do not forget to initialize the varaiables:
string text = "";
string text2 = "";
// Create (and initialize by system tomer) Random once, use many
Random rnd = new Random();
// Do not use goto, but loops (they are more readable: we have an infinite loop here)
while (true) {
int month = rnd.Next(1, 10);
// (month = 8) is an assignment, not comparison which is (month == 8)
// Let's use C/C++ language trick: comparing in reversed order:
// (8 == month) and you can easily find out such errors
if (8 == month)
Number1++;
else if (3 == month)
Number12++;
if (1 == Number1) {
text = "*";
// you want to leave the infinite loop (your current code never stops)
break;
}
}
// Let's inspect the outcome
Console.Write($"Number1 = {Number1}; Number12 = {Number12} Text = {text}");
脚本,并给其运行时间described here:
但是,如果要在python文件中执行此操作,则有几种方法。
第一种方法:
锁定计时器,而不是.py
退出循环。
类似的东西:
break
因此您的代码应为:
import time
starttime=time.time()
while True:
print("tick every 3 seconds")
time.sleep(3)
第二种方法:
使用Twisted, which is an event-driven networking engine written in Python.,
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)
在这种情况下,您不需要时间锁定:
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()
在这种情况下,请确保您有timeout
的时间 碰撞。即如果脚本正在捕获图片5分钟,则 超时应大于该值。换句话说,运行 嗯之后的脚本,假设是7分钟,而这需要5分钟 捕获图片。