如何保存open检测到的面

2024-03-29 01:46:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有识别人脸的密码。我要做的就是将检测到的人脸保存为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()

如何保存检测到的人脸?请帮帮我!你知道吗


Tags: importnumpy密码imgasnpcv2detector
1条回答
网友
1楼 · 发布于 2024-03-29 01:46:15

detectMultiScale方法返回一个列表,其中每个元素包含检测到的每个面的坐标、宽度和高度。你知道吗

因此可以使用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)

相关问题 更多 >