forked from naisy/realtime_object_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpfps.py
272 lines (250 loc) · 11.6 KB
/
mpfps.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
import multiprocessing
import logging
import time
import sys
import numpy as np
import threading
from lib.mpvariable import MPVariable
import types
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY2:
import copy_reg
elif PY3:
import copyreg as copy_reg
def _pickle_method(m):
if m.im_self is None:
return getattr, (m.im_class, m.im_func.func_name)
else:
return getattr, (m.im_self, m.im_func.func_name)
copy_reg.pickle(types.MethodType, _pickle_method)
class FPS():
def __init__(self, cfg):
self.cfg = cfg
return
def start_counter(self):
"""
Start via Process
"""
m = multiprocessing.Process(target=self.process_fps_counter, args=())
m.start()
return m
def start_console(self):
"""
Start via Process
"""
m = multiprocessing.Process(target=self.process_fps_console, args=())
m.start()
return m
def process_fps_counter(self):
"""
frame_counter.value: Frame counter value shared by processes.
update fps by 0.2 sec
"""
logging.debug("enter")
FPS_INTERVAL = self.cfg['fps_interval']
DEBUG_MODE = self.cfg['debug_mode']
MAX_VIS_FPS = self.cfg['max_vis_fps']
sleep_interval = 1.0 / 50.0 # Wakeup and time checks N times per sec.
snapshot_interval = 1.0 / 5.0 # FPS calculate N times per sec. (must snapshot_interval >= sleep_interval)
fps_stream_length = FPS_INTERVAL # FPS stream seconds length. FPS is the frames per second processed during the most recent this time.
fps_stream = [] # Array of fps_snapshot during fps_stream_length
snapshot = None # One array (frames_in_interval, interval_seconds, unixtime) per snapshot_interval
vis_fps_stream = []
vis_snapshot = None
if DEBUG_MODE:
""" FOR PERFORMANCE DEBUG """
snapshot_fps = 0
min_snapshot_fps = 10000
max_snapshot_fps = 0
min_snapshot_list = []
max_snapshot_list = []
""" """
try:
launch_time = time.time()
while MPVariable.running.value and MPVariable.frame_counter.value == 0:
time.sleep(sleep_interval)
MPVariable.first_complete_time.value = time.time() - launch_time
print("Time to first image:{}".format(MPVariable.first_complete_time.value))
previos_work_time = time.time()
while MPVariable.running.value:
time.sleep(sleep_interval)
now_time = time.time()
if now_time >= previos_work_time + snapshot_interval:
### FPS update by snapshot_interval ###
snapshot_frames = MPVariable.frame_counter.value
MPVariable.frame_counter.value = 0
vis_snapshot_frames = MPVariable.vis_frame_counter.value
MPVariable.vis_frame_counter.value = 0
"""
FPS stream
"""
snapshot_seconds = now_time - previos_work_time
snapshot = (snapshot_frames, snapshot_seconds, now_time)
fps_stream += [snapshot]
vis_snapshot = (vis_snapshot_frames, snapshot_seconds, now_time)
vis_fps_stream += [vis_snapshot]
if DEBUG_MODE:
"""
FOR PERFORMANCE DEBUG
FPS snapshot calculation
"""
snapshot_fps = snapshot_frames/snapshot_seconds
MPVariable.fps_snapshot.value = snapshot_fps # FPS of snapshot. for visualize
if min_snapshot_fps >= snapshot_fps:
min_snapshot_fps = snapshot_fps
min_snapshot_list += [snapshot_fps]
print("min_snapshot:{:.1f} {}".format(snapshot_fps, snapshot))
if max_snapshot_fps <= snapshot_fps:
max_snapshot_fps = snapshot_fps
max_snapshot_list += [snapshot_fps]
print("max_snapshot:{:.1f} {}".format(snapshot_fps, snapshot))
""" """
"""
PROC FPS
"""
while MPVariable.running.value:
(min_frame, min_seconds, min_time) = fps_stream[0]
if now_time - min_time > fps_stream_length:
"""
drop old snapshot
"""
fps_stream.pop(0)
else:
"""
goto FPS streaming calculation
"""
break
if(len(fps_stream) > 0):
"""
FPS streaming calculation
count frames and seconds in stream
"""
np_fps_stream = np.array(fps_stream)
np_fps_stream = np_fps_stream[:,:2] # take frames, seconds. drop unixtime.
np_fps = np.sum(np_fps_stream, axis=0) # [total_frames, total_seconds] duaring fps_stream_length
"""
insert local values to shared variables
"""
MPVariable.fps_frames.value = int(np_fps[0]) # for console output
MPVariable.fps_seconds.value = np_fps[1] # for console output
MPVariable.fps.value = np_fps[0]/np_fps[1] # for visualize and console
else:
MPVariable.fps_frames.value = 0
MPVariable.fps_seconds.value = -1 # for toooooooo slow fps check. if -1 sec appears, fps_stream_length should set more long time.
MPVariable.fps.value = 0
"""
VIS FPS
"""
while MPVariable.running.value:
(min_frame, min_seconds, min_time) = vis_fps_stream[0]
if now_time - min_time > fps_stream_length:
"""
drop old snapshot
"""
vis_fps_stream.pop(0)
else:
"""
goto FPS streaming calculation
"""
break
if(len(vis_fps_stream) > 0):
"""
FPS streaming calculation
count frames and seconds in stream
"""
np_fps_stream = np.array(vis_fps_stream)
np_fps_stream = np_fps_stream[:,:2] # take frames, seconds. drop unixtime.
np_fps = np.sum(np_fps_stream, axis=0) # [total_frames, total_seconds] duaring fps_stream_length
"""
insert local values to shared variables
"""
MPVariable.vis_fps_frames.value = int(np_fps[0]) # for console output
MPVariable.vis_fps.value = np_fps[0]/np_fps[1] # for visualize and console
if MAX_VIS_FPS <= 0:
MPVariable.vis_skip_rate.value = 0
else:
rate = MPVariable.fps.value/MAX_VIS_FPS
MPVariable.vis_skip_rate.value = rate
else:
MPVariable.vis_fps_frames.value = 0
MPVariable.vis_fps.value = 0
MPVariable.vis_skip_rate.value = 0
previos_work_time = now_time
except KeyboardInterrupt:
pass
except Exception as e:
import traceback
traceback.print_exc()
finally:
MPVariable.running.value = False
if DEBUG_MODE:
print("min_snapshot_fps:{:.1f}".format(min_snapshot_fps))
print("{}".format(min_snapshot_list))
print("max_snapshot_fps:{:.1f}".format(max_snapshot_fps))
print("{}".format(max_snapshot_list))
return
def process_fps_console(self):
"""
print fps by fps_interval sec.
"""
logging.debug("enter")
FPS_INTERVAL = self.cfg['fps_interval']
DEBUG_MODE = self.cfg['debug_mode']
SPLIT_MODEL = self.cfg['split_model']
sleep_interval = 1.0 / 50.0 # Wakeup and time checks N times per sec.
try:
while MPVariable.running.value and MPVariable.frame_counter.value == 0:
time.sleep(sleep_interval)
previos_work_time = time.time()
while MPVariable.running.value:
time.sleep(sleep_interval)
now_time = time.time()
if now_time >= previos_work_time + FPS_INTERVAL:
"""
FPS console by fps_interval
"""
frames = MPVariable.fps_frames.value
seconds = MPVariable.fps_seconds.value
if frames == 0:
total = 0
cap = 0
worker = 0
gpu = 0
cpu = 0
lost = 0
else:
total = MPVariable.total_proc_time.value/frames
cap = MPVariable.cap_proc_time.value/frames
worker = MPVariable.gpu_proc_time.value/frames
gpu = MPVariable.gpu_proc_time.value/frames
cpu = MPVariable.cpu_proc_time.value/frames
lost = MPVariable.lost_proc_time.value/frames
print("FPS:{: ^5.1f} Frames:{: ^3} Seconds:{: ^10.5f} | 1FRAME total:{: ^10.5f} cap:{: ^10.5f} worker:{: ^10.5f} gpu:{: ^10.5f} cpu:{: ^10.5f} lost:{: ^10.5f} send:{: ^10.5f} | VFPS:{: ^5.1f} VFrames:{: ^3} VDrops:{: ^3}"
.format(MPVariable.fps.value, MPVariable.fps_frames.value, MPVariable.fps_seconds.value,
total,
cap,
worker,
gpu,
cpu,
lost,
MPVariable.send_proc_time.value,
MPVariable.vis_fps.value, MPVariable.vis_fps_frames.value, MPVariable.vis_drop_frames.value))
MPVariable.cap_proc_time.value = 0
MPVariable.worker_proc_time.value = 0
MPVariable.gpu_proc_time.value = 0
MPVariable.cpu_proc_time.value = 0
MPVariable.lost_proc_time.value = 0
MPVariable.total_proc_time.value = 0
MPVariable.vis_proc_time.value = 0
MPVariable.vis_drop_frames.value = 0
previos_work_time = now_time
except KeyboardInterrupt:
pass
except Exception as e:
import traceback
traceback.print_exc()
finally:
MPVariable.running.value = False
print("Time to first image:{}".format(MPVariable.first_complete_time.value))
return