Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,29 @@
|
|
1 |
from fastai.vision.all import *
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
-
|
5 |
|
6 |
learn = load_learner('model.pkl')
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
categories = (['calling', 'clapping', 'cycling', 'dancing', 'drinking', 'eating', 'fighting', 'hugging',
|
11 |
-
'laughing', 'listening_to_music', 'running', 'sitting', 'sleeping', 'texting', 'using_laptop'])
|
12 |
-
|
13 |
-
|
14 |
|
15 |
def classify_image(img):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
image = gr.Image(type='pil')
|
24 |
label = gr.Label()
|
25 |
examples = ['laughing.jpg', 'dancing.jpg', 'drinking.jpg']
|
26 |
|
27 |
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
|
28 |
-
intf.launch(inline=False)
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
1 |
from fastai.vision.all import *
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
+
from PIL import Image as PILImage
|
5 |
|
6 |
learn = load_learner('model.pkl')
|
7 |
+
categories = ['calling', 'clapping', 'cycling', 'dancing', 'drinking', 'eating', 'fighting', 'hugging', 'laughing', 'listening_to_music', 'running', 'sitting', 'sleeping', 'texting', 'using_laptop']
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def classify_image(img):
|
10 |
+
# Convert to RGB if the image is in RGBA mode
|
11 |
+
if img.mode == 'RGBA':
|
12 |
+
img = img.convert('RGB')
|
13 |
+
|
14 |
+
# Resize the image
|
15 |
+
img = img.resize((192, 192))
|
16 |
+
|
17 |
+
# Convert to fastai PILImage
|
18 |
+
fastai_img = PILImage.create(np.array(img))
|
19 |
+
|
20 |
+
# Make prediction
|
21 |
+
pred, idx, probs = learn.predict(fastai_img)
|
22 |
+
return dict(zip(categories, map(float, probs)))
|
23 |
|
24 |
image = gr.Image(type='pil')
|
25 |
label = gr.Label()
|
26 |
examples = ['laughing.jpg', 'dancing.jpg', 'drinking.jpg']
|
27 |
|
28 |
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
|
29 |
+
intf.launch(inline=False)
|
|
|
|
|
|
|
|