Detect face then autocrop pictures

前端 未结 11 609
慢半拍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:23

    Just adding to @Israel Abebe's version. If you add a counter before image extension the algorithm will give all the faces detected. Attaching the code, same as Israel Abebe's. Just adding a counter and accepting the cascade file as an argument. The algorithm works beautifully! Thanks @Israel Abebe for this!

    import cv2
    import os
    import sys
    
    def facecrop(image):
    facedata = sys.argv[1]
    cascade = cv2.CascadeClassifier(facedata)
    
    img = cv2.imread(image)
    
    minisize = (img.shape[1],img.shape[0])
    miniframe = cv2.resize(img, minisize)
    
    faces = cascade.detectMultiScale(miniframe)
    counter = 0
    for f in faces:
        x, y, w, h = [ v for v in f ]
        cv2.rectangle(img, (x,y), (x+w,y+h), (255,255,255))
    
        sub_face = img[y:y+h, x:x+w]
        fname, ext = os.path.splitext(image)
        cv2.imwrite(fname+"_cropped_"+str(counter)+ext, sub_face)
        counter += 1
    return
    
    facecrop("Face_detect_1.jpg")
    

    PS: Adding as answer. Was not able to add comment because of points issue.

提交回复
热议问题