Gaeulove924 commited on
Commit
cbc3527
1 Parent(s): bcb9b6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -3,6 +3,7 @@ from sklearn.metrics import precision_score, recall_score, f1_score
3
  from io import BytesIO
4
  import requests
5
  import streamlit as st
 
6
 
7
  # 定義自定義度量函數
8
  def precision(inp, targ, average='macro'):
@@ -17,6 +18,7 @@ def f1(inp, targ, average='macro'):
17
  inp, targ = inp.cpu(), targ.cpu() # 確保張量在 CPU 上
18
  return f1_score(targ, inp.argmax(dim=-1), average=average)
19
 
 
20
  """
21
  # 層積岩的種類分類
22
  此模型為對層積岩進行分類,類別有:碎屑岩(Clastic Rock)、火山碎屑岩(Pyroclastic Rock)、生物岩(Biolite)和化學岩(Chemical Sedimentary Rock)
@@ -24,22 +26,31 @@ def f1(inp, targ, average='macro'):
24
  而在上傳圖片之後,模型將會為您分類出適合的類別與其對應的機率
25
  """
26
 
 
 
 
 
 
 
 
 
 
27
  def predict(img):
28
  st.image(img, caption="Your image", use_column_width=True)
29
  pred, key, probs = learn_inf.predict(img)
30
- # st.write(learn_inf.predict(img))
31
-
32
- f"""
33
  ### 預測的類別為: {pred}
34
- ### 符合程度: {probs[key].item()*100: .3f}%
35
- """
36
-
37
 
 
38
  path = "./"
39
  learn_inf = load_learner(path + "rockdetect.pkl")
40
 
 
41
  option = st.radio("", ["Upload Image", "Image URL"])
42
 
 
43
  if option == "Upload Image":
44
  uploaded_file = st.file_uploader("Please upload an image.")
45
 
@@ -47,15 +58,15 @@ if option == "Upload Image":
47
  img = PILImage.create(uploaded_file)
48
  predict(img)
49
 
 
50
  else:
51
  url = st.text_input("Please input a url.")
52
 
53
  if url != "":
54
  try:
 
55
  response = requests.get(url)
56
  pil_img = PILImage.create(BytesIO(response.content))
57
  predict(pil_img)
58
-
59
  except:
60
- st.text("Problem reading image from", url)
61
-
 
3
  from io import BytesIO
4
  import requests
5
  import streamlit as st
6
+ import os
7
 
8
  # 定義自定義度量函數
9
  def precision(inp, targ, average='macro'):
 
18
  inp, targ = inp.cpu(), targ.cpu() # 確保張量在 CPU 上
19
  return f1_score(targ, inp.argmax(dim=-1), average=average)
20
 
21
+ # 標題和描述
22
  """
23
  # 層積岩的種類分類
24
  此模型為對層積岩進行分類,類別有:碎屑岩(Clastic Rock)、火山碎屑岩(Pyroclastic Rock)、生物岩(Biolite)和化學岩(Chemical Sedimentary Rock)
 
26
  而在上傳圖片之後,模型將會為您分類出適合的類別與其對應的機率
27
  """
28
 
29
+ # 顯示示例圖片
30
+ st.subheader("示例圖片")
31
+ example_dir = "./example"
32
+ example_images = os.listdir(example_dir)
33
+
34
+ for image_name in example_images:
35
+ image_path = os.path.join(example_dir, image_name)
36
+ st.image(image_path, caption=image_name, use_column_width=True)
37
+
38
  def predict(img):
39
  st.image(img, caption="Your image", use_column_width=True)
40
  pred, key, probs = learn_inf.predict(img)
41
+ st.write(f"""
 
 
42
  ### 預測的類別為: {pred}
43
+ ### 符合程度: {probs[key].item()*100:.3f}%
44
+ """)
 
45
 
46
+ # 加載模型
47
  path = "./"
48
  learn_inf = load_learner(path + "rockdetect.pkl")
49
 
50
+ # 選擇上傳方式:本地上傳或URL
51
  option = st.radio("", ["Upload Image", "Image URL"])
52
 
53
+ # 處理本地圖片上傳
54
  if option == "Upload Image":
55
  uploaded_file = st.file_uploader("Please upload an image.")
56
 
 
58
  img = PILImage.create(uploaded_file)
59
  predict(img)
60
 
61
+ # 處理圖片URL
62
  else:
63
  url = st.text_input("Please input a url.")
64
 
65
  if url != "":
66
  try:
67
+ # 從URL下載圖片
68
  response = requests.get(url)
69
  pil_img = PILImage.create(BytesIO(response.content))
70
  predict(pil_img)
 
71
  except:
72
+ st.text("Problem reading image from", url)