别不多说,直接上代码。
import threading,time
import cv2
import os
i=0
def func(i,frame):
print(time.time(),"Hello Timer!",str(i))
while os.path.exists("/home/yang/data1/"+str(i)+".jpg"):
i+=1
cv2.imwrite('/home/yang/data1/' + str(i) + ".jpg", frame)#将拍摄到的图片保存在data1文件夹中
cap = cv2.VideoCapture(0)
t1=time.time()
while(1):
ret,frame=cap.read()
cv2.imshow('capture',frame)
t2=time.time()
if (t2-t1)>=1:
s = threading.Timer(1,func,(i,frame,))
s.start()
t1=t2
i=i+1
if cv2.waitKey(1)&0xFF==ord('q'):#按键盘q就停止拍照
break
cap.release()
cv2.destroyAllWindows()
这段代码展示了如何使用OpenCV从摄像头读取视频流,并以一定频率保存帧为图片。通过创建定时器,程序每秒调用一次`func`函数,检查并保存未存在的图片文件。当按下'q'键时,程序停止捕获并关闭窗口。
1360

被折叠的 条评论
为什么被折叠?



