我是 opencv 的初学者,我尝试从我的项目的图片中裁剪一张脸,但无法裁剪图片中的所有脸。
可以做些什么来检测所有面部并将它们裁剪以移动到文件夹?
从输入文件夹中获取图像并将裁剪后的图像发布到输出文件夹。

import numpy as np
import cv2
import os, os.path

#multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades

#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('faces.xml')
#https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
eye_cascade = cv2.CascadeClassifier('eye.xml')

DIR = 'input'
numPics = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])

for pic in range(1, (numPics+1)):
    img = cv2.imread('input/'+str(pic)+'.jpg')
    height = img.shape[0]
    width = img.shape[1]
    size = height * width

    if size > (500^2):
        r = 500.0 / img.shape[1]
        dim = (500, int(img.shape[0] * r))
        img2 = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
        img = img2

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    eyesn = 0

    for (x,y,w,h) in faces:
        imgCrop = img[y:y+h,x:x+w]
        #cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            #cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
            eyesn = eyesn +1
        if eyesn >= 2:
            cv2.imwrite("output/crop"+str(pic)+".jpg", imgCrop)

    #cv2.imshow('img',imgCrop)
    print("Image"+str(pic)+" has been processed and cropped")
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

#cap.release()
print("All images have been processed!!!")
cv2.destroyAllWindows()
cv2.destroyAllWindows()

如何裁剪图片中的所有面孔以保存在文件夹中?

最佳答案

#### the counter
cnt = 0

for pic in range(1, (numPics+1)):
    img = cv2.imread('input/'+str(pic)+'.jpg')
    height = img.shape[0]
    width = img.shape[1]
    size = height * width

    if size > (500^2):
        r = 500.0 / img.shape[1]
        dim = (500, int(img.shape[0] * r))
        img2 = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
        img = img2

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        eyesn = 0
        imgCrop = img[y:y+h,x:x+w]
        #cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            #cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
            eyesn = eyesn +1
        if eyesn >= 2:
            #### increase the counter and save
            cnt +=1
            cv2.imwrite("output/crop{}_{}.jpg".format(pic, cnt), imgCrop)

            #cv2.imshow('img',imgCrop)
            print("Image"+str(pic)+" has been processed and cropped")

    k = cv2.waitKey(100) & 0xff
    if k == 27:
        break

#cap.release()
print("All images have been processed!!!")
cv2.destroyAllWindows()
cv2.destroyAllWindows()

关于python - 使用 opencv 从图片中裁剪多个人脸并将它们存储在一个文件夹中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47495048/

10-13 09:14