【问题标题】:How to save faces detected by opencv如何保存opencv检测到的人脸
【发布时间】:2019-11-25 02:20:47
【问题描述】:

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

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

【问题讨论】:

  • 没有检测到的人脸。好像有多个。否则,为什么要使用 for 循环?

标签: python opencv face-detection face-recognition


【解决方案1】:

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)

【讨论】:

  • 非常感谢兄弟,它成功了!同时将count+更改为str(count+)
猜你喜欢
  • 2012-03-08
  • 2016-01-01
  • 2021-01-22
  • 2015-03-30
  • 1970-01-01
  • 2012-02-04
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多