Read a single frame from the current video or image source. If the stream is not active or end-of-stream is reached, returns an empty bytearray. For video sources, handles frame dropping to match requested frame rate. For image sources, reads the image once and sets end-of-s
(self)
| 514 | |
| 515 | # Read frame from source |
| 516 | def _readFrame(self): |
| 517 | """Read a single frame from the current video or image source. |
| 518 | |
| 519 | If the stream is not active or end-of-stream is reached, returns an empty bytearray. |
| 520 | For video sources, handles frame dropping to match requested frame rate. |
| 521 | For image sources, reads the image once and sets end-of-stream. |
| 522 | Resizes and converts color space as needed. |
| 523 | Returns: |
| 524 | frame: The read frame as a bytearray, or empty if no frame is available. |
| 525 | """ |
| 526 | frame = bytearray() |
| 527 | |
| 528 | # If the stream is not active, return empty frame |
| 529 | if not self.active: |
| 530 | logger.error("_readFrame: stream not active") |
| 531 | return frame |
| 532 | |
| 533 | # If end-of-stream has been reached, return empty frame |
| 534 | if self.eos: |
| 535 | logger.debug("_readFrame: end of stream reached") |
| 536 | return frame |
| 537 | |
| 538 | if self.video: |
| 539 | # Video source, read frame from the video stream |
| 540 | _, frame_in = self.stream.read() # Frame is numpy.ndarray, (height, width, channels), dtype=uint8 |
| 541 | |
| 542 | if frame_in is not None: |
| 543 | logger.debug(f"_readFrame: frame captured, size=({frame_in.shape[1]}, {frame_in.shape[0]})") |
| 544 | |
| 545 | # Handle frame dropping if input FPS > requested FPS |
| 546 | if self.frame_ratio > 1: |
| 547 | # Accumulate fractional frames to drop |
| 548 | self.frame_drop += (self.frame_ratio - 1) |
| 549 | |
| 550 | if self.frame_drop > 1: |
| 551 | logger.debug(f"_readFrame: frames to drop={self.frame_drop}") |
| 552 | drop = int(self.frame_drop // 1) |
| 553 | |
| 554 | # Drop the required number of frames to match requested FPS |
| 555 | for i in range(drop): |
| 556 | _, _ = self.stream.read() |
| 557 | logger.debug(f"_readFrame: frames dropped={drop}") |
| 558 | self.frame_drop -= drop |
| 559 | logger.debug(f"_readFrame: frames left to drop={self.frame_drop}") |
| 560 | else: |
| 561 | # Frame not read, mark end-of-stream |
| 562 | self.eos = True |
| 563 | logger.debug("_readFrame: end of stream.") |
| 564 | else: |
| 565 | # For image sources, read the image once and set end-of-stream |
| 566 | frame_in = cv2.imread(self.filename) |
| 567 | self.eos = True |
| 568 | logger.debug("_readFrame: end of stream.") |
| 569 | |
| 570 | if frame_in is not None: |
| 571 | target_width = self.frame_width |
| 572 | target_height = self.frame_height |
| 573 |
no test coverage detected