Spaces:
Runtime error
Runtime error
Commit
·
12b7c16
1
Parent(s):
18d8f91
Upload 11 files
Browse files- .gitattributes +3 -0
- Procfile +1 -0
- app.py +210 -0
- data/sample_images/1.jpg +0 -0
- data/sample_images/2.jpg +3 -0
- data/sample_videos/sample.mp4 +0 -0
- data/uploaded_data/upload.avi +3 -0
- data/uploaded_data/upload.jpg +0 -0
- data/uploaded_data/upload.mp4 +3 -0
- models/yolov5s.pt +3 -0
- requirements.txt +19 -0
- setup.sh +11 -0
.gitattributes
CHANGED
@@ -32,3 +32,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
data/sample_images/2.jpg filter=lfs diff=lfs merge=lfs -text
|
36 |
+
data/uploaded_data/upload.avi filter=lfs diff=lfs merge=lfs -text
|
37 |
+
data/uploaded_data/upload.mp4 filter=lfs diff=lfs merge=lfs -text
|
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: sh setup.sh && streamlit run app.py
|
app.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import glob
|
2 |
+
import streamlit as st
|
3 |
+
import wget
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
import cv2
|
7 |
+
import os
|
8 |
+
import time
|
9 |
+
|
10 |
+
st.set_page_config(layout="wide")
|
11 |
+
|
12 |
+
cfg_model_path = 'models/yolov5s.pt'
|
13 |
+
model = None
|
14 |
+
confidence = .25
|
15 |
+
|
16 |
+
|
17 |
+
def image_input(data_src):
|
18 |
+
img_file = None
|
19 |
+
if data_src == 'Sample data':
|
20 |
+
# get all sample images
|
21 |
+
img_path = glob.glob('data/sample_images/*')
|
22 |
+
img_slider = st.slider("Select a test image.", min_value=1, max_value=len(img_path), step=1)
|
23 |
+
img_file = img_path[img_slider - 1]
|
24 |
+
else:
|
25 |
+
img_bytes = st.sidebar.file_uploader("Upload an image", type=['png', 'jpeg', 'jpg'])
|
26 |
+
if img_bytes:
|
27 |
+
img_file = "data/uploaded_data/upload." + img_bytes.name.split('.')[-1]
|
28 |
+
Image.open(img_bytes).save(img_file)
|
29 |
+
|
30 |
+
if img_file:
|
31 |
+
col1, col2 = st.columns(2)
|
32 |
+
with col1:
|
33 |
+
st.image(img_file, caption="Selected Image")
|
34 |
+
with col2:
|
35 |
+
img = infer_image(img_file)
|
36 |
+
st.image(img, caption="Model prediction")
|
37 |
+
|
38 |
+
|
39 |
+
def video_input(data_src):
|
40 |
+
vid_file = None
|
41 |
+
if data_src == 'Sample data':
|
42 |
+
vid_file = "data/sample_videos/sample.mp4"
|
43 |
+
else:
|
44 |
+
vid_bytes = st.sidebar.file_uploader("Upload a video", type=['mp4', 'mpv', 'avi'])
|
45 |
+
if vid_bytes:
|
46 |
+
vid_file = "data/uploaded_data/upload." + vid_bytes.name.split('.')[-1]
|
47 |
+
with open(vid_file, 'wb') as out:
|
48 |
+
out.write(vid_bytes.read())
|
49 |
+
|
50 |
+
if vid_file:
|
51 |
+
cap = cv2.VideoCapture(vid_file)
|
52 |
+
custom_size = st.sidebar.checkbox("Custom frame size")
|
53 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
54 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
55 |
+
if custom_size:
|
56 |
+
width = st.sidebar.number_input("Width", min_value=120, step=20, value=width)
|
57 |
+
height = st.sidebar.number_input("Height", min_value=120, step=20, value=height)
|
58 |
+
|
59 |
+
fps = 0
|
60 |
+
st1, st2, st3 = st.columns(3)
|
61 |
+
with st1:
|
62 |
+
st.markdown("## Height")
|
63 |
+
st1_text = st.markdown(f"{height}")
|
64 |
+
with st2:
|
65 |
+
st.markdown("## Width")
|
66 |
+
st2_text = st.markdown(f"{width}")
|
67 |
+
with st3:
|
68 |
+
st.markdown("## FPS")
|
69 |
+
st3_text = st.markdown(f"{fps}")
|
70 |
+
|
71 |
+
st.markdown("---")
|
72 |
+
output = st.empty()
|
73 |
+
prev_time = 0
|
74 |
+
curr_time = 0
|
75 |
+
while True:
|
76 |
+
ret, frame = cap.read()
|
77 |
+
if not ret:
|
78 |
+
st.write("Can't read frame, stream ended? Exiting ....")
|
79 |
+
break
|
80 |
+
frame = cv2.resize(frame, (width, height))
|
81 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
82 |
+
output_img = infer_image(frame)
|
83 |
+
output.image(output_img)
|
84 |
+
curr_time = time.time()
|
85 |
+
fps = 1 / (curr_time - prev_time)
|
86 |
+
prev_time = curr_time
|
87 |
+
st1_text.markdown(f"**{height}**")
|
88 |
+
st2_text.markdown(f"**{width}**")
|
89 |
+
st3_text.markdown(f"**{fps:.2f}**")
|
90 |
+
|
91 |
+
cap.release()
|
92 |
+
|
93 |
+
from roboflow import Roboflow
|
94 |
+
rf = Roboflow(api_key="BSImkKkNh25lMbRDYAAC")
|
95 |
+
project = rf.workspace().project("ambulances-model")
|
96 |
+
modelA = project.version(5).model
|
97 |
+
def infer_image(img, size=None):
|
98 |
+
model.conf = confidence
|
99 |
+
result = model(img, size=size) if size else model(img)
|
100 |
+
abc=result.pandas().xyxy[0]
|
101 |
+
tdd=abc['name'].value_counts()
|
102 |
+
annos=tdd.to_string()
|
103 |
+
#print(str(result)[20:69])
|
104 |
+
result.render()
|
105 |
+
image = Image.fromarray(result.ims[0])
|
106 |
+
image.save("abc.jpg")
|
107 |
+
modelA.predict("abc.jpg", confidence=40, overlap=30).save("abc.jpg")
|
108 |
+
image=cv2.cvtColor(cv2.imread("abc.jpg"), cv2.COLOR_BGR2RGB)
|
109 |
+
y0, dy = 50, 50
|
110 |
+
for i, line in enumerate(annos.split('\n')):
|
111 |
+
y = y0 + i*dy
|
112 |
+
image=cv2.putText(image, line, (10, y ), cv2.FONT_HERSHEY_SIMPLEX,2,(255,255,0),10)
|
113 |
+
return image
|
114 |
+
|
115 |
+
|
116 |
+
@st.experimental_singleton
|
117 |
+
def load_model(path, device):
|
118 |
+
model_ = torch.hub.load('ultralytics/yolov5', 'custom', path=path, force_reload=True)
|
119 |
+
model_.to(device)
|
120 |
+
print("model to ", device)
|
121 |
+
return model_
|
122 |
+
|
123 |
+
|
124 |
+
@st.experimental_singleton
|
125 |
+
def download_model(url):
|
126 |
+
model_file = wget.download(url, out="models")
|
127 |
+
return model_file
|
128 |
+
|
129 |
+
|
130 |
+
def get_user_model():
|
131 |
+
model_src = st.sidebar.radio("Model source", ["file upload", "url"])
|
132 |
+
model_file = None
|
133 |
+
if model_src == "file upload":
|
134 |
+
model_bytes = st.sidebar.file_uploader("Upload a model file", type=['pt'])
|
135 |
+
if model_bytes:
|
136 |
+
model_file = "models/uploaded_" + model_bytes.name
|
137 |
+
with open(model_file, 'wb') as out:
|
138 |
+
out.write(model_bytes.read())
|
139 |
+
else:
|
140 |
+
url = st.sidebar.text_input("model url")
|
141 |
+
if url:
|
142 |
+
model_file_ = download_model(url)
|
143 |
+
if model_file_.split(".")[-1] == "pt":
|
144 |
+
model_file = model_file_
|
145 |
+
|
146 |
+
return model_file
|
147 |
+
|
148 |
+
def main():
|
149 |
+
# global variables
|
150 |
+
global model, confidence, cfg_model_path
|
151 |
+
|
152 |
+
st.title("Traffic Management Yolo")
|
153 |
+
|
154 |
+
st.sidebar.title("Options")
|
155 |
+
|
156 |
+
# upload model
|
157 |
+
model_src = st.sidebar.radio("Select yolov5 file", ["Use Yolo"])
|
158 |
+
# URL, upload file (max 200 mb)
|
159 |
+
if model_src == "Use your own model":
|
160 |
+
user_model_path = get_user_model()
|
161 |
+
if user_model_path:
|
162 |
+
cfg_model_path = user_model_path
|
163 |
+
|
164 |
+
st.sidebar.text(cfg_model_path.split("/")[-1])
|
165 |
+
st.sidebar.markdown("---")
|
166 |
+
|
167 |
+
# check if model file is available
|
168 |
+
if not os.path.isfile(cfg_model_path):
|
169 |
+
st.warning("Model not Found", icon="⚠️")
|
170 |
+
else:
|
171 |
+
# device options
|
172 |
+
if torch.cuda.is_available():
|
173 |
+
device_option = st.sidebar.radio("PC type", ['cpu', 'cuda'], disabled=False, index=0)
|
174 |
+
else:
|
175 |
+
device_option = st.sidebar.radio("PC type", ['cpu', 'cuda'], disabled=True, index=0)
|
176 |
+
|
177 |
+
# load model
|
178 |
+
model = load_model(cfg_model_path, device_option)
|
179 |
+
|
180 |
+
# confidence slider
|
181 |
+
confidence = st.sidebar.slider('Confidence', min_value=0.1, max_value=1.0, value=.45)
|
182 |
+
|
183 |
+
# custom classes
|
184 |
+
if st.sidebar.checkbox("Select Classes"):
|
185 |
+
model_names = list(model.names.values())
|
186 |
+
assigned_class = st.sidebar.multiselect("Select Classes", model_names, default=[model_names[0]])
|
187 |
+
classes = [model_names.index(name) for name in assigned_class]
|
188 |
+
model.classes = classes
|
189 |
+
else:
|
190 |
+
model.classes = list(model.names.keys())
|
191 |
+
|
192 |
+
st.sidebar.markdown("---")
|
193 |
+
|
194 |
+
# input options
|
195 |
+
input_option = st.sidebar.radio("Select type: ", ['image', 'video'])
|
196 |
+
|
197 |
+
# input src option
|
198 |
+
data_src = st.sidebar.radio("Select input source: ", ['Sample data', 'Upload your own data'])
|
199 |
+
|
200 |
+
if input_option == 'image':
|
201 |
+
image_input(data_src)
|
202 |
+
else:
|
203 |
+
video_input(data_src)
|
204 |
+
|
205 |
+
|
206 |
+
if __name__ == "__main__":
|
207 |
+
try:
|
208 |
+
main()
|
209 |
+
except SystemExit:
|
210 |
+
pass
|
data/sample_images/1.jpg
ADDED
![]() |
data/sample_images/2.jpg
ADDED
![]() |
Git LFS Details
|
data/sample_videos/sample.mp4
ADDED
Binary file (940 kB). View file
|
|
data/uploaded_data/upload.avi
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a7c7d98162e1ca5a971d29916b47cd15120cfc60f5bc435fb81d8807e254bade
|
3 |
+
size 2528436
|
data/uploaded_data/upload.jpg
ADDED
![]() |
data/uploaded_data/upload.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b41d9fba9daea828e3281bb5a846b9222b6b75341ed6332b6a7d20256b2fd37c
|
3 |
+
size 2505855
|
models/yolov5s.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b3b748c1e592ddd8868022e8732fde20025197328490623cc16c6f24d0782ee
|
3 |
+
size 14808437
|
requirements.txt
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
ultralytics
|
3 |
+
matplotlib>=3.2.2
|
4 |
+
numpy>=1.18.5
|
5 |
+
opencv-python-headless
|
6 |
+
Pillow>=7.1.2
|
7 |
+
PyYAML>=5.3.1
|
8 |
+
requests>=2.23.0
|
9 |
+
scipy>=1.4.1
|
10 |
+
torch>=1.7.0
|
11 |
+
torchvision>=0.8.1
|
12 |
+
tqdm>=4.41.0
|
13 |
+
protobuf<4.21.5
|
14 |
+
pandas>=1.1.4
|
15 |
+
seaborn>=0.11.0
|
16 |
+
ipython
|
17 |
+
psutil
|
18 |
+
thop
|
19 |
+
wget
|
setup.sh
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mkdir -p ~/.streamlit/
|
2 |
+
echo "\
|
3 |
+
[general]\n\
|
4 |
+
email = \"[email protected]\"\n\
|
5 |
+
" > ~/.streamlit/credentials.toml
|
6 |
+
echo "\
|
7 |
+
[server]\n\
|
8 |
+
headless = true\n\
|
9 |
+
enableCORS=false\n\
|
10 |
+
port = $PORT\n\
|
11 |
+
" > ~/.streamlit/config.toml
|