christinapeng commited on
Commit
f4b990d
·
verified ·
1 Parent(s): 10f17d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -51
app.py CHANGED
@@ -1,66 +1,55 @@
1
- '''PyTorch Food Classifier - FastAI 2022- Mostly Good For Pastries
2
- and trained on ResNet 34'''
3
-
4
- import streamlit as st
5
- import os
6
- from PIL import Image
7
- import time
8
- from fastai.vision.all import *
9
- from fastai.learner import load_learner
10
-
11
- def is_penguin(img):
12
- return img.split('-')[0]
13
-
14
-
15
- #Load the Learner (Exported from ipnyb file with learn.export() )
16
- learn = load_learner('model.pkl')
17
-
18
-
19
- #Classify image
20
- def classify_image(cl_img):
21
- img = Image.open(cl_img)
22
- st.image(img)
23
- pred, _ , _ = learn.predict(img)
24
- return pred
25
 
 
 
 
 
 
 
26
 
 
 
27
 
28
-
29
- st.set_page_config(page_title="PyTorch Food Classifier - FastAI 2022", page_icon=":robot:")
30
- st.header("PyTorch Food Classifier")
31
 
32
- file_up = st.file_uploader("Upload Your Food Image Below", type=["jpg","png"])
33
 
34
- if st.button('Run Model'):
35
- st.write("Button Pressed")
36
- cl_done = classify_image(file_up)
 
 
 
37
 
38
- st.write(f"Your food is: {cl_done}")
39
-
40
- st.write('This classifier is trained on Resnet-34 and works primarily for three classes. Donuts, CheeseCake and Panna Cotta (HOTDOGNOTHOTDOG).\n\n Thankyou to FastAI for the exercise.')
41
-
42
 
 
43
 
 
 
 
 
44
 
 
45
 
46
- # from fastai.vision.all import *
47
- # from fastai.learner import load_learner
48
- # import gradio as gr
49
-
50
- # import sys
51
- # print(f"Python version: {sys.version}")
52
 
53
- # learn = load_learner('model.pkl')
54
 
55
- # categories = ('Penguin', 'Puffin')
56
 
57
- # def classifyImage(img):
58
- # pred, idx, prob = learn.predict(img)
59
- # return dict(zip(categories, map(float, prob)))
60
 
61
- # image = gr.Image(shape=(192, 192))
62
- # label = gr.Label()
63
- # examples = ['penguin.jpg', 'puffin.png', 'razorbill.jpg']
64
 
65
- # intf = gr.Interface(fn=classifyImage, inputs=image, outputs=label, examples=examples)
66
- # intf.launch(inline=False)
 
1
+ # '''PyTorch Food Classifier - FastAI 2022- Mostly Good For Pastries
2
+ # and trained on ResNet 34'''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # import streamlit as st
5
+ # import os
6
+ # from PIL import Image
7
+ # import time
8
+ # from fastai.vision.all import *
9
+ # from fastai.learner import load_learner
10
 
11
+ # def is_penguin(img):
12
+ # return img.split('-')[0]
13
 
14
+ # #Load the Learner (Exported from ipnyb file with learn.export() )
15
+ # learn = load_learner('model.pkl')
 
16
 
17
+ # categories = ('Penguin', 'Puffin')
18
 
19
+ # #Classify image
20
+ # def classify_image(cl_img):
21
+ # img = Image.open(cl_img)
22
+ # st.image(img)
23
+ # pred, _ , prob = learn.predict(img)
24
+ # return dict(zip(categories, map(float, prob)))
25
 
26
+ # st.set_page_config(page_title="Penguin vs Puffin Classifier - FastAI 2025", page_icon=":robot:")
27
+ # st.header("Penguin vs Puffin Classifier")
 
 
28
 
29
+ # file_up = st.file_uploader("Upload Your Image Below", type=["jpg","png"])
30
 
31
+ # if st.button('Run Model'):
32
+ # st.write("Button Pressed")
33
+ # cl_done = classify_image(file_up)
34
+ # st.write(f"Your food is: {cl_done}")
35
 
36
+ # st.write('This classifier is trained on Resnet-34 and specializes in differentiating penguins from puffins).')
37
 
38
+ from fastai.vision.all import *
39
+ from fastai.learner import load_learner
40
+ import gradio as gr
 
 
 
41
 
42
+ learn = load_learner('model.pkl')
43
 
44
+ categories = ('Penguin', 'Puffin')
45
 
46
+ def classifyImage(img):
47
+ pred, idx, prob = learn.predict(img)
48
+ return dict(zip(categories, map(float, prob)))
49
 
50
+ image = gr.Image(shape=(192, 192))
51
+ label = gr.Label()
52
+ examples = ['penguin.jpg', 'puffin.png', 'razorbill.jpg']
53
 
54
+ intf = gr.Interface(fn=classifyImage, inputs=image, outputs=label, examples=examples)
55
+ intf.launch(inline=False)