import sys import cv2 import numpy as np import multiprocessing from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5.QtCore import QTimer from Detection_window import Ui_MainWindow from camera.camera_process import CameraProcess class DetectionApp(QtWidgets.QMainWindow, Ui_MainWindow): def __init__(self): super(DetectionApp, self).__init__() self.setupUi(self) self.image_queue = multiprocessing.Queue(maxsize=1) # ✅ 啟動 CameraProcess self.camera_process = CameraProcess(self.image_queue) self.camera_process.start() # ✅ 設定 QTimer,每 100ms 更新影像 self.timer = QTimer(self) self.timer.timeout.connect(self.update_view_origin) self.timer.start(100) # 每 100ms 更新一次影像 def update_view_origin(self): """ 從 Queue 獲取影像並顯示在 QLabel (view_origin) 上 """ if not self.image_queue.empty(): image = self.image_queue.get() # 取得最新影像 self.display_image(image) def display_image(self, image): """ 顯示影像到 QLabel (view_origin) """ image_bgr = cv2.cvtColor(image, cv2.COLOR_BayerBG2BGR) if len(image.shape) == 2 else image height, width, channel = image_bgr.shape bytes_per_line = 3 * width qimage = QtGui.QImage(image_bgr.data, width, height, bytes_per_line, QtGui.QImage.Format_BGR888) pixmap = QtGui.QPixmap.fromImage(qimage).scaled(self.view_origin.size(), QtCore.Qt.KeepAspectRatio) self.view_origin.setPixmap(pixmap) def closeEvent(self, event): """ 確保程式關閉時正確停止相機擷取進程 """ self.camera_process.stop() # 停止相機擷取 self.camera_process.join() # 等待進程結束 event.accept() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = DetectionApp() window.show() sys.exit(app.exec_())