Search code examples
pythonopencvpython-3.xvideo-captureface-detection

Auto-capture an image from a video in OpenCV using python


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.


Solution

  • 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.