jioaxi 1 tahun lalu
induk
melakukan
565641f197

TEMPAT SAMPAH
Day07/2.jpg


TEMPAT SAMPAH
Day07/best.pt


TEMPAT SAMPAH
Day07/datasets/labels/train.cache


TEMPAT SAMPAH
Day07/datasets/labels/val.cache


+ 1 - 0
Day07/jkApp/app.bat

@@ -0,0 +1 @@
+python  main.py

TEMPAT SAMPAH
Day07/jkApp/dev/__pycache__/camera.cpython-39.pyc


+ 32 - 0
Day07/jkApp/dev/camera.py

@@ -0,0 +1,32 @@
+from PyQt5.QtCore import QThread    # 引入多线程,设备是多个,一个设备一个任务
+import cv2
+# from cv2 import VideoCapture 
+
+# 1. 定义信号(引入)
+from PyQt5.QtCore import pyqtSignal
+
+class CameraDev(QThread):
+    # 定义信号(定义)
+    sig_video = pyqtSignal(bytes, int, int, int)  # 信号传递的数据(图像的二进制数据,字节序列(bytes), 图像高度int,宽度int,通道数int)
+    def __init__(self):
+        super(CameraDev, self).__init__()
+        # 开始视频抓取的任务初始化
+        # 初始化摄像头
+        self.cam = cv2.VideoCapture(
+            0, # 摄像头的编号,从0
+            cv2.CAP_DSHOW # 视频的处理调用DirectX 3D (DirectShow)
+        )
+
+    def run(self):
+        # 设备线程的任务,run结束,则任务结束
+        while True:
+            # 反复抓取视频处理
+            # print("设备准备工作!")
+            status, img = self.cam.read()  # 从摄像头读取图像
+            if status:
+                # print(img.shape)
+                # 显示图像
+                # 2. 发送信号
+                self.sig_video.emit(img.tobytes(), img.shape[0], img.shape[1], img.shape[2])
+            QThread.usleep(100000)  # 1000000微秒 = 1秒
+            

TEMPAT SAMPAH
Day07/jkApp/frame/__pycache__/app.cpython-39.pyc


TEMPAT SAMPAH
Day07/jkApp/frame/__pycache__/win.cpython-39.pyc


+ 11 - 0
Day07/jkApp/frame/app.py

@@ -0,0 +1,11 @@
+from PyQt5.QtWidgets import QApplication
+from frame.win import Win
+
+class App(QApplication):  # 扩展QApplication类
+    def __init__(self):   # 实现构造器
+        super(App, self).__init__([])   # 调用父类构造器(参数:命令行参数)
+
+        # 调用Win
+        self.win = Win()
+        self.win.show()
+        

+ 45 - 0
Day07/jkApp/frame/win.py

@@ -0,0 +1,45 @@
+from PyQt5.QtWidgets import QDialog
+from PyQt5.QtGui import QImage  #,QPxmap
+from PyQt5.QtGui import QPixmap  
+# 引入
+from ui.traffic_ui import Ui_Traffic
+
+from dev.camera import CameraDev
+
+class Win(QDialog):  # 扩展QDialog(新增,覆盖功能)
+    def __init__(self):  # 实现构造器(完成初始化,数据初始化,功能初始化)
+        super(Win, self).__init__()   # 调用父类构造器
+        # 调用ui
+        # 创建对象
+        self.ui = Ui_Traffic()  
+        # 关联ui到当前窗体
+        self.ui.setupUi(self)
+        
+        # 创建一个设备对象
+        self.dev = CameraDev()
+        # 启动设备线程工作
+        self.dev.start()
+
+        # 3. 绑定信号与槽。
+        self.dev.sig_video.connect(self.showVideo)
+
+    # 3. 定义槽(Slot)函数 (Qt技术:信号与槽),一定与信号同型
+    def showVideo(self, data, h, w, c):
+        # print("(",h, ",", w, ",",c, ")")  # python格式字符串
+        # 1. 使用data,h, w, c创建QImage
+        q_img = QImage(
+            data,    # 图像的字节数组
+            w,       # 图像宽度
+            h,       # 图像高度
+            w * c,   # 图像每行字节数
+            QImage.Format_BGR888   # 图像格式BGR,每个通道8个bit,1个字节
+        )
+        # 2. 使用QImage创建QPixmap
+        pix_img = QPixmap.fromImage(q_img)  # 自动从QImage转换为QPixmap,QLabel只支持QPixmap格式
+
+        # 3. 显示QLabel:lblVideo  
+        self.ui.lblVideo.setPixmap(pix_img)
+
+        # 4 适当的缩放
+        self.ui.lblVideo.setScaledContents(True)
+        

+ 4 - 0
Day07/jkApp/main.py

@@ -0,0 +1,4 @@
+from frame.app import App
+
+app = App()
+app.exec()  # 消息循环(程序循环处理操作系统发过来的消息)阻塞函数

TEMPAT SAMPAH
Day07/jkApp/mods/best.pt


TEMPAT SAMPAH
Day07/jkApp/ui/__pycache__/traffic_ui.cpython-39.pyc


+ 50 - 0
Day07/jkApp/ui/monItor_ui.py

@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+
+# Form implementation generated from reading ui file 'monitor.ui'
+#
+# Created by: PyQt5 UI code generator 5.15.9
+#
+# WARNING: Any manual changes made to this file will be lost when pyuic5 is
+# run again.  Do not edit this file unless you know what you are doing.
+
+
+from PyQt5 import QtCore, QtGui, QtWidgets
+
+
+class Ui_Dialog(object):
+    def setupUi(self, Dialog):
+        Dialog.setObjectName("Dialog")
+        Dialog.resize(488, 356)
+        self.label = QtWidgets.QLabel(Dialog)
+        self.label.setGeometry(QtCore.QRect(60, 50, 351, 201))
+        self.label.setStyleSheet("border-width:3px;\n"
+"border-style:solid;\n"
+"border-radius:10px;\n"
+"border-top-color:red;\n"
+"border-bottom-color:green;\n"
+"border-left-color:yellow;\n"
+"border-right-color:pink;\n"
+"")
+        self.label.setObjectName("label")
+        self.pushButton = QtWidgets.QPushButton(Dialog)
+        self.pushButton.setGeometry(QtCore.QRect(190, 270, 101, 31))
+        self.pushButton.setStyleSheet("border-width:1px;\n"
+"border-style:solid;\n"
+"border-radius:7px;\n"
+"border-top-color:#ffffff;\n"
+"border-left-color:#ffffff;\n"
+"border-bottom-color:#888888;\n"
+"border-right-color:#888888;\n"
+"\n"
+"")
+        self.pushButton.setObjectName("pushButton")
+
+        self.retranslateUi(Dialog)
+        QtCore.QMetaObject.connectSlotsByName(Dialog)
+
+    def retranslateUi(self, Dialog):
+        _translate = QtCore.QCoreApplication.translate
+        Dialog.setWindowTitle(_translate("Dialog", "智能交通监控系统"))
+        self.label.setText(_translate("Dialog", "视频显示区"))
+        self.pushButton.setText(_translate("Dialog", "处理视频"))
+        

+ 66 - 0
Day07/jkApp/ui/monitor.ui

@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Dialog</class>
+ <widget class="QDialog" name="Dialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>488</width>
+    <height>356</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>智能交通监控系统</string>
+  </property>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>60</x>
+     <y>50</y>
+     <width>351</width>
+     <height>201</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:3px;
+border-style:solid;
+border-radius:10px;
+border-top-color:red;
+border-bottom-color:green;
+border-left-color:yellow;
+border-right-color:pink;
+</string>
+   </property>
+   <property name="text">
+    <string>视频显示区</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton">
+   <property name="geometry">
+    <rect>
+     <x>190</x>
+     <y>270</y>
+     <width>101</width>
+     <height>31</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style:solid;
+border-radius:7px;
+border-top-color:#ffffff;
+border-left-color:#ffffff;
+border-bottom-color:#888888;
+border-right-color:#888888;
+
+</string>
+   </property>
+   <property name="text">
+    <string>处理视频</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 1 - 0
Day07/jkApp/ui/tools.bat

