-
Notifications
You must be signed in to change notification settings - Fork 7
/
streamlit_app.py
executable file
·130 lines (108 loc) · 4.15 KB
/
streamlit_app.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
import Detector
import cv2 as cv
import numpy as np
import streamlit as st
import pandas as pd
import tempfile
import time
# Bib bounding box color
color = [252, 15, 192]
# App Title and Mode
st.title('Race Bib Number Detector')
st.sidebar.header("Mode")
mode = st.sidebar.radio(
'Select Mode',
options=['Demo', 'Image', 'Video']
)
if mode == 'Image':
# Get Image from User
user_file = st.file_uploader(label='Image for analysis:',
type=['jpg', 'png'])
button_loc = st.empty()
# Display image and convert for predicting
if user_file != None:
text_loc = st.empty()
text_loc.text('This is your image:')
# Convert the file to an opencv image.
# From: https://github.com/streamlit/streamlit/issues/888
file_bytes = np.asarray(bytearray(user_file.read()), dtype=np.uint8)
img = cv.imdecode(file_bytes, 1)
img_loc = st.empty()
img_loc.image(img, channels='BGR')
if button_loc.button('Detect'):
# get bib prediction
output = Detector.get_rbns(img)
# annotate image
if output != None:
text_loc.text(f"Detected {len(output)} RBN(s)")
for detection in output:
img = Detector.annotate(img, detection, color)
else:
text_loc.text("No RBN's Detected")
#display annotated image
img_loc.image(img, channels='BGR')
else:
if mode == 'Demo':
video_path = 'Data/bib_detector_demo.mp4'
video_file = open(video_path, 'rb')
video_bytes = video_file.read()
elif mode == 'Video':
video_bytes = st.file_uploader(label='Video for analysis:',
type=['mp4'])
# Use temp file for OpenCV with user uploaded video
# from https://discuss.streamlit.io/t/how-to-access-uploaded-video-in-streamlit-by-open-cv/5831/4
if video_bytes == None:
st.stop()
else:
tfile = tempfile.NamedTemporaryFile(delete=False)
tfile.write(video_bytes.read())
video_path = tfile.name
button_loc = st.empty()
text_loc = st.empty()
video_loc = st.empty()
video_loc.video(video_bytes)
if button_loc.button('Detect'):
# open video for detection
cap = cv.VideoCapture(video_path)
cap.set(cv.CAP_PROP_FPS, 25)
# set ouput specifications
fourcc = cv.VideoWriter_fourcc('m','p','4','v')
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
num_frames = cap.get(cv.CAP_PROP_FRAME_COUNT)
vid_out = cv.VideoWriter('Data/output.mp4', fourcc, 25.0, (width,height))
#outfile = tempfile.NamedTemporaryFile(delete=False)
#vid_out = cv.VideoWriter(outfile.name, fourcc, 25.0, (width,height))
frames_complete = 0
rank = []
prev_rbn = None
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# get bib prediction
output = Detector.get_rbns(frame, single=True)
# annotate image
if output != None:
frame = Detector.annotate(frame, output[0], color)
if prev_rbn == None or prev_rbn != output[0][0]:
rbn_count = 0
prev_rbn = output[0][0]
else:
rbn_count += 1
if rbn_count >= 25 and prev_rbn not in rank:
rank.append(prev_rbn)
#save annotated frame
vid_out.write(frame)
frames_complete += 1
video_loc.progress(frames_complete / num_frames)
cap.release()
vid_out.release()
button_loc.text("Complete. Press play to see annotated video.")
video_file = open('Data/output.mp4', 'rb')
#video_file = open(outfile.name, 'rb')
video_bytes = video_file.read()
video_loc.video(video_bytes)
st.header("Rank Order")
for i, rbn in enumerate(rank):
st.subheader(f'{i+1}. {rbn}')