tmfu685717/Python_AI
0
1from fastai.vision.all import *2from io import BytesIO3import requests4import streamlit as st5 6"""7# Animal Classification8這是一個照片分類器,可以區分波斯貓、柴犬、倉鼠、兔子 等9"""10 11def predict(img):12 st.image(img, caption="Your image", use_column_width=True)13 pred, key, probs = learn_inf.predict(img)14 # st.write(learn_inf.predict(img))15 16 f"""17 ### Rediction result: {pred}18 ### Probability of {pred}: {probs[key].item()*100: .2f}%19 """20 21 22path = "./"23learn_inf = load_learner(path + "Animal_stage-2.pkl")24 25option = st.radio("", ["Upload Image", "Image URL"])26 27if option == "Upload Image":28 uploaded_file = st.file_uploader("請上傳照片.")29 30 if uploaded_file is not None:31 img = PILImage.create(uploaded_file)32 predict(img)33 34else:35 url = st.text_input("請輸入超連結.")36 37 if url != "":38 try:39 response = requests.get(url)40 pil_img = PILImage.create(BytesIO(response.content))41 predict(pil_img)42 43 except:44 st.text("Problem reading image from", url)45 