Detect face then autocrop pictures

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

    Another available option is dlib, which is based on machine learning approaches.

    import dlib
    from PIL import Image
    from skimage import io
    import matplotlib.pyplot as plt
    
    
    def detect_faces(image):
    
        # Create a face detector
        face_detector = dlib.get_frontal_face_detector()
    
        # Run detector and get bounding boxes of the faces on image.
        detected_faces = face_detector(image, 1)
        face_frames = [(x.left(), x.top(),
                        x.right(), x.bottom()) for x in detected_faces]
    
        return face_frames
    
    # Load image
    img_path = 'test.jpg'
    image = io.imread(img_path)
    
    # Detect faces
    detected_faces = detect_faces(image)
    
    # Crop faces and plot
    for n, face_rect in enumerate(detected_faces):
        face = Image.fromarray(image).crop(face_rect)
        plt.subplot(1, len(detected_faces), n+1)
        plt.axis('off')
        plt.imshow(face)
    

提交回复
热议问题