@@ -0,0 +1 @@
+pyuic5 -o traffic_ui.py traffic.ui 

+ 104 - 0
Day07/jkApp/ui/traffic.ui

@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Traffic</class>
+ <widget class="QDialog" name="Traffic">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>961</width>
+    <height>643</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>智能交通监控系统</string>
+  </property>
+  <widget class="QLabel" name="lblVideo">
+   <property name="geometry">
+    <rect>
+     <x>30</x>
+     <y>80</y>
+     <width>640</width>
+     <height>480</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style:solid;
+border-color:#FF0000;
+border-radius:10px;</string>
+   </property>
+   <property name="text">
+    <string>TextLabel</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="btnCapture">
+   <property name="geometry">
+    <rect>
+     <x>310</x>
+     <y>580</y>
+     <width>75</width>
+     <height>31</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>抓取图像</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>230</x>
+     <y>30</y>
+     <width>261</width>
+     <height>31</height>
+    </rect>
+   </property>
+   <property name="font">
+    <font>
+     <pointsize>24</pointsize>
+     <bold>true</bold>
+    </font>
+   </property>
+   <property name="text">
+    <string>智能交通监控系统</string>
+   </property>
+   <property name="alignment">
+    <set>Qt::AlignCenter</set>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_2">
+   <property name="geometry">
+    <rect>
+     <x>733</x>
+     <y>230</y>
+     <width>81</width>
+     <height>20</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>识别信息:</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="lblInfo">
+   <property name="geometry">
+    <rect>
+     <x>700</x>
+     <y>270</y>
+     <width>171</width>
+     <height>141</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style:dotted;
+boder-color:blue;</string>
+   </property>
+   <property name="text">
+    <string>显示信息</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 57 - 0
Day07/jkApp/ui/traffic_ui.py

@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+
+# Form implementation generated from reading ui file 'traffic.ui'
+#
+# Created by: PyQt5 UI code generator 5.15.9
+#
+# WARNING: Any manual changes made to this file will be lost when pyuic5 is
+# run again.  Do not edit this file unless you know what you are doing.
+
+
+from PyQt5 import QtCore, QtGui, QtWidgets
+
+
+class Ui_Traffic(object):
+    def setupUi(self, Traffic):
+        Traffic.setObjectName("Traffic")
+        Traffic.resize(961, 643)
+        self.lblVideo = QtWidgets.QLabel(Traffic)
+        self.lblVideo.setGeometry(QtCore.QRect(30, 80, 640, 480))
+        self.lblVideo.setStyleSheet("border-width:1px;\n"
+"border-style:solid;\n"
+"border-color:#FF0000;\n"
+"border-radius:10px;")
+        self.lblVideo.setObjectName("lblVideo")
+        self.btnCapture = QtWidgets.QPushButton(Traffic)
+        self.btnCapture.setGeometry(QtCore.QRect(310, 580, 75, 31))
+        self.btnCapture.setObjectName("btnCapture")
+        self.label = QtWidgets.QLabel(Traffic)
+        self.label.setGeometry(QtCore.QRect(230, 30, 261, 31))
+        font = QtGui.QFont()
+        font.setPointSize(24)
+        font.setBold(True)
+        self.label.setFont(font)
+        self.label.setAlignment(QtCore.Qt.AlignCenter)
+        self.label.setObjectName("label")
+        self.label_2 = QtWidgets.QLabel(Traffic)
+        self.label_2.setGeometry(QtCore.QRect(733, 230, 81, 20))
+        self.label_2.setObjectName("label_2")
+        self.lblInfo = QtWidgets.QLabel(Traffic)
+        self.lblInfo.setGeometry(QtCore.QRect(700, 270, 171, 141))
+        self.lblInfo.setStyleSheet("border-width:1px;\n"
+"border-style:dotted;\n"
+"border-color:blue;")
+        self.lblInfo.setObjectName("lblInfo")
+
+        self.retranslateUi(Traffic)
+        QtCore.QMetaObject.connectSlotsByName(Traffic)
+
+    def retranslateUi(self, Traffic):
+        _translate = QtCore.QCoreApplication.translate
+        Traffic.setWindowTitle(_translate("Traffic", "智能交通监控系统"))
+        self.lblVideo.setText(_translate("Traffic", "TextLabel"))
+        self.btnCapture.setText(_translate("Traffic", "抓取图像"))
+        self.label.setText(_translate("Traffic", "智能交通监控系统"))
+        self.label_2.setText(_translate("Traffic", "识别信息:"))
+        self.lblInfo.setText(_translate("Traffic", "显示信息"))
+        

