(self, sources='streams.txt', img_size=640)
| 281 | |
| 282 | class 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 |
nothing calls this directly
no test coverage detected