-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_thread.py
277 lines (218 loc) · 8.42 KB
/
video_thread.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Aug 22 20:00:06 2021
Idea y código original:
https://www.youtube.com/watch?v=sW4CVI51jDY
Clayton Darwin
https://gitlab.com/duder1966/youtube-projects/-/tree/master/
@author: mherrera
"""
import time
import threading
import queue
import cv2
import numpy as np
# ------------------------------
# Camera Tread
# ------------------------------
class VideoThread:
def __init__(self,
video_source=0, # device, stream or file
video_width=640,
video_height=480,
video_frame_rate=10,
buffer_all=False,
video_fourcc=cv2.VideoWriter_fourcc(*"MJPG"),
try_to_reconnect=False):
self.video_source = video_source
self.video_width = video_width
self.video_height = video_height
self.video_frame_rate = video_frame_rate
self.video_fourcc = video_fourcc
self.buffer_all = buffer_all
self.try_to_reconnect = try_to_reconnect
# ------------------------------
# System Variables
# ------------------------------
# buffer setup
self.buffer_length = 5
# control states
self.frame_grab_run = False
self.frame_grab_on = False
# counts and amounts
self.frame_count = 0
self.frames_returned = 0
self.current_frame_rate = 0.0
self.loop_start_time = 0
self.last_try_reconnection_time = 0
# buffer
if self.buffer_all:
self.buffer = queue.Queue(self.buffer_length)
else:
# last frame only
self.buffer = queue.Queue(1)
self.finished = False
# camera setup
self.video_init_wait_time = 0.5
self.resource = cv2.VideoCapture(self.video_source)
self.resource.set(cv2.CAP_PROP_FRAME_WIDTH, self.video_width)
self.resource.set(cv2.CAP_PROP_FRAME_HEIGHT, self.video_height)
self.resource.set(cv2.CAP_PROP_FPS, self.video_frame_rate)
self.resource.set(cv2.CAP_PROP_FOURCC, self.video_fourcc)
time.sleep(self.video_init_wait_time)
if not self.resource.isOpened():
self.resource_available = False
print('SELF.NOT')
else:
self.resource_available = True
# get the actual cam configuration
self.video_width = int(self.resource.get(cv2.CAP_PROP_FRAME_WIDTH))
self.video_height = int(self.resource.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.video_frame_rate = self.resource.get(cv2.CAP_PROP_FPS)
self.video_fourcc = self.resource.get(cv2.CAP_PROP_FOURCC)
# black frame (filler)
self.black_frame = np.zeros((
self.video_height, self.video_width, 3), np.uint8)
def get_curr_config_fps(self):
return self.video_frame_rate
def get_curr_config_widht(self):
return self.video_width
def get_curr_config_height(self):
return self.video_height
def get_curr_frame_number(self):
return self.frame_count
def reconnect(self):
self.stop()
self.__init__(
buffer_all=self.buffer_all,
video_source=self.video_source,
video_width=self.video_width,
video_height=self.video_height,
video_frame_rate=self.video_frame_rate,
video_fourcc=self.video_fourcc,
try_to_reconnect=self.try_to_reconnect
)
self.start()
print('reconnecting...')
def is_available(self):
return self.resource_available
def start(self):
# set run state
self.frame_grab_run = True
# start thread
self.thread = threading.Thread(target=self.loop)
self.thread.start()
def stop(self):
#print('########## stop')
# set loop kill state
self.frame_grab_run = False
# let loop stop
while self.frame_grab_on:
time.sleep(0.1)
# stop camera if not already stopped
if self.resource:
try:
self.resource.release()
except Exception:
pass
self.resource = None
self.resource_available = False
# drop buffer
self.buffer = None
# set recconection time
self.last_try_reconnection_time = 0
def loop(self):
# load start frame
frame = self.black_frame
if not self.buffer.full():
self.buffer.put(frame, False)
# status
self.frame_grab_on = True
self.loop_start_time = time.time()
# frame rate
local_loop_frame_counter = 0
local_loop_start_time = time.time()
while self.resource.grab():
# external shut down
if not self.frame_grab_run:
break
# true buffered mode (for files, no loss)
if self.buffer_all:
# buffer is full, pause and loop
if self.buffer.full():
time.sleep(1/self.video_frame_rate)
# or load buffer with next frame
else:
grabbed, frame = self.resource.retrieve()
# grabbed, frame = self.resource.read()
if not grabbed:
break
self.buffer.put(frame, False)
self.frame_count += 1
local_loop_frame_counter += 1
# false buffered mode (for camera, loss allowed)
else:
grabbed, frame = self.resource.retrieve()
# grabbed, frame = self.resource.read()
if not grabbed:
break
# open a spot in the buffer
if self.buffer.full():
self.buffer.get()
self.buffer.put(frame, False)
self.frame_count += 1
local_loop_frame_counter += 1
# update frame read rate
if local_loop_frame_counter >= 10:
self.current_frame_rate = \
round(local_loop_frame_counter/
(time.time()-local_loop_start_time), 2)
local_loop_frame_counter = 0
local_loop_start_time = time.time()
# shut down
self.loop_start_time = 0
self.frame_grab_on = False
self.resource_available = False
# self.stop()
def next(self, black=True, wait=0):
# black frame default
if black:
frame = self.black_frame.copy()
# no frame default
else:
frame = None
# # can't open camera by index or loss connection or EOF
# if not self.is_available():
# print('not available:{}'.format(self.video_source))
if not self.finished:
# if self.is_available():
# print('########## self.buffer.qsize():{}'.format(self.buffer.qsize()))
# print('########## self.buffer.empty():{}'.format(self.buffer.empty()))
if self.is_available() or not self.buffer.empty():
try:
#print('\t########## self.buffer.qsize():{}'.format(self.buffer.qsize()))
frame = self.buffer.get(timeout=wait)
self.frames_returned += 1
except queue.Empty:
# print('Queue Empty!')
# print(traceback.format_exc())
pass
# elif not self.buffer.empty():
# print('\t@@@@@@@@@@@@@@@@@ self.is_available() and self.buffer.empty()')
elif self.try_to_reconnect:
if self.last_try_reconnection_time == 0:
self.last_try_reconnection_time = time.time()
else:
if time.time() - self.last_try_reconnection_time >= 10.0:
self.reconnect()
if self.is_available():
self.last_try_reconnection_time = 0
else:
self.last_try_reconnection_time = time.time()
else:
#print('\t########## STOP: self.buffer.qsize():{}'.format(self.buffer.qsize()))
self.finished = True
#self.stop()
#print('\n')
return self.finished, frame