recyclehw2 / app.py
lnky's picture
Update app.py
b9d24a9 verified
raw
history blame contribute delete
No virus
1.36 kB
from fastai.vision.all import *
from io import BytesIO
import requests
import streamlit as st
"""
# ๅ›žๆ”ถๅˆ†้กž
ๅˆ†้กžๅ›žๆ”ถ็จฎ้กž:ๅปš้ค˜(food waste,fruit peels,vegetable scraps,eggshells,coffee grounds)ใ€็Žป็’ƒ(glass food jars,glass cosmetic containers jars)ใ€้‡‘ๅฑฌ(aluminum cans,steel food cans,aerosol cans)ใ€
็ด™้กž(Paper and Cardboard)ใ€ๅก‘่† (Plastic bag,plastic food container,plastic disposable cutlery,plastic straw,plastic cup lids)
"""
def predict(img):
st.image(img, caption="Your image", use_column_width=True)
pred, key, probs = learn_inf.predict(img)
# st.write(learn_inf.predict(img))
f"""
### Rediction result: {pred}
### Probability of {pred}: {probs[key].item()*100: .2f}%
"""
path = "./"
learn_inf = load_learner(path + "resnet34_hw2-2.pkl")
option = st.radio("", ["Upload Image", "Image URL"])
if option == "Upload Image":
uploaded_file = st.file_uploader("Please upload an image.")
if uploaded_file is not None:
img = PILImage.create(uploaded_file)
predict(img)
else:
url = st.text_input("Please input a url.")
if url != "":
try:
response = requests.get(url)
pil_img = PILImage.create(BytesIO(response.content))
predict(pil_img)
except:
st.text("Problem reading image from", url)