Detect face then autocrop pictures

前端 未结 11 884
慢半拍i
慢半拍i 2021-01-29 17:44

I am trying to find an app that can detect faces in my pictures, make the detected face centered and crop 720 x 720 pixels of the picture. It is rather very time consuming &

11条回答
  •  无人共我
    2021-01-29 18:12

    Detect face and then crop and save the cropped image into folder ..

    import numpy as np
    import cv2 as cv
    face_cascade = cv.CascadeClassifier('./haarcascade_frontalface_default.xml')
    #eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
    img = cv.imread('./face/nancy-Copy1.jpg')
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        cv.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:
         #   cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
        sub_face = img[y:y+h, x:x+w]
        face_file_name = "face/" + str(y) + ".jpg"
        plt.imsave(face_file_name, sub_face)
    plt.imshow(sub_face)


提交回复
热议问题