import sys import cv2 import numpy as np from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout from PyQt6.QtGui import QImage, QPixmap from PyQt6.QtCore import QTimer import signal class ImageViewer(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Detection Results") self.label = QLabel() layout = QVBoxLayout() layout.addWidget(self.label) self.setLayout(layout) # Timer to periodically update the image self.timer = QTimer() self.timer.timeout.connect(self.update_frame) self.timer.start(30) # Update every 30 ms (~33 FPS) # Load or generate image self.image = self.load_test_image() def load_test_image(self): # Replace this with your own image source (e.g., converted ROS2 Image) img = cv2.imread("a.png") # img = np.zeros((480, 640, 3), dtype=np.uint8) # cv2.putText(img, "Marker Display", (50, 240), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 2) return img def update_frame(self): img = self.image.copy() # Convert BGR to RGB rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to QImage h, w, ch = rgb_image.shape bytes_per_line = ch * w q_img = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format.Format_RGB888) # Convert to QPixmap and display self.label.setPixmap(QPixmap.fromImage(q_img)) def handle_exit_signal(sig, frame): print("Quitting...") QApplication.quit() if __name__ == "__main__": app = QApplication(sys.argv) signal.signal(signal.SIGINT, handle_exit_signal) viewer = ImageViewer() viewer.show() sys.exit(app.exec())