Face_recognition: how make the image rotate back that they are right ways

Created on 29 Aug 2019  路  9Comments  路  Source: ageitgey/face_recognition

how make the image rotate back that they are right ways

Most helpful comment

Hi
below post from @ageitgey about the handling EXIF rotation in your code.

https://medium.com/@ageitgey/the-dumb-reason-your-fancy-computer-vision-app-isnt-working-exif-orientation-73166c7d39da

hope this helps.

All 9 comments

face_recognition.load_image_file that image always sideways or upside-down images

With only so few information it will be hard to help you.

I would take a guess and think that the images are saved in the orientation that you receive them and they contain a EXIF Property which tells them to be rotated when viewed.

Either access the property and rotate them accordingly or ensure they are saved in the desired orientation and remove the EXIF property.

This numpy function should be of help:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.rot90.html

rotating images can be done using this code
inside api.py function load_image_file should rotate image if EXIF tag is not 1, before doing face detection, otherwise faces in incorrect orientation cannot be detected

    from PIL import Image, ExifTags

    img_path = "somefile_path"

    pil_image = Image.open(img_path).convert("RGB")
    img_exif = pil_image.getexif()
    ret = {}
    orientation  = 0
    if img_exif:
        for tag, value in img_exif.items():
            decoded = ExifTags.TAGS.get(tag, tag)
            ret[decoded] = value
        orientation  = ret["Orientation"]

    # fix orientation
    # if 8, 90 degree
    # if 3, 180 degree
    # if 6, 270 degree
    if orientation == 8:
        pil_image = pil_image.rotate(90, Image.NEAREST, expand=1)
    elif orientation == 3:
        pil_image = pil_image.rotate(180, Image.NEAREST, expand=1)
    elif orientation == 6:
        pil_image = pil_image.rotate(270, Image.NEAREST, expand=1)

Hi
below post from @ageitgey about the handling EXIF rotation in your code.

https://medium.com/@ageitgey/the-dumb-reason-your-fancy-computer-vision-app-isnt-working-exif-orientation-73166c7d39da

hope this helps.

You can use the jitter parameter in order to increase the type/method of distortion. It will recognize photos with different rotation.

@alessiosavi I tried increasing jitter but it doesn't work.

Which was the value? Higher or lesser than 300?

  1. So in order to work, I should increase it to 300 at least?

This number drive the times that the image are augmented. If you increase the jitter you have more chance to perform a rotation and recognize the face. I remember that with a jitter=300, i was able to recognize rotated photos (90掳). Note, the time to encode the face will increase by 300x

Was this page helpful?
0 / 5 - 0 ratings