增加推論程式

This commit is contained in:
JEFF 2025-03-10 21:33:20 +08:00
parent d91bbd1003
commit c231a3abf8
2 changed files with 83 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import numpy as np
import multiprocessing
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import QTimer
from ultralytics import YOLO
from Detection_window import Ui_MainWindow
from camera.camera_process import CameraProcess
@ -25,10 +26,20 @@ class DetectionApp(QtWidgets.QMainWindow, Ui_MainWindow):
# ✅ 先初始化 image_queue再啟動相機擷取
self.image_queue = multiprocessing.Queue(maxsize=1)
self.camera_process = None # 相機進程尚未啟動
self.latest_image = None # 存儲最新影像
# ✅ 載入 YOLO模型
try:
self.model = YOLO("model/best.pt")
self.log_handler.write_log("YOLO 模型載入成功")
except Exception as e:
self.log_handler.write_log(f"⚠️ YOLO 模型載入失敗: {e}")
self.model = None
# ✅ 連接按鈕事件
self.bt_KeepShot.clicked.connect(self.KeepShot)
self.bt_StopKeepShot.clicked.connect(self.StopKeepShot)
self.bt_detection.clicked.connect(self.detection)
# ✅ 設定 QTimer每 100ms 更新影像
self.timer = QTimer(self)
@ -71,16 +82,47 @@ class DetectionApp(QtWidgets.QMainWindow, Ui_MainWindow):
""" 從 Queue 獲取影像並顯示在 QLabel (view_origin) 上 """
if not self.image_queue.empty():
image = self.image_queue.get() # 取得最新影像
self.display_image(image)
self.latest_image = image # 存儲最新影像
self.display_image(image, self.view_origin)
def display_image(self, image):
""" 顯示影像到 QLabel (view_origin) """
image_bgr = cv2.cvtColor(image, cv2.COLOR_BayerBG2BGR) if len(image.shape) == 2 else image
def display_image(self, image, label):
""" 顯示影像到 QLabel """
try:
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) 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)
pixmap = QtGui.QPixmap.fromImage(qimage).scaled(label.size(), QtCore.Qt.KeepAspectRatio)
label.setPixmap(pixmap)
except Exception as e:
self.log_handler.write_log(f"⚠️ 顯示影像時發生錯誤: {e}")
def detection(self):
""" 使用 YOLO進行推論 """
if self.latest_image is None:
self.log_handler.write_log("⚠️ 無影像可進行推論")
return
if self.model is None:
self.log_handler.write_log("⚠️ YOLO 模型未載入,無法執行推論")
return
try:
# ✅ 轉換影像格式 (BGR → RGB)
image_rgb = cv2.cvtColor(self.latest_image, cv2.COLOR_BGR2RGB)
# ✅ 使用 YOLO 模型進行推論
results = self.model.predict(image_rgb, imgsz=640, conf=0.5) # 影像大小 640, 置信度閾值 0.5
self.log_handler.write_log("YOLO 推論完成")
# ✅ 取得標註結果
result_image = results[0].plot() # `plot()` 會回傳畫出標註的影像
# ✅ 顯示結果
self.display_image(result_image, self.view_predict)
except Exception as e:
self.log_handler.write_log(f"⚠️ 推論時發生錯誤: {e}")
def closeEvent(self, event):
reply = QtWidgets.QMessageBox.question(

View File

@ -16,3 +16,35 @@
[2025-03-10 21:04:32] 相機啟動
[2025-03-10 21:04:37] 相機停止
[2025-03-10 21:04:44] 程式關閉
[2025-03-10 21:17:09] 程式啟動
[2025-03-10 21:17:09] YOLO 模型載入成功
[2025-03-10 21:17:12] 相機啟動
[2025-03-10 21:20:22] 程式啟動
[2025-03-10 21:20:22] YOLO 模型載入成功
[2025-03-10 21:20:48] 相機啟動
[2025-03-10 21:21:02] 相機啟動
[2025-03-10 21:21:14] 程式關閉
[2025-03-10 21:21:21] 程式啟動
[2025-03-10 21:21:21] YOLO 模型載入成功
[2025-03-10 21:21:45] 相機啟動
[2025-03-10 21:24:41] 程式啟動
[2025-03-10 21:24:41] YOLO 模型載入成功
[2025-03-10 21:24:44] 相機啟動
[2025-03-10 21:25:21] 程式啟動
[2025-03-10 21:25:21] YOLO 模型載入成功
[2025-03-10 21:25:23] 相機啟動
[2025-03-10 21:25:36] 程式關閉
[2025-03-10 21:25:55] 程式啟動
[2025-03-10 21:25:55] YOLO 模型載入成功
[2025-03-10 21:26:03] 相機啟動
[2025-03-10 21:29:04] 程式啟動
[2025-03-10 21:29:04] YOLO 模型載入成功
[2025-03-10 21:29:06] 相機啟動
[2025-03-10 21:29:20] YOLO 推論完成
[2025-03-10 21:29:20] ⚠️ 推論時發生錯誤: display_image() takes 2 positional arguments but 3 were given
[2025-03-10 21:32:07] 程式啟動
[2025-03-10 21:32:07] YOLO 模型載入成功
[2025-03-10 21:32:12] 相機啟動
[2025-03-10 21:32:21] YOLO 推論完成
[2025-03-10 21:32:32] 相機停止
[2025-03-10 21:32:33] 程式關閉