Detect face then autocrop pictures

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

    the above codes work but this is recent implementation using OpenCV I was unable to run the above by the latest and found something that works (from various places)

    import cv2
    import os
    
    def facecrop(image):
        facedata = "haarcascade_frontalface_alt.xml"
        cascade = cv2.CascadeClassifier(facedata)
    
        img = cv2.imread(image)
    
        minisize = (img.shape[1],img.shape[0])
        miniframe = cv2.resize(img, minisize)
    
        faces = cascade.detectMultiScale(miniframe)
    
       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_"+ext, sub_face)
    
    
    
        return
    
    
    
    facecrop("1.jpg")
    

提交回复
热议问题