segadeds's picture
Update app.py
23b7e7e verified
raw
history blame
971 Bytes
from fastai.vision.all import *
import gradio as gr
import numpy as np
from PIL import Image as PILImage
learn = load_learner('model.pkl')
categories = ['calling', 'clapping', 'cycling', 'dancing', 'drinking', 'eating', 'fighting', 'hugging', 'laughing', 'listening_to_music', 'running', 'sitting', 'sleeping', 'texting', 'using_laptop']
def classify_image(img):
# Convert to RGB if the image is in RGBA mode
if img.mode == 'RGBA':
img = img.convert('RGB')
# Resize the image
img = img.resize((192, 192))
# Convert to fastai PILImage
fastai_img = PILImage.create(np.array(img))
# Make prediction
pred, idx, probs = learn.predict(fastai_img)
return dict(zip(categories, map(float, probs)))
image = gr.Image(type='pil')
label = gr.Label()
examples = ['laughing.jpg', 'dancing.jpg', 'drinking.jpg']
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(inline=False)