Alex Gonzalez commited on
Commit ·
851ec4b
1
Parent(s): a18bc6a
Facial Recognition Before Prediction
Browse files- app.py +14 -2
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,16 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastai.vision.all import *
|
| 3 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 4 |
-
|
| 5 |
from PIL import Image
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
learn_inf = load_learner("export.pkl")
|
| 9 |
processor = AutoImageProcessor.from_pretrained("dima806/facial_emotions_image_detection")
|
| 10 |
model = AutoModelForImageClassification.from_pretrained("dima806/facial_emotions_image_detection")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def predict(value) -> str:
|
| 13 |
-
image = Image.fromarray(value)
|
| 14 |
inputs = processor(images=image, return_tensors="pt")
|
| 15 |
outputs = model(**inputs)
|
| 16 |
logits = outputs.logits
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastai.vision.all import *
|
| 3 |
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import requests
|
| 6 |
+
import face_recognition
|
| 7 |
|
| 8 |
learn_inf = load_learner("export.pkl")
|
| 9 |
processor = AutoImageProcessor.from_pretrained("dima806/facial_emotions_image_detection")
|
| 10 |
model = AutoModelForImageClassification.from_pretrained("dima806/facial_emotions_image_detection")
|
| 11 |
|
| 12 |
+
def extract_face(image)-> Image.Image:
|
| 13 |
+
# Detect face locations
|
| 14 |
+
face_locations = face_recognition.face_locations(image)
|
| 15 |
+
|
| 16 |
+
# If a face is detected, extract the first one
|
| 17 |
+
if face_locations:
|
| 18 |
+
top, right, bottom, left = face_locations[0]
|
| 19 |
+
face_image = Image.fromarray(image[top:bottom, left:right])
|
| 20 |
+
return face_image
|
| 21 |
+
else:
|
| 22 |
+
return image
|
| 23 |
+
|
| 24 |
def predict(value) -> str:
|
| 25 |
+
image = extract_face(Image.fromarray(value)).convert("L")
|
| 26 |
inputs = processor(images=image, return_tensors="pt")
|
| 27 |
outputs = model(**inputs)
|
| 28 |
logits = outputs.logits
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
fastai
|
| 2 |
-
transformers
|
|
|
|
|
|
| 1 |
fastai
|
| 2 |
+
transformers
|
| 3 |
+
face_recognition
|