-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp2.py
281 lines (218 loc) · 10.4 KB
/
app2.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from io import StringIO
from pathlib import Path
import streamlit as st
import time
from detect import *
import os
import sys
import argparse
from PIL import Image
import cv2
import time
from PIL import Image, ImageFilter, ImageDraw
import numpy as np
import torch
import torchvision.models as models
import cityscapesscripts
from torchvision.models import ResNet
import json
import subprocess
#st.set_page_config(layout = "wide")
st.set_page_config(page_title = "PEDESTRIAN DETECTION - PSISAV", page_icon="👨🏼🦯")
#Load the Citypersons Yolov5 trained model
model1=torch.load('/Users/cs/Desktop/PSISAV/Dataset/psisav.pth')
hide_st_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_st_style, unsafe_allow_html=True)
st.markdown(
'''<style>
[data-testid="stSidebar"][aria-expanded="true"] > div:first-child {
width: 340px;
}
[data-testid="stSidebar"][aria-expanded="false"] > div:first-child {
width: 340px;
margin-left: -340px;
}
</style>
''',
unsafe_allow_html=True,
)
#################### Title #####################################################
st.markdown("<h3 style='text-align: center; color: red; font-family: font of choice, fallback font no1, sans-serif;'>PSISAV Pedestrian detection</h3>", unsafe_allow_html=True)
st.markdown("<h2 style='text-align: center; color: black; font-family: font of choice, fallback font no1, sans-serif;'>Developed by team PSISAV</h2>", unsafe_allow_html=True)
#st.markdown('---') # inserts underline
#st.markdown("<hr/>", unsafe_allow_html=True) # inserts underline
st.markdown('#') # inserts empty space
#--------------------------------------------------------------------------------
DEMO_VIDEO = os.path.join('data', 'videos', 'sampleVideo0.mp4')
DEMO_PIC = os.path.join('data', 'images', 'bus.jpg')
def get_subdirs(b='.'):
'''
Returns all sub-directories in a specific Path
'''
result = []
for d in os.listdir(b):
bd = os.path.join(b, d)
if os.path.isdir(bd):
result.append(bd)
return result
def get_detection_folder():
'''
Returns the latest folder in a runs\detect
'''
return max(get_subdirs(os.path.join('runs', 'detect')), key=os.path.getmtime)
#---------------------------Main Function for Execution--------------------------
def main():
source = ("Detect From Image", "Detect From Video")
source_index = st.sidebar.selectbox("Select Activity", range(
len(source)), format_func = lambda x: source[x])
cocoClassesLst = ["person"]
#cocoClassesLst = ["person","traffic light","stop sign"]
#classes_index = [0,9,11]
#classes_index=[0,1,2]
classes_index=[0]
isAllinList = 80 in classes_index
if isAllinList == True:
classes_index = classes_index.clear()
print("Selected Classes: ", classes_index)
#################### Parameters to setup ########################################
# MAX_BOXES_TO_DRAW = st.sidebar.number_input('Maximum Boxes To Draw', value = 5, min_value = 1, max_value = 5)
#deviceLst = ['cpu', '0', '1', '2', '3']
deviceLst = ['cpu']
DEVICES = st.sidebar.selectbox(" ", deviceLst, index = 0)
print("Devices: ", DEVICES)
#MIN_SCORE_THRES = st.sidebar.slider('Min Confidence Score Threshold', min_value = 0.0, max_value = 1.0, value = 0.4)
MIN_SCORE_THRES = st.sidebar.slider('Min Confidence Score Threshold', min_value = 0.0, max_value = 1.0, value = 0.2)
#################### Parameters to setup Streamlit and model.pth ########################################
weights = os.path.join("weights", "yolov5s.pt")
if source_index == 0:
uploaded_file = st.sidebar.file_uploader(
"Upload Image", type = ['png', 'jpeg', 'jpg'])
if uploaded_file is not None:
is_valid = True
with st.spinner(text = 'Resource Loading...'):
st.sidebar.text("Uploaded Pic")
st.sidebar.image(uploaded_file)
picture = Image.open(uploaded_file)
picture.save(os.path.join('data', 'images', uploaded_file.name))
data_source = os.path.join('data', 'images', uploaded_file.name)
elif uploaded_file is None:
is_valid = True
st.sidebar.text("DEMO Pic")
st.sidebar.image(DEMO_PIC)
data_source = DEMO_PIC
else:
is_valid = False
elif source_index == 1:
uploaded_file = st.sidebar.file_uploader("Upload Video", type = ['mp4'])
if uploaded_file is not None:
is_valid = True
with st.spinner(text = 'Resource Loading...'):
st.sidebar.text("Uploaded Video")
st.sidebar.video(uploaded_file)
with open(os.path.join("data", "videos", uploaded_file.name), "wb") as f:
f.write(uploaded_file.getbuffer())
data_source = os.path.join("data", "videos", uploaded_file.name)
elif uploaded_file is None:
is_valid = True
st.sidebar.text("DEMO Video")
st.sidebar.video(DEMO_VIDEO)
data_source = DEMO_VIDEO
else:
is_valid = False
else:
######### Select and capture Camera (Disabled) ################# Disabled In the code
selectedCam = st.sidebar.selectbox("Select Camera", ("Use WebCam", "Use Other Camera"), index = 0)
if selectedCam:
if selectedCam == "Use Other Camera":
data_source = int(1)
is_valid = True
else:
data_source = int(0)
is_valid = True
else:
is_valid = False
st.sidebar.markdown("<strong>Press 'q' multiple times on camera window and 'Ctrl + C' on CMD to clear camera window/exit</strong>", unsafe_allow_html=True)
if is_valid:
print('valid')
if st.button('Detect'):
if classes_index:
with st.spinner(text = 'Inferencing, Please Wait.....'):
run(weights = weights,
source = data_source,
#source = 0, #for webcam
conf_thres = MIN_SCORE_THRES,
#max_det = MAX_BOXES_TO_DRAW,
device = DEVICES,
save_txt = True,
save_conf = True,
classes = classes_index,
nosave = False,
)
else:
with st.spinner(text = 'Inferencing, Please Wait.....'):
run(weights = weights,
source = data_source,
#source = 0, #for webcam
conf_thres = MIN_SCORE_THRES,
#max_det = MAX_BOXES_TO_DRAW,
device = DEVICES,
save_txt = True,
save_conf = True,
nosave = False,
)
if source_index == 0:
with st.spinner(text = 'Preparing Images'):
for img in os.listdir(get_detection_folder()):
if img.endswith(".jpg") or img.endswith(".jpeg") or img.endswith(".png"):
pathImg = os.path.join(get_detection_folder(), img)
st.image(pathImg)
#image_with_boxes = draw_bounding_boxes(img, boxes, labels)
st.markdown("### Output")
st.write("Path of Saved Images: ", pathImg)
st.write("Path of TXT File: ", os.path.join(get_detection_folder(), 'labels'))
elif source_index == 1:
with st.spinner(text = 'Preparing Video'):
for vid in os.listdir(get_detection_folder()):
if vid.endswith(".mp4"):
#st.video(os.path.join(get_detection_folder(), vid))
#video_file = open(os.path.join(get_detection_folder(), vid), 'rb')
#video_bytes = video_file.read()
#st.video(video_bytes)
video_file = os.path.join(get_detection_folder(), vid)
stframe = st.empty()
cap = cv2.VideoCapture(video_file)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print("Width: ", width, "\n")
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("Height: ", height, "\n")
while cap.isOpened():
ret, img = cap.read()
if ret:
stframe.image(cv2.resize(img, (width, height)), channels = 'BGR', use_column_width = True)
#stframe.image(cv2.resize(img, (width, height)), channels='BGR', use_column_width=True)
else:
break
cap.release()
st.markdown("### Output")
st.write("Path of Saved Video: ", video_file)
st.write("Path of TXT File: ", os.path.join(get_detection_folder(), 'labels'))
else:
with st.spinner(text = 'Preparing Video'):
for vid in os.listdir(get_detection_folder()):
if vid.endswith(".mp4"):
liveFeedvideoFile = os.path.join(get_detection_folder(), vid)
st.markdown("### Output")
st.write("Path of Live Feed Saved Video: ", liveFeedvideoFile)
st.write("Path of TXT File: ", os.path.join(get_detection_folder(), 'labels'))
# --------------------MAIN FUNCTION CODE------------------------
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
# ------------------------------------------------------------------