话不多说,先上界面:
前情提示:做这个例子,需要USB摄像头已经调好了,同时电脑上已经安好了python、pyqt5、opencv等,下面演出开始吧。。。。直接上代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'F:\源代码\video.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon, QPixmap, QPainter, QPen
from PyQt5.QtWidgets import *
import threading
from threading import *
import sys
import cv2
import requests
import numpy as np
from PyQt5.Qt import Qt
import time
class Ui_video(object):
def setupUi(self, video):
video.setObjectName("video")
video.resize(1200, 788)
self.label = QtWidgets.QLabel(video)
self.label.setGeometry(QtCore.QRect(0, -2, 1200, 788))
self.label.setText("")
self.label.setObjectName("label")
pixmap = QPixmap('2.png')
self.label.setPixmap(pixmap)
self.label.setScaledContents (True)
self.groupBox = QtWidgets.QGroupBox(video)
self.groupBox.setGeometry(QtCore.QRect(40, 50, 1120, 688))
font = QtGui.QFont()
font.setPointSize(10)
self.groupBox.setFont(font)
self.groupBox.setObjectName("groupBox")
self.pushButton = QtWidgets.QPushButton(self.groupBox)
self.pushButton.setGeometry(QtCore.QRect(840, 490, 200, 48))
font = QtGui.QFont()
font.setPointSize(10)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_2.setGeometry(QtCore.QRect(840, 230, 200, 50))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_4 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_4.setGeometry(QtCore.QRect(840, 360, 200, 50))
self.pushButton_4.setObjectName("pushButton_4")
self.label_2 = QtWidgets.QLabel(self.groupBox)
self.label_2.setGeometry(QtCore.QRect(58, 80, 300, 50))
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit.setGeometry(QtCore.QRect(370, 80, 400, 48))
self.lineEdit.setObjectName("lineEdit")
self.pushButton_5 = QtWidgets.QPushButton(self.groupBox)
self.pushButton_5.setGeometry(QtCore.QRect(840, 80, 200, 50))
self.pushButton_5.setObjectName("pushButton_5")
self.label_3 = QtWidgets.QLabel(self.groupBox)
self.label_3.setGeometry(QtCore.QRect(50, 160, 738, 500))
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.retranslateUi(video)
self.pushButton.clicked.connect(self.pushButtonClick)
self.pushButton_2.clicked.connect(self.pushButton_2Click)
self.pushButton_4.clicked.connect(self.pushButton_4Click)
self.pushButton_5.clicked.connect(self.pushButton_5Click)
self.Video1=''
self.flag=1
self.flag0=1
QtCore.QMetaObject.connectSlotsByName(video)
def retranslateUi(self, video):
_translate = QtCore.QCoreApplication.translate
video.setWindowTitle(_translate("video", "IMX6ULL摄像头传输"))
video.setWindowIcon(QIcon('1.png'))
self.groupBox.setTitle(_translate("video", "数据处理区"))
self.pushButton_2.setText(_translate("video", "视频接收"))
self.pushButton_4.setText(_translate("video", "视频断开"))
self.label_2.setText(_translate("video", "请输入IMX6ULL的IP地址:"))
self.pushButton_5.setText(_translate("video", "连 接"))
self.pushButton.setText(_translate("video", "关闭窗口"))
def ShowVideo(self):
Video1=self.Video1
#Video1='192.168.0.102'
print(Video1)
while self.flag == 1:
url1 = 'http://' + Video1 + ':8080/?action=snapshot'
try:
if self.flag0==1:
req = requests.get(url1).content
print('222')
pix = QPixmap()
pix.loadFromData(req)
image = np.asarray(bytearray(req), dtype="uint8")
img = cv2.imdecode(image, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print('333')
classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
print('444')
result = classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
a = len(result)
print(a)
if len(result) > 0:
painter = QPainter(pix)
pen = QPen(Qt.red, 2)
painter.setPen(pen)
face_num = len(result)
for i in range(face_num):
x = int(result[i][0])
y = (result[i][1])
width = (result[i][2])
height = (result[i][3])
painter.drawRect(x, y, width, height)
painter.drawText(x, y, "People")
painter.end()
else:
pix=QPixmap('1.jpg')
print('000')
except:
print('999')
break
self.label_3.setPixmap(pix)
self.label_3.setScaledContents (True)
time.sleep(0.5)
def Video(self):
try:
Video_thread = Thread(target=self.ShowVideo)
Video_thread.setDaemon(True)
self.flag=1
self.flag0=1
Video_thread.start() # 启动线程
except Exception as e:
print(e)
def pushButtonClick(self):
video.close()
def pushButton_2Click(self):
self.Video()
def pushButton_4Click(self):
self.flag=0
self.ShowVideo()
def pushButton_5Click(self):
self.Video1=self.lineEdit.text()
if len(self.Video1)==0:
QtWidgets.QMessageBox.information(video, "信息提示", "视频IP不能为空!")
else:
self.flag=0
QtWidgets.QMessageBox.information(video, "信息提示", "连接成功,可以正常操作。")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
video = QtWidgets.QWidget()
ui = Ui_video()
ui.setupUi(video)
video.show()
sys.exit(app.exec_())
代码中有好多不规范的地方,上图中也应该是人脸检测,见谅。。。。不多做解释了,下次是一起一点一点做界面呢。。。。还是上带口罩检测呢。。。。再说吧,先玩儿会去。。。。