Search code examples
pythonimage-processingface-recognition

Remove all but specific part of image


I am using Python face-recognition library to detect faces in an image, it returnes coordenates of faces in the image. i would like to delete all but the coordenates returned by the face-recognition function

import face_recognition

image1 = face_recognition.load_image_file("image extract/image.jpg")
face_locations = face_recognition.face_locations(image1)
print(face_locations)

Solution

  • thanks to @Mark Setchell and @Bohdan 's help

        count2=0
        for file in image_folder:
            loc=f"image extract/frame{count2}.jpg"
            image1 = face_recognition.load_image_file("image extract/"+file)
            face_locations = face_recognition.face_locations(image1)
            width, height =0,0
            for face_location in face_locations:
                top, right, bottom, left = face_location
                face_image = image1[top+20:bottom+20, left+20:right+20]
                pil_image = Image2.fromarray(face_image)
                if (pil_image.size[0]*pil_image.size[1]>width*height):
                    width, height = pil_image.size
                    pil_image.save(loc)