serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
11.87k stars 2.02k forks source link

Validate image is a face #76

Closed arkkanoid closed 4 years ago

arkkanoid commented 4 years ago

Hi, I'm analyzing thousands of images but sometimes it's not a face. image The image I attach is a sample of them. In this case, the result is: emotion: neutral gender:Man race: white race_ratio:99 age:31.

I'd like to filter this can of images, as it obviously is not a face. Can I do it with DeepFace?

Thanks in advance

serengil commented 4 years ago

Could you share the code block you are running?

from deepface import DeepFace
obj = DeepFace.analyze("test-image.png")
print(obj)

When I pass the image you've attached, then it returns an error.

ValueError: Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.

arkkanoid commented 4 years ago

Hi @serengil ,

import urllib.request
from deepface import DeepFace
from deepface.extendedmodels import Age, Gender, Race, Emotion
os.environ["CUDA_VISIBLE_DEVICES"]=""
models = {}
models["emotion"] = Emotion.loadModel()
models["age"] = Age.loadModel()
models["gender"] = Gender.loadModel()
models["race"] = Race.loadModel()
urllib.request.urlretrieve('https://user-images.githubusercontent.com/3811604/89106034-c6f4d000-d426-11ea-8df4-e5bc90a43c8b.png', "img.png")
res = DeepFace.analyze("img.png", actions = ['age', 'gender', 'race', 'emotion'], enforce_detection=False, models=models)

Result:

{'age': 31.641831879826373, 'gender': 'Man', 'race': {'asian': 0.08276459589240302, 'indian': 5.141781640355836e-05, 'black': 4.015000546356394e-05, 'white': 99.85778333451947, 'middle eastern': 0.04132406991954805, 'latino hispanic': 0.01803678245403976}, 'dominant_race': 'white', 'emotion': {'angry': 5.5653750523880815e-06, 'disgust': 1.1916463810202048e-12, 'fear': 0.044355897067529314, 'happy': 0.051120464649592086, 'sad': 0.015812459903308314, 'surprise': 0.004936415419228213, 'neutral': 99.8837649960798}, 'dominant_emotion': 'neutral'}
serengil commented 4 years ago

Set enforce_detection argument to true. Its default value is true btw. If you don’t pass it, it will be true as well.

arkkanoid commented 4 years ago

Working! Thanks