2

In Ubuntu 16.04 I am trying to detect a face on a live video and save that image using OpenCV and Python. Specifically, I want to save just one image per face detected until I press 'q'. So for each different face that is detected, another picture of it, is taken. In the following code, the script is taking a picture each second until I exit.

import cv2

# Import the cascade for face detection
face_cascade = cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml')

# Access the webcam (every webcam has a number, the default is 0)
video = cv2.VideoCapture(0)

num = 0

while True:

# Capture frame-by-frame
    ret, frame = video.read()

# Detect faces in video
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

# Draw rectangles around faces
    for (x,y,w,h) in faces:
            cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]
# Display the image
    cv2.imshow('Video', frame)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)        
        cv2.imwrite('opencv'+str(num)+'.jpg',frame)
            num = num+1
# Press q for exit
    if cv2.waitKey(1) & 0xFF == ord('q'):
# Write frame in file

        break
video.release()
cv2.destroyAllWindows()

Any suggestions?

melvudin
  • 87
  • 7
  • You said `So for each different face that is detected, another picture of it, is taken. ` What do you mean by this? One face image per face, or one whole image with face labeled per face? And `cv2.waitKey(1)` is not waiting for `1 second`. – Kinght 金 Jan 09 '18 at 01:39
  • @Silencer One whole image per face detected, without cutting, it doesn't matter if the same face appears again. So, the person appears, take a single snapshot. Dont take another picture until this person disappears from the video, but only if another one appears with this person or separately of course. Im not sure if this is precise enough? Should I describe more? – melvudin Jan 09 '18 at 15:24

1 Answers1

0

As @Silencer commented, the answer depends on the meaning that you associate with "different face".

  1. If you want to actually recognize the faces as persons: One possibility is to actually create a database of detected faces (or their features) and then compare the faces in subsequent frames with those in the database. In this case, you need an algorithm that performs face recognition. OpenCV and dlib have libraries for this task.

  2. If you want to just record one photo for each appearance of a given face in front of the camera: record the size and location of the first set of faces that are detected when the program is run, and then check if the following frames contain faces that are similar in size and location (the frame rate has to be high for this to work). In this case you need a tracking algorithm to match faces correctly. If a face disappears from the scene and appears again later, it will be captured again.

Totoro
  • 3,398
  • 1
  • 24
  • 39
  • 2
    Without any database of known faces. Trigger a full snapshot when a face is detected. If another one comes, trigger again. Just take one picture until this single one remains in the scene. And if another one appears with the first one or alone, continue taking pictures. I hope this is a bit more clear. Thank you! – melvudin Jan 09 '18 at 15:31
  • In this case, the second option is fine. The code you posted above can be run at 12 frames/second on a not-so-old desktop so it should be easy to check if a face box in the current frame has 50% or more overlap with one in the previous frame. In that case, it is not a new face. – Totoro Jan 09 '18 at 20:16
  • 1
    I will try to figure that one out. Thank you! – melvudin Jan 10 '18 at 14:10