MCPcopy
hub / github.com/WongKinYiu/PyTorch_YOLOv4 / LoadStreams

Class LoadStreams

utils/datasets.py:282–352  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

280
281
282class LoadStreams: # multiple IP or RTSP cameras
283 def __init__(self, sources='streams.txt', img_size=640):
284 self.mode = 'images'
285 self.img_size = img_size
286
287 if os.path.isfile(sources):
288 with open(sources, 'r') as f:
289 sources = [x.strip() for x in f.read().splitlines() if len(x.strip())]
290 else:
291 sources = [sources]
292
293 n = len(sources)
294 self.imgs = [None] * n
295 self.sources = sources
296 for i, s in enumerate(sources):
297 # Start the thread to read frames from the video stream
298 print('%g/%g: %s... ' % (i + 1, n, s), end='')
299 cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
300 assert cap.isOpened(), 'Failed to open %s' % s
301 w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
302 h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
303 fps = cap.get(cv2.CAP_PROP_FPS) % 100
304 _, self.imgs[i] = cap.read() # guarantee first frame
305 thread = Thread(target=self.update, args=([i, cap]), daemon=True)
306 print(' success (%gx%g at %.2f FPS).' % (w, h, fps))
307 thread.start()
308 print('') # newline
309
310 # check for common shapes
311 s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes
312 self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
313 if not self.rect:
314 print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
315
316 def update(self, index, cap):
317 # Read next stream frame in a daemon thread
318 n = 0
319 while cap.isOpened():
320 n += 1
321 # _, self.imgs[index] = cap.read()
322 cap.grab()
323 if n == 4: # read every 4th frame
324 _, self.imgs[index] = cap.retrieve()
325 n = 0
326 time.sleep(0.01) # wait time
327
328 def __iter__(self):
329 self.count = -1
330 return self
331
332 def __next__(self):
333 self.count += 1
334 img0 = self.imgs.copy()
335 if cv2.waitKey(1) == ord('q'): # q to quit
336 cv2.destroyAllWindows()
337 raise StopIteration
338
339 # Letterbox

Callers 1

detectFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected