from fastai.vision.all import * from io import BytesIO import requests import streamlit as st """ # 分辨海獅的兄弟姐妹 """ 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""" ## 牠 **{'是 ' if probs[key].item()*100 >= 95 else '不是'}** 海獅的兄弟姐妹. ### Rediction result: {pred} ### Probability of {pred}: {probs[key].item()*100: .2f}% """ path = "./" learn_inf = load_learner(path + "resnet34_stage-2.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("Problem reading image from", url)