Joker 1 рік тому
батько
коміт
a06b413d1f

+ 1 - 0
day5/AIAPP/app.bat

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

BIN
day5/AIAPP/dev/__pycache__/camera.cpython-39.pyc


+ 31 - 0
day5/AIAPP/dev/camera.py

@@ -0,0 +1,31 @@
+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秒

BIN
day5/AIAPP/frame/__pycache__/app.cpython-39.pyc


BIN
day5/AIAPP/frame/__pycache__/win.cpython-39.pyc


+ 10 - 0
day5/AIAPP/frame/app.py

@@ -0,0 +1,10 @@
+from PyQt5.QtWidgets import QApplication
+from frame.win import Win
+
+class App(QApplication): 
+    def __init__(self):
+       super(App,self).__init__([])
+
+       #调用win
+       self.win =Win()
+       self.win.show()

+ 44 - 0
day5/AIAPP/frame/win.py

@@ -0,0 +1,44 @@
+from PyQt5.QtWidgets import QDialog
+from PyQt5.QtGui import QImage  #,QPxmap
+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()
+
+        # 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.Traffic.setPixmap(pix_img)
+
+        # 4 适当的缩放
+        self.ui.Traffic.setScaledContents(True)

+ 7 - 0
day5/AIAPP/main.py

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

BIN
day5/AIAPP/ui/__pycache__/traffic_ui.cpython-39.pyc


+ 2 - 0
day5/AIAPP/ui/tools.bat

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

+ 63 - 0
day5/AIAPP/ui/traffic.ui

@@ -0,0 +1,63 @@
+<?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>512</width>
+    <height>467</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>智能交通监控系统</string>
+  </property>
+  <widget class="QLabel" name="Traffic">
+   <property name="geometry">
+    <rect>
+     <x>40</x>
+     <y>40</y>
+     <width>431</width>
+     <height>211</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:2px;
+border-style:solid;
+border-radius:10px;
+border-top-color:black;
+border-left-color:red;
+border-botton-color:green;
+border-right-color:red;</string>
+   </property>
+   <property name="text">
+    <string>TextLabel</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="btnCapture">
+   <property name="geometry">
+    <rect>
+     <x>180</x>
+     <y>340</y>
+     <width>181</width>
+     <height>91</height>
+    </rect>
+   </property>
+   <property name="styleSheet">
+    <string notr="true">border-width:2px;
+border-style:solid;
+border-radius:5px;
+border-top-color:#ffffff;
+border-left-color:#ffffff;
+border-botton-color:#88888888;
+border-right-color:#bbbbbb;</string>
+   </property>
+   <property name="text">
+    <string>PushButton</string>
+   </property>
+  </widget>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 46 - 0
day5/AIAPP/ui/traffic_ui.py

@@ -0,0 +1,46 @@
+# -*- 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(512, 467)
+        self.Traffic = QtWidgets.QLabel(dialog)
+        self.Traffic.setGeometry(QtCore.QRect(40, 40, 431, 211))
+        self.Traffic.setStyleSheet("border-width:2px;\n"
+"border-style:solid;\n"
+"border-radius:10px;\n"
+"border-top-color:black;\n"
+"border-left-color:red;\n"
+"border-botton-color:green;\n"
+"border-right-color:red;")
+        self.Traffic.setObjectName("Traffic")
+        self.btnCapture = QtWidgets.QPushButton(dialog)
+        self.btnCapture.setGeometry(QtCore.QRect(180, 340, 181, 91))
+        self.btnCapture.setStyleSheet("border-width:2px;\n"
+"border-style:solid;\n"
+"border-radius:5px;\n"
+"border-top-color:#ffffff;\n"
+"border-left-color:#ffffff;\n"
+"border-botton-color:#88888888;\n"
+"border-right-color:#bbbbbb;")
+        self.btnCapture.setObjectName("btnCapture")
+
+        self.retranslateUi(dialog)
+        QtCore.QMetaObject.connectSlotsByName(dialog)
+
+    def retranslateUi(self, dialog):
+        _translate = QtCore.QCoreApplication.translate
+        dialog.setWindowTitle(_translate("dialog", "智能交通监控系统"))
+        self.Traffic.setText(_translate("dialog", "TextLabel"))
+        self.btnCapture.setText(_translate("dialog", "PushButton"))