from fastai.vision.all import * from io import BytesIO import requests import streamlit as st """ # 犬種分類器 將柴犬(Japan)、薩摩耶(Russia)、德國狼犬(Germany)、鬆獅犬(China)分類的模型。 """ 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""" ### 預測結果: {pred} ### 預測{pred}的可能性: {probs[key].item()*100: .2f}% """ path = "./" learn_inf = load_learner(path + "resnet34_stage-1.pkl") option = st.radio("", ["上傳檔案", "上傳圖片連結"]) if option == "上傳檔案": uploaded_file = st.file_uploader("請上傳圖片") if uploaded_file is not None: img = PILImage.create(uploaded_file) predict(img) else: url = st.text_input("請上傳連結") if url != "": try: response = requests.get(url) pil_img = PILImage.create(BytesIO(response.content)) predict(pil_img) except: st.text("連結出現問題", url)