ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.23k stars 13.47k forks source link

How crop face from image? #801

Open arpsyapathy opened 5 years ago

arpsyapathy commented 5 years ago

Hello!

Help me please with simple question. I want crop face from image in separate image cropped_face.jpg. how can I do that?

Thank you!

SimonLovskog commented 5 years ago

This should do it. This will load the image file from the inputImg variable, then it will find all the faces and save each face in the image, the cropped faces will be named "img###.png", where ### is the so that is can save multiple faces without conflicting file names.

from PIL import Image
import face_recognition

inputImg = "biden.jpg"

image = face_recognition.load_image_file(inputImg)
faces = face_recognition.face_locations(image)

for i in range(len(faces)):
    top, right, bottom, left = faces[i]

    faceImage = image[top:bottom, left:right]
    final = Image.fromarray(faceImage)
    final.save("img%s.png" % (str(i)), "PNG")
gfobiyatechnical commented 5 years ago

Please go through the links shared below, ....if than also you find any kind of problem ,please let me know.....

links: 1. http://gregblogs.com/computer-vision-cropping-faces-from-images-using-opencv2/ 2. https://codereview.stackexchange.com/questions/156736/cropping-faces-from-images-in-a-directory

if you would share image of where you are stucking with, it will be easy for me to identify actual problem.

On Sun, Apr 14, 2019 at 1:08 AM Simon Lövskog notifications@github.com wrote:

This should do it. This will load the image file from the inputImg variable, then it will find all the faces and save each face in the image, the cropped faces will be named "img###.png", where ### is the so that is can save multiple faces without conflicting file names.

from PIL import Image import face_recognition

inputImg = "biden.jpg"

image = face_recognition.load_image_file(inputImg) faces = face_recognition.face_locations(image)

for i in range(len(faces)): top, right, bottom, left = faces[i]

faceImage = image[top:bottom, left:right]
final = Image.fromarray(faceImage)
final.save("img%s.png" % (str(i)), "PNG")

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ageitgey/face_recognition/issues/801#issuecomment-482872229, or mute the thread https://github.com/notifications/unsubscribe-auth/AqOF-0myt1vHQQXtkRSF06LkP2nFurlfks5vgjI5gaJpZM4cts24 .

vinaywadhwa commented 5 years ago

I extended @SimonCircle 's code to extract faces from all images in a folder (with the ability to provide source, output dirs), someone might find it useful:

import argparse
import os
import face_recognition
from PIL import Image

def extract_faces_from_image(source_image_file, output_directory):
    image = face_recognition.load_image_file(source_image_file)
    faces = face_recognition.face_locations(image)
    extracted_image_file_paths = []
    for i in range(len(faces)):
        top, right, bottom, left = faces[i]
        extracted_image = Image.fromarray(image[top:bottom, left:right])
        extracted_image_file_path = "%s_face_%s.png" % (source_directory_file, str(i))
        print(" Saving - %s"%extracted_image_file_path)
        extracted_image.save("%s/%s" % (output_directory, extracted_image_file_path), "PNG")
        extracted_image_file_paths.append(extracted_image_file_path)
    return extracted_image_file_paths

if __name__ == "__main__":
    print("||Running Face Extractor||")
    parser = argparse.ArgumentParser(
        description='This program extracts/crops out face-images out of the images, saves each face as a png.')
    parser.add_argument('-sd', '--source_directory', default='.',
                        help='[Optional] Absolute path to the directory of the source images (relative paths like ~/Downloads may not work)')
    parser.add_argument('-od', '--output_directory',
                        help='[Optional] Absolute path to the output directory (relative paths like ~/Downloads may not work)')

    args = parser.parse_args()
    source_directory = args.source_directory
    output_directory = args.output_directory

    if output_directory is None:
        output_directory = source_directory

    source_directory_files = os.listdir(source_directory)

    for source_directory_file in source_directory_files:
        print("Processing - %s"%source_directory_file)
        source_file_path = "%s/%s" % (source_directory, source_directory_file)
        extract_faces_from_image(source_file_path, output_directory)
sandip-narwade commented 5 years ago

@SimonCircle thank you sir. Its working for me...