Write a frame to the output destination (file or display window). If filename is empty, displays the frame in a window. If filename is set and video mode, writes frame to video file. If filename is set and image mode, saves frame as image file. Args:
(self, frame)
| 600 | return frame |
| 601 | |
| 602 | def _writeFrame(self, frame): |
| 603 | """ |
| 604 | Write a frame to the output destination (file or display window). |
| 605 | |
| 606 | If filename is empty, displays the frame in a window. |
| 607 | If filename is set and video mode, writes frame to video file. |
| 608 | If filename is set and image mode, saves frame as image file. |
| 609 | Args: |
| 610 | frame: The input frame as a bytearray to be written. |
| 611 | Returns: |
| 612 | None |
| 613 | """ |
| 614 | logger.debug(f"_writeFrame: frame size={len(frame)} bytes") |
| 615 | |
| 616 | # If the stream is not active, do nothing |
| 617 | if not self.active: |
| 618 | logger.error("_writeFrame: stream not active") |
| 619 | return |
| 620 | |
| 621 | try: |
| 622 | # Decode the frame from bytearray to a NumPy array |
| 623 | decoded_frame = np.frombuffer(frame, dtype=np.uint8) |
| 624 | |
| 625 | # Reshape the decoded frame to match the target resolution |
| 626 | decoded_frame = decoded_frame.reshape((self.frame_height, self.frame_width, 3)) |
| 627 | logger.debug(f"_writeFrame: decoded frame size=({decoded_frame.shape[1]}, {decoded_frame.shape[0]})") |
| 628 | |
| 629 | # Convert color space to BGR |
| 630 | frame_out = self.__convertToBGR(decoded_frame) |
| 631 | |
| 632 | if self.filename == None: |
| 633 | logger.debug("_writeFrame: display frame in window") |
| 634 | # If no filename, display the frame in a window |
| 635 | cv2.imshow(self.filename, frame_out) |
| 636 | cv2.waitKey(10) |
| 637 | else: |
| 638 | if self.video: |
| 639 | logger.debug("_writeFrame: write frame to video file") |
| 640 | # Write frame to video file |
| 641 | self.stream.write(np.uint8(frame_out)) |
| 642 | else: |
| 643 | logger.debug("_writeFrame: write frame as image file") |
| 644 | # Write frame as image file |
| 645 | cv2.imwrite(self.filename, frame_out) |
| 646 | |
| 647 | except Exception as e: |
| 648 | # Output exception debug information but continue |
| 649 | logger.error(f"Exception in _writeFrame: {type(e).__name__}: {e}", exc_info=True) |
| 650 | pass |
| 651 | |
| 652 | |
| 653 | def run(self): |