使用python从OpenCV中的视频自动捕获图像
我正在尝试开发一个代码,它可以作为自拍相机使用。视频将在窗口中看到,人的脸和眼睛将被连续检测到,一旦用户选择特定时间,该时间点的帧将被捕获。我可以使用时间模块中的睡眠功能捕获特定时间后的帧,但视频帧似乎冻结。是否有任何解决方案可以让我继续观看视频,并在延迟一段时间后自动进行视频捕获 我正在使用代码-使用python从OpenCV中的视频自动捕获图像,python,opencv,python-3.x,video-capture,face-detection,Python,Opencv,Python 3.x,Video Capture,Face Detection,我正在尝试开发一个代码,它可以作为自拍相机使用。视频将在窗口中看到,人的脸和眼睛将被连续检测到,一旦用户选择特定时间,该时间点的帧将被捕获。我可以使用时间模块中的睡眠功能捕获特定时间后的帧,但视频帧似乎冻结。是否有任何解决方案可以让我继续观看视频,并在延迟一段时间后自动进行视频捕获 我正在使用代码- import numpy as np import cv2 import time import cv2.cv as cv cap = cv2.VideoCapture(0) while(True
import numpy as np
import cv2
import time
import cv2.cv as cv
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',frame)
#time.sleep(01)
Capture = cv.CaptureFromCAM(0)
time.sleep(5)
ret, frame = cap.read()
image = cv.QueryFrame(Capture) #here you have an IplImage
imgarray = np.asarray(image[:,:]) #this is the way I use to convert it to numpy array
cv2.imshow('capImage', imgarray)
cv2.waitKey(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
有人能推荐我吗?任何形式的帮助都将不胜感激。延迟结束时,您需要添加另一个“cap.read()”行,因为这是实际捕获图像的代码。为了连续查看视频,您需要重复先显示视频的同一部分代码,并将其放入while循环。确保窗口的句柄没有丢失。您可以将捕获作为鼠标单击事件,并使用tickcount,一个在while循环开始之前,一个在循环内部。一旦两个刻度计数之间的差值等于预定义的秒数,捕获该帧,使用break并退出while循环。使用threading并与函数分开定义cv.imshow()
import threading
import cv2
def getFrame():
global frame
while True:
frame = video_capture.read()[1]
def face_analyse():
while True:
#do some of the opeartion you want
def realtime():
while True:
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
video_capture.release()
cv2.destroyAllWindows()
break
if __name__ == "__main__":
video_capture = cv2.VideoCapture(cam)
frame = video_capture.read()[1]
gfthread = threading.Thread(target=getFrame, args='')
gfthread.daemon = True
gfthread.start()
rtthread = threading.Thread(target=realtime, args='')
rtthread.daemon = True
rtthread.start()
fathread = threading.Thread(target=face_analyse, args='')
fathread.daemon = True
fathread.start()
while True: #keep main thread running while the other two threads are non-daemon
pass
是的,它可以工作,但我想看视频。一旦调用睡眠,视频帧就会冻结。我希望它能像手机的自拍器一样工作。请删除帖子中所有不必要的代码。我删除了代码中的人脸检测部分。只剩下必要的部分了为什么对同一个id使用2个视频捕获
Capture=cv.CaptureFromCAM(0)
将失败,因为它已在使用中。请坚持使用cv2 api,旧的cv api不能再使用了