face_recognition
face_recognition copied to clipboard
Failing to Detect faces from IPhone images
- 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?
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")
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!
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_transposefrom 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!