jie před 1 rokem
rodič
revize
cf3be64c90

+ 1 - 0
Day05/aiapp/app.bat

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

binární
Day05/aiapp/dev/__pycache__/camera.cpython-39.pyc


+ 52 - 0
Day05/aiapp/dev/camera.py

@@ -0,0 +1,52 @@
+from PyQt5.QtCore import QThread    # 引入多线程,设备是多个,一个设备一个任务
+import cv2
+import numpy as np
+# 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)
+        )
+        self.isOver = False
+
+    def run(self):
+        kernel = np.array([  # 深度学习就是找到一个kernel是的特征对分类有效
+            [0, -2, 0],
+            [-2, 8, -2],
+            [0, -2, 0]
+        ])
+        # 设备线程的任务,run结束,则任务结束
+        while not self.isOver:
+            # 反复抓取视频处理
+            # print("设备准备工作!")
+            status, img = self.cam.read()  # 从摄像头读取图像
+            if status:
+                # print(img.shape)
+                # 显示图像
+                # 调用人工智能模块,进行图像识别(处理)
+                # img = cv2.GaussianBlur(img, (3, 3), 2.0)
+                img = cv2.filter2D(img, -1, kernel, delta=200.0)
+                # 2. 发送信号
+                self.sig_video.emit(img.tobytes(), img.shape[0], img.shape[1], img.shape[2])
+            QThread.usleep(100000)  # 1000000微秒 = 1秒
+    
+    def close(self):
+        # 停止多任务
+        self.isOver = True
+        while self.isRunning():
+            pass
+
+        print("线程终止")
+        # 释放设备
+        self.cam.release()
+        print("设备释放")

binární
Day05/aiapp/frame/__pycache__/app.cpython-39.pyc


binární
Day05/aiapp/frame/__pycache__/win.cpython-39.pyc


+ 10 - 0
Day05/aiapp/frame/app.py

@@ -0,0 +1,10 @@
+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()

+ 50 - 0
Day05/aiapp/frame/win.py

@@ -0,0 +1,50 @@
+from PyQt5.QtWidgets import QDialog
+from PyQt5.QtGui import QImage
+from PyQt5.QtGui import QPixmap
+# 引入
+from ui.traffic_ui import Ui_Dialog
+
+from dev.camera import CameraDev
+
+
+class Win(QDialog):  #扩展QDialog(新增,覆盖功能)
+    def __init__(self):  # 实现构造器 (完成初始化,数据初始化,功能初始化)
+        super(Win,self).__init__()  #调用父类构造器
+        # 调用ui
+        #创建对象
+        self.ui = Ui_Dialog()
+        # 关联ui到当前窗体
+        self.ui.setupUi(self)
+
+        # 创建一个设备对象
+        self.dev = CameraDev()
+        #启动设备线程工作
+        self.dev.start()
+
+        # 绑定信号与槽。
+        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只支持oPixmap格式
+        # 3.显示0Label: 1blvideo
+        self.ui.Yi.setPixmap(pix_img)
+        # 4. 适当的缩放
+        self.ui.Yi.setScaledContents(True)
+
+    def closeEvent(self,e): # 当窗体关闭前会调用
+        # 关闭摄像头
+        # 释放设备 
+        # 停止多任务   
+        self.dev.close()
+        print("关闭")           

+ 9 - 0
Day05/aiapp/main.py

@@ -0,0 +1,9 @@
+import PyQt5.QtCore
+from frame.app import App
+
+PyQt5.QtCore.QCoreApplication.setAttribute(PyQt5.QtCore.Qt.AA_EnableHighDpiScaling)
+app = App()
+
+app.exec()  # 消息循环 (程序循环处理操作系统发过来的消息) 阻塞函数
+
+print("程序正常终止")

binární
Day05/aiapp/ui/__pycache__/traffic_ui.cpython-39.pyc


+ 2 - 0
Day05/aiapp/ui/tools.bat

@@ -0,0 +1,2 @@
+@rem @符号在执行命令,不回显命令行
+@pyuic5 -o traffic_ui.py traffic.ui

+ 120 - 0
Day05/aiapp/ui/traffic.ui

