| import gradio as gr |
| from fastai.vision.all import * |
| from transformers import AutoImageProcessor, AutoModelForImageClassification |
| from PIL import Image |
| import requests |
|
|
| 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 predict(value) -> str: |
| image = Image.fromarray(value).convert("L").convert("RGB") |
| 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,concurrency_limit=20,time_limit=20,stream_every=0.1) |
|
|
| if __name__ == "__main__": |
|
|
| demo.launch() |