Auto-capture an image from a video in OpenCV using

2019-04-12 13:25发布

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.

I am using the code-

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.

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-12 13:58

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-04-12 14:14

You need to add another 'cap.read()' line when the delay ends, as this is the code that actually captures the image.

查看更多
何必那么认真
4楼-- · 2019-04-12 14:23

use threading and define the cv.imshow() separately from your function

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
查看更多
登录 后发表回答