Egor Orel glenn-jocher commited on
Commit
48b00db
1 Parent(s): 57ce73c

Re-open IP-camera videostream if disconnected (#5074)

Browse files

* Re-open IP-camera videostream if disconnected. Provide IP-streaming analysis stability

* Re-open IP-camera videostream if disconnected. Provide IP-streaming analysis stability

* `self.imgs[i] *= 0` bug fix and cleanup

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>

Files changed (1) hide show
  1. utils/datasets.py +8 -3
utils/datasets.py CHANGED
@@ -312,7 +312,7 @@ class LoadStreams:
312
  self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
313
 
314
  _, self.imgs[i] = cap.read() # guarantee first frame
315
- self.threads[i] = Thread(target=self.update, args=([i, cap]), daemon=True)
316
  print(f" success ({self.frames[i]} frames {w}x{h} at {self.fps[i]:.2f} FPS)")
317
  self.threads[i].start()
318
  print('') # newline
@@ -323,7 +323,7 @@ class LoadStreams:
323
  if not self.rect:
324
  print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
325
 
326
- def update(self, i, cap):
327
  # Read stream `i` frames in daemon thread
328
  n, f, read = 0, self.frames[i], 1 # frame number, frame array, inference every 'read' frame
329
  while cap.isOpened() and n < f:
@@ -332,7 +332,12 @@ class LoadStreams:
332
  cap.grab()
333
  if n % read == 0:
334
  success, im = cap.retrieve()
335
- self.imgs[i] = im if success else self.imgs[i] * 0
 
 
 
 
 
336
  time.sleep(1 / self.fps[i]) # wait time
337
 
338
  def __iter__(self):
 
312
  self.frames[i] = max(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)), 0) or float('inf') # infinite stream fallback
313
 
314
  _, self.imgs[i] = cap.read() # guarantee first frame
315
+ self.threads[i] = Thread(target=self.update, args=([i, cap, s]), daemon=True)
316
  print(f" success ({self.frames[i]} frames {w}x{h} at {self.fps[i]:.2f} FPS)")
317
  self.threads[i].start()
318
  print('') # newline
 
323
  if not self.rect:
324
  print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
325
 
326
+ def update(self, i, cap, stream):
327
  # Read stream `i` frames in daemon thread
328
  n, f, read = 0, self.frames[i], 1 # frame number, frame array, inference every 'read' frame
329
  while cap.isOpened() and n < f:
 
332
  cap.grab()
333
  if n % read == 0:
334
  success, im = cap.retrieve()
335
+ if success:
336
+ self.imgs[i] = im
337
+ else:
338
+ print('WARNING: Video stream unresponsive, please check your IP camera connection.')
339
+ self.imgs[i] *= 0
340
+ cap.open(stream) # re-open stream if signal was lost
341
  time.sleep(1 / self.fps[i]) # wait time
342
 
343
  def __iter__(self):