Spaces:
Sleeping
Sleeping
File size: 1,410 Bytes
1f74b34 3a06270 1e7ab48 1f74b34 1e7ab48 1f74b34 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from fastai.vision.all import *
from io import BytesIO
import requests
import streamlit as st
# 应用标题
st.title("能源類型分類器")
st.write("這是一個可以分類不同能源形式(如風能、太陽能、水能)的分類器。")
# 展示示例图片
st.write("## 範例圖")
st.image("exwind.jpg", caption="Example EKG Image", use_column_width=True)
def predict(img):
st.image(img, caption="Your image", use_column_width=True)
pred, key, probs = learn_inf.predict(img)
result_message = f"""
### Prediction result: {pred}
### Probability of {pred}: {probs[key].item()*100: .2f}%
"""
st.write(result_message)
# 加载模型
path = "./"
learn_inf = load_learner(path + "resnet34_stage_4.pkl")
# 用户上传图片或提供图片 URL
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 for an image.")
if url != "":
try:
response = requests.get(url)
pil_img = PILImage.create(BytesIO(response.content))
predict(pil_img)
except Exception as e:
st.error(f"Problem reading image from the URL. Please check the URL and try again. Error: {str(e)}")
|