Spaces:
Sleeping
Sleeping
import gradio as gr | |
from fastai.vision.all import * | |
import skimage | |
from facenet_pytorch import MTCNN | |
import torch | |
import pandas as pd | |
from PIL import Image, ImageDraw, ImageFont | |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') | |
print('Running on device: {}'.format(device)) | |
mtcnn = MTCNN(margin=40, keep_all=True, post_process=False, device=device) | |
learn = load_learner('export.pkl') | |
labels = learn.dls.vocab | |
emotions = {"x": [], "y": [], "State": []} | |
def predict(img): | |
img = PILImage.create(img) | |
boxes, _ = mtcnn.detect(img) | |
o_img = img.copy() | |
draw = ImageDraw.Draw(o_img) | |
for box in boxes: | |
coords = tuple(box.tolist()) | |
pred,pred_idx,probs = learn.predict(img.crop(coords)) | |
draw.rectangle(coords, outline=(0, 0, 0), width=1) | |
draw.text((coords[0]-10, coords[1]-10), pred, font=ImageFont.truetype("arial")) | |
return o_img | |
''' | |
emotions["x"].append((coords[0] + coords[2])/2) | |
emotions["y"].append((coords[1] + coords[3])/2) | |
emotions["State"].append(pred) | |
emotions_df = pd.DataFrame(emotions) | |
return gr.ScatterPlot.update( | |
value=emotions_df, | |
x="x", | |
y="y", | |
color="State", | |
title="Class Heat Map", | |
color_legend_title="State of the student", | |
caption="Class Monitor", | |
) | |
''' | |
title = "Students emotion classifer" | |
description = "A students emotion classifer trained with fastai. Created as a demo for Gradio and HuggingFace Spaces." | |
interpretation='default' | |
enable_queue=True | |
#gr.Interface(fn=predict,inputs=gr.Image(source="webcam",shape=(512, 512)),outputs=gr.outputs.Label(num_top_classes=3),title=title,description=description,interpretation=interpretation,enable_queue=enable_queue).launch() | |
#gr.Interface(fn=predict,inputs=gr.Image(source="webcam",shape=(512, 512)),outputs=gr.ScatterPlot(),title=title,description=description,interpretation=interpretation,enable_queue=enable_queue, share=True).launch() | |
gr.Interface(fn=predict,inputs=gr.Image(),outputs=gr.Image(),title=title,description=description,interpretation=interpretation,enable_queue=enable_queue, share=True).launch() | |