File size: 971 Bytes
a1d3bc3
 
a717b41
23b7e7e
a1d3bc3
 
23b7e7e
a1d3bc3
 
23b7e7e
 
 
 
 
 
 
 
 
 
 
 
 
a1d3bc3
7504c67
9bc7a40
a1d3bc3
 
 
23b7e7e
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
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)