更新相機進程

This commit is contained in:
JEFF 2025-03-10 13:45:43 +08:00
parent 7eeba44dfe
commit b2312d0758
4 changed files with 134 additions and 30 deletions

View File

@ -14,16 +14,31 @@ from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object): class Ui_MainWindow(object):
def setupUi(self, MainWindow): def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow") MainWindow.setObjectName("MainWindow")
MainWindow.resize(1308, 865) MainWindow.resize(2024, 1118)
self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget") self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget) self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout") self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.bt_KeepShot = QtWidgets.QPushButton(self.centralwidget)
self.bt_KeepShot.setMinimumSize(QtCore.QSize(100, 50))
self.bt_KeepShot.setMaximumSize(QtCore.QSize(100, 50))
self.bt_KeepShot.setObjectName("bt_KeepShot")
self.horizontalLayout.addWidget(self.bt_KeepShot)
self.bt_StopKeepShot = QtWidgets.QPushButton(self.centralwidget)
self.bt_StopKeepShot.setMinimumSize(QtCore.QSize(100, 50))
self.bt_StopKeepShot.setMaximumSize(QtCore.QSize(100, 50))
self.bt_StopKeepShot.setObjectName("bt_StopKeepShot")
self.horizontalLayout.addWidget(self.bt_StopKeepShot)
self.bt_detection = QtWidgets.QPushButton(self.centralwidget) self.bt_detection = QtWidgets.QPushButton(self.centralwidget)
self.bt_detection.setMinimumSize(QtCore.QSize(100, 50)) self.bt_detection.setMinimumSize(QtCore.QSize(100, 50))
self.bt_detection.setMaximumSize(QtCore.QSize(100, 50)) self.bt_detection.setMaximumSize(QtCore.QSize(100, 50))
self.bt_detection.setObjectName("bt_detection") self.bt_detection.setObjectName("bt_detection")
self.gridLayout.addWidget(self.bt_detection, 0, 0, 1, 1) self.horizontalLayout.addWidget(self.bt_detection)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.view_origin = QtWidgets.QLabel(self.centralwidget) self.view_origin = QtWidgets.QLabel(self.centralwidget)
self.view_origin.setMinimumSize(QtCore.QSize(1000, 1000)) self.view_origin.setMinimumSize(QtCore.QSize(1000, 1000))
self.view_origin.setMaximumSize(QtCore.QSize(1000, 1000)) self.view_origin.setMaximumSize(QtCore.QSize(1000, 1000))
@ -36,7 +51,7 @@ class Ui_MainWindow(object):
self.gridLayout.addWidget(self.view_predict, 1, 1, 1, 1) self.gridLayout.addWidget(self.view_predict, 1, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget) MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1308, 22)) self.menubar.setGeometry(QtCore.QRect(0, 0, 2024, 22))
self.menubar.setObjectName("menubar") self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar) MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar = QtWidgets.QStatusBar(MainWindow)
@ -49,6 +64,8 @@ class Ui_MainWindow(object):
def retranslateUi(self, MainWindow): def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate _translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.bt_KeepShot.setText(_translate("MainWindow", "開始連續取像"))
self.bt_StopKeepShot.setText(_translate("MainWindow", "停止連續取像"))
self.bt_detection.setText(_translate("MainWindow", "檢測")) self.bt_detection.setText(_translate("MainWindow", "檢測"))
self.view_origin.setText(_translate("MainWindow", "原圖影像")) self.view_origin.setText(_translate("MainWindow", "原圖影像"))
self.view_predict.setText(_translate("MainWindow", "檢測影像")) self.view_predict.setText(_translate("MainWindow", "檢測影像"))

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1308</width> <width>2024</width>
<height>865</height> <height>1118</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -16,23 +16,78 @@
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QPushButton" name="bt_detection"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="minimumSize"> <item>
<size> <widget class="QPushButton" name="bt_KeepShot">
<width>100</width> <property name="minimumSize">
<height>50</height> <size>
</size> <width>100</width>
</property> <height>50</height>
<property name="maximumSize"> </size>
<size> </property>
<width>100</width> <property name="maximumSize">
<height>50</height> <size>
</size> <width>100</width>
</property> <height>50</height>
<property name="text"> </size>
<string>檢測</string> </property>
</property> <property name="text">
</widget> <string>開始連續取像</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="bt_StopKeepShot">
<property name="minimumSize">
<size>
<width>100</width>
<height>50</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>停止連續取像</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="bt_detection">
<property name="minimumSize">
<size>
<width>100</width>
<height>50</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>100</width>
<height>50</height>
</size>
</property>
<property name="text">
<string>檢測</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item> </item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="view_origin"> <widget class="QLabel" name="view_origin">
@ -79,7 +134,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1308</width> <width>2024</width>
<height>22</height> <height>22</height>
</rect> </rect>
</property> </property>