+ 85 - 0
Day07/jkApp/ui/untitled.ui

@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>Dialog</class>
+ <widget class="QDialog" name="Dialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1364</width>
+    <height>948</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>250</x>
+     <y>110</y>
+     <width>251</width>
+     <height>171</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">qradialgradient
+border-width:1px;
+border-style:solid;
+border-color:green;</string>
+   </property>
+   <property name="text">
+    <string/>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton">
+   <property name="geometry">
+    <rect>
+     <x>330</x>
+     <y>320</y>
+     <width>93</width>
+     <height>28</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style:solid;
+border-left-color:#ffffff;
+border-top-color:#ffffff;
+border-right-color:#bbbbbb;
+border-bottom-color:#bbbbbb;</string>
+   </property>
+   <property name="text">
+    <string>抓取图像</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_2">
+   <property name="geometry">
+    <rect>
+     <x>620</x>
+     <y>150</y>
+     <width>72</width>
+     <height>15</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>识别信息:</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label_3">
+   <property name="geometry">
+    <rect>
+     <x>620</x>
+     <y>220</y>
+     <width>72</width>
+     <height>15</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>显示信息</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

TEMPAT SAMPAH
Day07/last.pt


+ 35 - 0
Day07/pred.py

@@ -0,0 +1,35 @@
+# 侦测
+from ultralytics import YOLO
+import cv2
+import numpy as np
+
+# 加载我们训练的模型
+model = YOLO("best.pt")
+# 预测结果
+result = model("zl_29.jpg")  # 利用模型侦测图片中的目标
+
+# print(result[0])   # [0]表示第一张图像的识别结果
+
+names = result[0].names # 类别对应的名字
+print(names)
+
+boxes = result[0].boxes  # 取一个目标
+
+cls = int(boxes.cls[0].cpu().item())   # 
+conf = boxes.conf[0].cpu().item()
+
+x1, y1, x2, y2 = boxes.xyxy[0].cpu().numpy().astype(np.int32)
+
+print(cls)              # 类别ID
+print(conf)             # 目标侦测成功的置信度 
+print(x1, y1, x2, y2)   # 侦测目标的区域
+print(names[cls])   # 获取侦测的目标名字
+
+
+# 把目标在图像中标注出来
+# 1. 打开图像
+img = cv2.imread("zl_29.jpg")
+# 2. 标注图像
+img = cv2.rectangle(img, (x1, y1), (x2, y2), color=(0, 0, 255), thickness=5)
+# 3. 保存图像
+cv2.imwrite("2.jpg", img)

+ 1 - 1
Day07/train.py

@@ -3,4 +3,4 @@ from ultralytics import YOLO
 
 if __name__ == "__main__":
     model = YOLO("yolov8n.pt")  # 创建一个模型,加载预训练的算子(coco数据集,imagenet数据集,voc)
-    model.train(data="dataset.yaml", epochs=2)
+    model.train(data="dataset.yaml", epochs=30)

TEMPAT SAMPAH
Day07/zl_29.jpg