@@ -0,0 +1,120 @@
+<?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>793</width>
+    <height>613</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <widget class="QLabel" name="Yi">
+   <property name="geometry">
+    <rect>
+     <x>20</x>
+     <y>110</y>
+     <width>521</width>
+     <height>361</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:2px;
+border-style:solid;
+border-color:#FF0000;
+border-radius:10px;</string>
+   </property>
+   <property name="text">
+    <string>视频显示区</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="btnCapture">
+   <property name="geometry">
+    <rect>
+     <x>190</x>
+     <y>490</y>
+     <width>91</width>
+     <height>31</height>
+    </rect>
+   </property>
+   <property name="sizeIncrement">
+    <size>
+     <width>0</width>
+     <height>0</height>
+    </size>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style:solid;
+border-color:gray;
+border-radius:10px;</string>
+   </property>
+   <property name="text">
+    <string>抓取图像</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton">
+   <property name="geometry">
+    <rect>
+     <x>290</x>
+     <y>10</y>
+     <width>241</width>
+     <height>51</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">font: 14pt &quot;Agency FB&quot;;
+border-width:3px;
+border-style:solid;
+border-color:black;
+border-radius:10px;</string>
+   </property>
+   <property name="text">
+    <string>智能交通监控系统</string>
+   </property>
+  </widget>
+  <widget class="QLabel" name="label">
+   <property name="geometry">
+    <rect>
+     <x>580</x>
+     <y>300</y>
+     <width>191</width>
+     <height>171</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:2px;
+border-style:dotted;
+border-color:blue;</string>
+   </property>
+   <property name="text">
+    <string>识别信息</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton_2">
+   <property name="geometry">
+    <rect>
+     <x>630</x>
+     <y>490</y>
+     <width>81</width>
+     <height>31</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:1px;
+border-style:solid;
+border-color:gray;
+border-radius:10px;</string>
+   </property>
+   <property name="text">
+    <string>显示信息</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 53 - 0
Day05/aiapp/ui/traffic_ui.py

@@ -0,0 +1,53 @@
+# -*- 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_Dialog(object):
+    def setupUi(self, Dialog):
+        Dialog.setObjectName("Dialog")
+        Dialog.resize(400, 300)
+        self.Yi = QtWidgets.QLabel(Dialog)
+        self.Yi.setGeometry(QtCore.QRect(10, 70, 261, 191))
+        self.Yi.setStyleSheet("border-width:1px;\n"
+"border-style:solid;\n"
+"border-color:#FF0000;\n"
+"border-radius:10px;")
+        self.Yi.setObjectName("Yi")
+        self.btnCapture = QtWidgets.QPushButton(Dialog)
+        self.btnCapture.setGeometry(QtCore.QRect(100, 270, 75, 23))
+        self.btnCapture.setSizeIncrement(QtCore.QSize(0, 0))
+        self.btnCapture.setObjectName("btnCapture")
+        self.pushButton = QtWidgets.QPushButton(Dialog)
+        self.pushButton.setGeometry(QtCore.QRect(190, 10, 111, 23))
+        self.pushButton.setStyleSheet("font: 9pt \"Bahnschrift SemiLight\";")
+        self.pushButton.setObjectName("pushButton")
+        self.label = QtWidgets.QLabel(Dialog)
+        self.label.setGeometry(QtCore.QRect(290, 140, 81, 81))
+        self.label.setStyleSheet("border-width:1px;\n"
+"border-style:dotted;\n"
+"border-color:blue;")
+        self.label.setObjectName("label")
+        self.pushButton_2 = QtWidgets.QPushButton(Dialog)
+        self.pushButton_2.setGeometry(QtCore.QRect(290, 90, 75, 23))
+        self.pushButton_2.setObjectName("pushButton_2")
+
+        self.retranslateUi(Dialog)
+        QtCore.QMetaObject.connectSlotsByName(Dialog)
+
+    def retranslateUi(self, Dialog):
+        _translate = QtCore.QCoreApplication.translate
+        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
+        self.Yi.setText(_translate("Dialog", "TextLabel"))
+        self.btnCapture.setText(_translate("Dialog", "抓取图像"))
+        self.pushButton.setText(_translate("Dialog", "智能交通监控系统"))
+        self.label.setText(_translate("Dialog", "TextLabel"))
+        self.pushButton_2.setText(_translate("Dialog", "PushButton"))