View File

@ -46,4 +46,7 @@ class CameraProcess(multiprocessing.Process):
def stop(self): def stop(self):
""" 停止相機擷取進程 """ """ 停止相機擷取進程 """
self.running.value = False print("正在停止相機擷取...")
self.running.value = False # 設定為 False讓 while 迴圈停止
self.terminate() # 強制終止進程
print("相機擷取進程已終止")

View File

@ -12,17 +12,47 @@ class DetectionApp(QtWidgets.QMainWindow, Ui_MainWindow):
super(DetectionApp, self).__init__() super(DetectionApp, self).__init__()
self.setupUi(self) self.setupUi(self)
# ✅ 先初始化 image_queue再啟動相機擷取
self.image_queue = multiprocessing.Queue(maxsize=1) self.image_queue = multiprocessing.Queue(maxsize=1)
self.camera_process = None # 相機進程尚未啟動
# ✅ 啟動 CameraProcess self.bt_KeepShot.clicked.connect(self.KeepShot)
self.camera_process = CameraProcess(self.image_queue) self.bt_StopKeepShot.clicked.connect(self.StopKeepShot)
self.camera_process.start()
# ✅ 設定 QTimer每 100ms 更新影像 # ✅ 設定 QTimer每 100ms 更新影像
self.timer = QTimer(self) self.timer = QTimer(self)
self.timer.timeout.connect(self.update_view_origin) self.timer.timeout.connect(self.update_view_origin)
self.timer.start(100) # 每 100ms 更新一次影像 self.timer.start(100) # 每 100ms 更新一次影像
def KeepShot(self):
""" 啟動相機擷取 """
if self.camera_process is None or not self.camera_process.is_alive():
self.StopKeepShot() # 確保先停止舊的 Process避免衝突
# ✅ 重新建立 Queue避免殘留影像影響新擷取
self.image_queue = multiprocessing.Queue(maxsize=1)
# ✅ 確保 `CameraProcess` 是新的
self.camera_process = CameraProcess(self.image_queue)
self.camera_process.start()
else:
print("相機已經在擷取")
def StopKeepShot(self):
""" 停止相機擷取 """
if self.camera_process and self.camera_process.is_alive():
self.camera_process.stop() # 停止擷取
self.camera_process.join() # 等待進程完全結束
self.camera_process = None
print("已停止影像擷取")
# ✅ 清空 Queue確保新擷取不會讀取到舊影像
while not self.image_queue.empty():
try:
self.image_queue.get_nowait()
except:
break
def update_view_origin(self): def update_view_origin(self):
""" 從 Queue 獲取影像並顯示在 QLabel (view_origin) 上 """ """ 從 Queue 獲取影像並顯示在 QLabel (view_origin) 上 """
if not self.image_queue.empty(): if not self.image_queue.empty():
@ -40,9 +70,8 @@ class DetectionApp(QtWidgets.QMainWindow, Ui_MainWindow):
def closeEvent(self, event): def closeEvent(self, event):
""" 確保程式關閉時正確停止相機擷取進程 """ """ 確保程式關閉時正確停止相機擷取進程 """
self.camera_process.stop() # 停止相機擷取 self.StopKeepShot() # 確保關閉程式時停止相機擷取
self.camera_process.join() # 等待進程結束 event.accept() # ✅ 允許視窗關閉
event.accept()
if __name__ == "__main__": if __name__ == "__main__":