face_recognition icon indicating copy to clipboard operation
face_recognition copied to clipboard

Failing to Detect faces from IPhone images

Open cullena20 opened this issue 4 years ago • 3 comments

  • face_recognition version: 1.2.3
  • Python version: 3.9.1
  • Operating System: Mac

Description

I am working on a website to do emotion detection on faces from images. The first step is to detect faces in an image. This mostly works fine, but whenever I upload images straight from my iphone to the website, no faces are detected. The images are properly converted to numpy arrays, but no faces are recognized.

What I Did

image = face_recognition.load_image_file(image_fp) face_locations = face_recognition.face_locations(image)

print(image.shape) returns the expected shape so face_recognition.load_image_file is working. What could be going wrong?

cullena20 avatar Feb 07 '21 18:02 cullena20

You might have the same issue I had. It's because the iPhone automatically rotates the image based on exif tag, in order to display it in the correct orientation.

In other words, the image file you're uploading is probably rotated by 90 degrees, which is why no faces are detected.

To fix this, you need to process the image first before running face recognition. ImageOps.exif_transpose from the PIL library worked for me:

# Resize and rotate Image
            try:
                print("Processing Image...")
                img = Image.open(f)

                try:
                    #rotate accordingly
                    img = ImageOps.exif_transpose(img)   
                except:
                    pass

                img.thumbnail((800, 800))
                temp = BytesIO()
                img.save(temp, format="png")

fastfret888 avatar Feb 20 '21 08:02 fastfret888

Thank you! This doesn't seem to be what's happening though. It's passing because there is no exif tag and still no faces are being detected. Thanks anyway though!

cullena20 avatar Mar 18 '21 01:03 cullena20

You might have the same issue I had. It's because the iPhone automatically rotates the image based on exif tag, in order to display it in the correct orientation.

In other words, the image file you're uploading is probably rotated by 90 degrees, which is why no faces are detected.

To fix this, you need to process the image first before running face recognition. ImageOps.exif_transpose from the PIL library worked for me:

# Resize and rotate Image
            try:
                print("Processing Image...")
                img = Image.open(f)

                try:
                    #rotate accordingly
                    img = ImageOps.exif_transpose(img)   
                except:
                    pass

                img.thumbnail((800, 800))
                temp = BytesIO()
                img.save(temp, format="png")

It works for me. thank you!

KyleChenZ avatar Nov 29 '21 19:11 KyleChenZ