使用python在OpenCV中自动捕获视频中的图像 [英] Auto-capture an image from a video in OpenCV using python

查看:247
本文介绍了使用python在OpenCV中自动捕获视频中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发用作自拍相机的代码.视频将显示在窗口中,并且将连续检测人的脸和眼睛,并且一旦用户选择了特定时间,就会捕获该时间点的帧.我可以使用时间模块中的睡眠功能在一定时间后捕获帧,但是视频帧似乎死机了.有什么解决方法可以让我继续观看视频,并且经过一段时间的延迟后会自动进行视频捕获.

I am trying developing a code which functions as the self-timer camera. The video would be seen in the window and the person's face and eyes would be continuously detected and once the user selects a specific time, the frame at that point of time is captured. I am able to capture the frame after a certain time using sleep function in time module but the video frame seems to freeze. Is there any solution such that I can continue to see the video and the video capture takes place after some delay automatically.

我正在使用代码-

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()

有人可以建议我吗?任何帮助将不胜感激.

Can someone suggest me? Any kind of help would be appreciated.

推荐答案

要连续观看视频,您需要重复代码的相同部分,该部分首先显示视频,然后将其置于while循环中.确保窗口的句柄没有丢失.您可以将捕获作为鼠标单击事件并使用刻度计数,一次在while循环开始之前,一次在循环内部.一旦两个滴答计数之间的差等于某个预定义的秒数,则捕获该帧,使用break并退出while循环.

In order to continuously view the video, you need to repeat the same part of the code which displays the video first and put it in a while loop. Make sure that the handle to the window is not lost.You can make the capture as a mouse click event and use a tickcount, one before the start of the while loop and one inside the loop. Once the difference between the two tick counts is equal to some pre-defined seconds,capture that frame, use break and come out of the while loop.

这篇关于使用python在OpenCV中自动捕获视频中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