Update app.py
Browse files
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 |
-
|
| 30 |
-
st.header("PyTorch Food Classifier")
|
| 31 |
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# import sys
|
| 51 |
-
# print(f"Python version: {sys.version}")
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 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)
|