python - 使用opencv从一张图片中裁剪多张人脸并将它们存储在一个文件夹中

标签 python numpy opencv ubuntu face-detection

我是 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/

相关文章:

python - VS2013中的PTVS : Why does this for loop not catch the StopIteration?

Python 脚本正在运行。我有一个方法名称作为字符串。我该如何调用这个方法?

python - 为什么 numpy.empty_like() 返回用零填充的数组不安全?

python - 通过分布项乘以 Numpy 数组

c++ - knnMatchImpl 中的断言失败 (queryDescriptors.type() == trainDescCollection[0].type()),

python - 无法完成 OpenCV 的安装。 'numpy/ndarrayobject.h' 找不到文件

python - python装饰器可以使函数能够智能地操作单个对象和集合对象吗?

javascript - 在不同的 html 模板中链接两个面板的方法

Python:x[i,:] 和 x[i][:] 之间有什么区别

image - OpenCV:使用霍夫变换进行抛物线检测