|
from io import BytesIO |
|
from typing import Annotated |
|
|
|
import gradio as gr |
|
import numpy as np |
|
from deepface import DeepFace |
|
|
|
|
|
def get_face_type(file): |
|
try: |
|
attribute = DeepFace.analyze( |
|
img_path=file, |
|
actions=['age', 'gender'], |
|
) |
|
gender = attribute[0]['dominant_gender'] |
|
age = attribute[0]['age'] |
|
if gender == 'Man': |
|
if age < 10: |
|
face_type = 7 |
|
elif age < 20: |
|
face_type = 3 |
|
elif age < 30: |
|
face_type = 12 |
|
elif age < 40: |
|
face_type = 1 |
|
elif age < 50: |
|
face_type = 15 |
|
elif age < 60: |
|
face_type = 5 |
|
elif age < 70: |
|
face_type = 10 |
|
else: |
|
face_type = 8 |
|
elif gender == 'Woman': |
|
if age < 10: |
|
face_type = 14 |
|
elif age < 20: |
|
face_type = 0 |
|
elif age < 30: |
|
face_type = 4 |
|
elif age < 40: |
|
face_type = 6 |
|
elif age < 50: |
|
face_type = 13 |
|
elif age < 60: |
|
face_type = 2 |
|
elif age < 70: |
|
face_type = 9 |
|
else: |
|
face_type = 11 |
|
else: |
|
return "Face could not be detected." |
|
return f"face type:{face_type}---gender:{gender}---age:{age}" |
|
except Exception as e: |
|
print(e) |
|
return f"Face could not be detected." |
|
|
|
|
|
if __name__ == '__main__': |
|
demo = gr.Interface(fn=get_face_type, inputs="image", outputs="label") |
|
demo.launch(share=False) |
|
|