0
import cv2
import sys

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=2.0,
        minNeighbors=5,A        minSize=(30, 30),
        flags = 0
        #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        # Write frame in file
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)        
            cv2.imwrite('only_face.jpg')
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

The above code is detecting faces from live feed of camera and saving image of only one face. i want to save image of multiple faces that are detected from the feed. It only saves image of one face

  • You should include a for loop to increment the image each time it saves a new image – Jeru Luke Mar 19 '17 at 14:17
  • TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3') it is showing me this error – Talha Azhar Mar 19 '17 at 15:20

1 Answers1

0

See what I have included here:

import cv2
import sys

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)
increment = 0  #---Declare a variable that will increment for every image saved
while True:
# Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(gray, scaleFactor=2.0, minNeighbors=5,A        minSize=(30, 30), flags = 0
    #flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

# Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the resulting frame
cv2.imshow('Video', frame)
    increment = increment + 1  #--- Initiate the increment variable
    if cv2.waitKey(1) & 0xFF == ord('q'):
    # Write frame in file
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)        
            cv2.imwrite('only_face' + str(increment) + '.jpg')  #--- Here is where the increment variable is placed. It will be incremented for every face and thus saving every face that gets detected.
        break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

I have included the increment variable, which will be incremented for every face detected thus saving every image separately.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87