-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
187 lines (150 loc) · 4.44 KB
/
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
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
from contextlib import suppress
from pathlib import Path
from shutil import which
import streamlit as st
from av import VideoFrame
from cv2 import VideoCapture, VideoWriter_fourcc
from psutil import ZombieProcess, process_iter
from streamlit import session_state
from streamlit import sidebar as sb
from streamlit_webrtc import webrtc_streamer
from supervision import VideoInfo
from core import Annotator
from model import Model
from utils import cvt, hms, maxcam, st_config, trim_vid
_shape = None
if 'path' not in session_state:
session_state['path'] = ''
def prepare(path: str, place):
vid = VideoInfo.from_video_path(path)
if which('ffmpeg'):
if place.toggle('Trim'):
length = int(vid.total_frames / vid.fps)
begin, end = place.slider(
' ',
value=(0, length),
max_value=length,
label_visibility='collapsed',
)
begin, end = hms(begin), hms(end)
if place.button(f'Trim from {begin[3:]} to {end[3:]}'):
path = trim_vid(path, begin, end)
session_state['path'] = path
else:
session_state['path'] = path
else:
session_state['path'] = path
def main(state):
st_config()
file = sb.file_uploader(' ', label_visibility='collapsed')
running = sb.toggle('Realtime inference', help='Slower than native')
usecam = sb.toggle('Use camera')
mt = st.empty()
if usecam:
file = None
an = Annotator.ui(0)
reso = maxcam()
width, height = reso
cap = VideoCapture(0)
codec = VideoWriter_fourcc(*'MJPG')
cap.set(6, codec)
cap.set(5, 30)
cap.set(3, width)
cap.set(4, height)
while running:
t1, t2 = mt.tabs(['Main', 'Fallback'])
if an.unneeded:
t1, t2 = t2, t1
success, f = cap.read()
if success:
f, fallback = an.from_frame(f)
t1.image(cvt(f))
t2.image(fallback)
else:
break
cap.release()
def cam_stream(key, callback):
webrtc_streamer(
key=key,
video_frame_callback=callback,
media_stream_constraints={
'video': {
'width': {'min': width},
'height': {'min': height},
}
},
)
def simplecam(frame):
f = frame.to_ndarray(format='bgr24')
return VideoFrame.from_ndarray(an.from_frame(f)[1])
# oh my god, it took me so long to realize the frame bigger through time
def cam(frame):
f = frame.to_ndarray(format='bgr24')
global _shape
if an.linezone and f.shape != _shape:
_shape = f.shape
an.linezone.update(f)
return VideoFrame.from_ndarray(cvt(an.from_frame(f)[0]))
if an.unneeded:
cam_stream('cp', simplecam)
else:
cam_stream('ds', cam)
if file:
ex = sb.expander('Uploaded file')
if 'image' in file.type:
ex.image(file)
Model.ui(track=False).predict_image(file)
elif 'video' in file.type:
ex.video(file)
path = f'up_{file.name}'
with open(path, 'wb') as up:
up.write(file.read())
prepare(path, ex)
path = session_state['path']
vid = VideoInfo.from_video_path(path)
reso = vid.resolution_wh
total_frames = vid.total_frames
ex.markdown(
f"""
- Video resolution: {'x'.join([str(i) for i in reso])}
- Total frames: {total_frames}
- FPS: {vid.fps}
- Path: {path}
"""
)
an = Annotator.ui(path)
count = 0
while running:
for f, fallback in an.gen(path):
if count == total_frames:
running = False
break
t1, t2 = mt.tabs(['Main', 'Fallback'])
if an.unneeded:
t1, t2 = t2, t1
t1.image(cvt(f))
t2.image(fallback)
t1.progress(count / total_frames)
count += 1
else:
sb.warning('Please upload image/video')
try:
from lightning.app import LightningApp, LightningFlow
from lightning.app.frontend import StreamlitFrontend
class App(LightningFlow):
def configure_layout(self):
return StreamlitFrontend(render_fn=main)
def run(self):
pass
lit = LightningApp(App())
running_apps = []
for p in process_iter():
with suppress(ZombieProcess):
running_apps.append(p.cmdline())
running_apps = [i for i in running_apps if 'run' in i]
this_process = next(p for p in running_apps if any(Path(__file__).stem in a for a in p))
if 'app' not in this_process:
main('')
except ImportError:
pass
main('')