HW2 / app.py
ppolly's picture
Update app.py
90c9264 verified
raw
history blame
1.46 kB
from fastai.vision.all import *
from io import BytesIO
import requests
import streamlit as st
"""
# 使用卷積神經網路分類海洋污染影像以支持水下生物保護
此模型使用 ResNet34 卷積神經網路來分類海洋污染影像,識別四種類別:塑料污染、油污染、金屬廢棄物和乾淨海洋。
模型支持聯合國可持續發展目標(SDG)中的“水下生物”目標,旨在識別和減少海洋污染,保護海洋生態系統。
請上傳塑料污染、油污染、金屬廢棄物或乾淨海洋的圖片
"""
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 + "demo_model.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)