如何保存opencv检测到的人脸

How to save faces detected by opencv

我有检测人脸的代码。我只想将检测到的人脸保存为 jpg

这是我的程序的代码:

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()

如何保存检测到的人脸?请帮忙!

detectMultiScale方法returns一个列表,其中每个元素包含检测到的每个人脸的坐标和宽度和高度。

所以你可以使用 cv2.imwritearray 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)