JASON123454321 commited on
Commit
7d0e758
·
verified ·
1 Parent(s): be310bf

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +154 -34
src/streamlit_app.py CHANGED
@@ -1,40 +1,160 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
5
 
6
- """
7
- # Welcome to Streamlit!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
 
 
 
12
 
13
- In the meantime, below is an example of what you can do with just a few lines of code:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  """
 
 
 
 
15
 
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
1
+ from fastai.vision.all import *
2
+ from io import BytesIO
3
+ import requests
4
  import streamlit as st
5
 
6
+ import numpy as np
7
+ import torch
8
+ import time
9
+ import cv2
10
+ from numpy import random
11
+ import os
12
+ import sys
13
+
14
+ # 加入上層目錄到模組搜尋路徑中
15
+ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
16
+
17
+ from models.experimental import attempt_load
18
+ from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
19
+ scale_coords, xyxy2xywh, strip_optimizer, set_logging, increment_path
20
+ from utils.plots import plot_one_box
21
+
22
+ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
23
+ # Resize and pad image while meeting stride-multiple constraints
24
+ shape = img.shape[:2] # current shape [height, width]
25
+ if isinstance(new_shape, int):
26
+ new_shape = (new_shape, new_shape)
27
+
28
+ # Scale ratio (new / old)
29
+ r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
30
+ if not scaleup: # only scale down, do not scale up (for better test mAP)
31
+ r = min(r, 1.0)
32
+
33
+ # Compute padding
34
+ ratio = r, r # width, height ratios
35
+ new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
36
+ dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
37
+ if auto: # minimum rectangle
38
+ dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
39
+ elif scaleFill: # stretch
40
+ dw, dh = 0.0, 0.0
41
+ new_unpad = (new_shape[1], new_shape[0])
42
+ ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
43
+
44
+ dw /= 2 # divide padding into 2 sides
45
+ dh /= 2
46
+
47
+ if shape[::-1] != new_unpad: # resize
48
+ img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
49
+ top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
50
+ left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
51
+ img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
52
+ return img, ratio, (dw, dh)
53
+
54
+ def detect_modify(img0, model, conf=0.4, imgsz=640, conf_thres = 0.25, iou_thres=0.45):
55
+ st.image(img0, caption="Your image", use_column_width=True)
56
+
57
+ stride = int(model.stride.max()) # model stride
58
+ imgsz = check_img_size(imgsz, s=stride) # check img_size
59
+
60
+ # Padded resize
61
+ img0 = cv2.cvtColor(np.asarray(img0), cv2.COLOR_RGB2BGR)
62
+ img = letterbox(img0, imgsz, stride=stride)[0]
63
+ # Convert
64
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
65
+ img = np.ascontiguousarray(img)
66
+
67
+
68
+ # Get names and colors
69
+ names = model.module.names if hasattr(model, 'module') else model.names
70
+ colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
71
+
72
+ # Run inference
73
+ old_img_w = old_img_h = imgsz
74
+ old_img_b = 1
75
 
76
+ t0 = time.time()
77
+ img = torch.from_numpy(img).to(device)
78
+ # img /= 255.0 # 0 - 255 to 0.0 - 1.0
79
+ img = img/255.0
80
+ if img.ndimension() == 3:
81
+ img = img.unsqueeze(0)
82
 
83
+ # Inference
84
+ # t1 = time_synchronized()
85
+ with torch.no_grad(): # Calculating gradients would cause a GPU memory leak
86
+ pred = model(img)[0]
87
+ # t2 = time_synchronized()
88
+
89
+ # Apply NMS
90
+ pred = non_max_suppression(pred, conf_thres, iou_thres)
91
+ # t3 = time_synchronized()
92
+
93
+ # Process detections
94
+ # for i, det in enumerate(pred): # detections per image
95
+
96
+ gn = torch.tensor(img0.shape)[[1, 0, 1, 0]] # normalization gain whwh
97
+
98
+ det = pred[0]
99
+ if len(det):
100
+ # Rescale boxes from img_size to im0 size
101
+ det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
102
+
103
+ # Print results
104
+ s = ''
105
+ for c in det[:, -1].unique():
106
+ n = (det[:, -1] == c).sum() # detections per class
107
+ s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
108
+
109
+ # Write results
110
+ for *xyxy, conf, cls in reversed(det):
111
+ label = f'{names[int(cls)]} {conf:.2f}'
112
+ plot_one_box(xyxy, img0, label=label, color=colors[int(cls)], line_thickness=1)
113
+
114
+ f"""
115
+ ### Prediction result:
116
+ """
117
+ img0 = cv2.cvtColor(np.asarray(img0), cv2.COLOR_BGR2RGB)
118
+ st.image(img0, caption="Prediction Result", use_column_width=True)
119
+
120
+ #set paramters
121
+
122
+ # 取得目前檔案 (streamlit_app.py) 所在的目錄
123
+ current_dir = os.path.dirname(os.path.abspath(__file__))
124
+
125
+ # 回到根目錄後組合出 .pkl 檔案的路徑
126
+ weight_path = os.path.join(current_dir, 'best.pt')
127
+
128
+ imgsz = 640
129
+ conf = 0.4
130
+ conf_thres = 0.25
131
+ iou_thres=0.45
132
+ device = torch.device("cpu")
133
+ path = "./"
134
+
135
+ # Load model
136
+ #model = attempt_load(weight_path, map_location=torch.device('cpu')) # load FP32 model
137
+ ckpt = torch.load(weight_path, map_location=torch.device('cpu'), weights_only=False)
138
+ model = ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()
139
+
140
+ """
141
+ # YOLOv7
142
+ This is a object detection model for [Objects].
143
  """
144
+ option = st.radio("", ["Upload Image", "Image URL"])
145
+
146
+ if option == "Upload Image":
147
+ uploaded_file = st.file_uploader("Please upload an image.")
148
 
149
+ if uploaded_file is not None:
150
+ img = PILImage.create(uploaded_file)
151
+ detect_modify(img, model, conf=conf, imgsz=imgsz, conf_thres=conf_thres, iou_thres=iou_thres)
152
+ else:
153
+ url = st.text_input("Please input a url.")
154
+ if url != "":
155
+ try:
156
+ response = requests.get(url)
157
+ pil_img = PILImage.create(BytesIO(response.content))
158
+ detect_modify(pil_img, model, conf=conf, imgsz=imgsz, conf_thres=conf_thres, iou_thres=iou_thres)
159
+ except:
160
+ st.text("Problem reading image from", url)