File size: 1,533 Bytes
16cf1ad
af3e0f6
4fb5c0d
2a29d13
4fb5c0d
851ec4b
2a29d13
e34b118
4fb5c0d
 
af3e0f6
851ec4b
 
 
 
 
 
 
 
 
 
 
 
b34bbc3
851ec4b
4fb5c0d
 
 
 
 
16cf1ad
39d5445
 
 
 
 
e10e9b7
25e8fcd
16cf1ad
39d5445
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr
from fastai.vision.all import *
from transformers import AutoImageProcessor, AutoModelForImageClassification
from PIL import Image
import requests
import face_recognition

learn_inf = load_learner("export.pkl")
processor = AutoImageProcessor.from_pretrained("dima806/facial_emotions_image_detection")
model = AutoModelForImageClassification.from_pretrained("dima806/facial_emotions_image_detection")

def extract_face(image)-> Image.Image:
    # Detect face locations
    face_locations = face_recognition.face_locations(image)

    # If a face is detected, extract the first one
    if face_locations:
        top, right, bottom, left = face_locations[0]
        face_image = Image.fromarray(image[top:bottom, left:right])
        return face_image
    else:
        return image

def predict(value) -> str:
    image = extract_face(Image.fromarray(value)).convert("L")
    inputs = processor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    logits = outputs.logits
    predicted_class_idx = logits.argmax(-1).item()
    return model.config.id2label[predicted_class_idx]

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            input_img = gr.Image(label="Input", sources="webcam")
        with gr.Column():
            output_lbl = gr.Label(value="Output", label="Expression Prediction")
        input_img.stream(fn=predict, inputs=input_img, outputs=output_lbl, time_limit=15, stream_every=0.1, concurrency_limit=30)

if __name__ == "__main__":

    demo.launch()