(self, sources='streams.txt', img_size=640)
| 254 | |
| 255 | class LoadStreams: # multiple IP or RTSP cameras |
| 256 | def __init__(self, sources='streams.txt', img_size=640): |
| 257 | self.mode = 'images' |
| 258 | self.img_size = img_size |
| 259 | |
| 260 | if os.path.isfile(sources): |
| 261 | with open(sources, 'r') as f: |
| 262 | sources = [x.strip() for x in f.read().splitlines() if len(x.strip())] |
| 263 | else: |
| 264 | sources = [sources] |
| 265 | |
| 266 | n = len(sources) |
| 267 | self.imgs = [None] * n |
| 268 | self.sources = sources |
| 269 | for i, s in enumerate(sources): |
| 270 | # Start the thread to read frames from the video stream |
| 271 | print('%g/%g: %s... ' % (i + 1, n, s), end='') |
| 272 | cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s) |
| 273 | assert cap.isOpened(), 'Failed to open %s' % s |
| 274 | w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 275 | h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 276 | fps = cap.get(cv2.CAP_PROP_FPS) % 100 |
| 277 | _, self.imgs[i] = cap.read() # guarantee first frame |
| 278 | thread = Thread(target=self.update, args=([i, cap]), daemon=True) |
| 279 | print(' success (%gx%g at %.2f FPS).' % (w, h, fps)) |
| 280 | thread.start() |
| 281 | print('') # newline |
| 282 | |
| 283 | # check for common shapes |
| 284 | s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes |
| 285 | self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal |
| 286 | if not self.rect: |
| 287 | print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.') |
| 288 | |
| 289 | def update(self, index, cap): |
| 290 | # Read next stream frame in a daemon thread |
nothing calls this directly
no test coverage detected