Arpit
Arpit

Reputation: 3

How to save faces detected by opencv

I have code that detects a face. All I want to do is save the detected face as a jpg

Here is the code for my program:

import numpy as np
import cv2

detector= cv2.CascadeClassifier('haarcascade_fullbody.xml')
cap = cv2.VideoCapture(0)

while(True):
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = detector.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

    cv2.imshow('frame',img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

How do I save the detected face? Please help!

Upvotes: 0

Views: 3199

Answers (1)

Mohit Motwani
Mohit Motwani

Reputation: 4792

detectMultiScale method returns a list where each elements contains the coordinates and width and height of each face that is detected.

So you can use cv2.imwrite and array slicing:

count = 0
for (x,y,w,h) in faces:
        face = img[y:y+h, x:x+w] #slice the face from the image
        cv2.imwrite(str(count)+'.jpg', face) #save the image
        count+=1
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

Upvotes: 2

Related Questions