当前位置:网站首页>【界面】pyqt5和Swin Transformer对人脸进行识别
【界面】pyqt5和Swin Transformer对人脸进行识别
2022-06-26 22:46:00 【努力的袁】
基于python,使用pyqt5模块和swin transformer检测算法对人脸进行识别
工具
语言:
python
主要库:
pyqt5
检测模型:
swin transformer
核心代码
import time, cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QFileDialog, QMainWindow
from PyQt5.QtCore import QTimer, QDateTime
from PyQt5.Qt import QThread, pyqtSignal, QMutex
import qimage2ndarray
from queue import Queue
from Project import Ui_Form
from infer import *
video_steam = Queue()
class Thread1(QThread): # 线程1
# 使用自定义信号,一定要记得信号是类变量,必须在类中定义,不能在实例方法中定义,
thread1_signal2 = pyqtSignal(object) #定义信号,定义参数为object类型
def __init__(self):
super(Thread1, self).__init__()
self.t = 0
self.Image_thread1 = None
self.mutex = QMutex() # 创建线程锁
self._isPause = False
def run(self):
while True:
self.mutex.lock() # 加锁
time.sleep(0.001) # 休眠
_, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.Image_thread1 = frame
self.thread1_signal2.emit(self.Image_thread1) # 传信号
self.mutex.unlock() # 解锁
class Thread2(QThread): # 线程2
thread1_signal3 = pyqtSignal(object)
def __init__(self):
super(Thread2, self).__init__()
self.t = 0
self.Image_thread1 = None
self.mutex = QMutex()
self._isPause = False
self.video_flag = 0
def run(self):
while True:
self.mutex.lock()
time.sleep(0.001)
# time.sleep(1 / self.a) 在这里改帧率
_, frame = cap.read()
config_file = 'mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py'
checkpoint_file = 'epoch_100.pth'
classes = ['The color of silica gel is abnormal', 'The color of silica gel is normal', 'The door is open',
'The door is close', 'breakage', 'dirt', 'rust', 'foreign object', 'oil leakage', 'animal',
'hat', 'person']
model = init_detector(config_file, checkpoint_file, device='cuda:0')
frame, result_label = InferResult(frame, config_file, checkpoint_file, classes, model)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if self.video_flag == 0:
video_steam.put(frame)
self.Image_thread1 = frame
self.thread1_signal3.emit(self.Image_thread1)
self.mutex.unlock()
class PyQtMainEntry(QMainWindow, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.mark = 0
self.btnReadImage.clicked.connect(self.btnReadImage_Clicked)
self.btnShowCamera.clicked.connect(self.btnOpenCamera_Clicked)
self.btnStartLabel.clicked.connect(self.startRecognize)
self.btnSaveResult.clicked.connect(self.resultsave)
self.time()
def btnReadImage_Clicked(self):
self.mark = 0
filename, _ = QFileDialog.getOpenFileName(self, '打开')
if filename:
self.captured = cv2.imread(str(filename))
self.captured = cv2.cvtColor(self.captured, cv2.COLOR_BGR2RGB)
rows, cols, channels = self.captured.shape
bytesPerLine = channels * cols
QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
self.Videolabel.setPixmap(QPixmap.fromImage(QImg).scaled(
self.Videolabel.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.Videolabel.setScaledContents(True)
def btnOpenCamera_Clicked(self):
self.mark = 1
self.process_thread = Thread2()
self.process_thread.thread1_signal3.connect(self.thread2_work2)
self.preview_thread = Thread1()
self.preview_thread.thread1_signal2.connect(self.thread1_work2)
self.preview_thread.start()
def thread1_work2(self, img):
self.Image = img
qimg = qimage2ndarray.array2qimage(img)
self.Videolabel.setPixmap(QPixmap(qimg))
self.Videolabel.show()
self.Videolabel.setScaledContents(True)
def thread2_work2(self, img):
self.Image = img
qimg = qimage2ndarray.array2qimage(img)
self.DetectImagelabel.setPixmap(QPixmap(qimg))
self.DetectImagelabel.show()
self.DetectImagelabel.setScaledContents(True)
self.Videolabel_2.setPixmap(QPixmap(qimg))
self.Videolabel_2.show()
self.Videolabel_2.setScaledContents(True)
def startRecognize(self):
if self.mark == 0:
img = self.captured
config_file = 'mask_rcnn_swin_tiny_patch4_window7_mstrain_480-800_adamw_1x_coco.py'
checkpoint_file = 'epoch_100.pth'
classes = ['The color of silica gel is abnormal', 'The color of silica gel is normal', 'The door is open',
'The door is close', 'breakage', 'dirt', 'rust', 'foreign object', 'oil leakage', 'animal',
'hat', 'person']
model = init_detector(config_file, checkpoint_file, device='cuda:0')
draw_1, result_label = InferResult(img, config_file, checkpoint_file, classes, model)
self.result = draw_1
draw_2 = qimage2ndarray.array2qimage(draw_1)
self.DetectImagelabel.setPixmap(QPixmap(draw_2))
self.DetectImagelabel.setScaledContents(True)
self.DetectImagelabel.show()
else:
self.process_thread.start()
# 显示时间
def showCurrentTime(self, timeLabel):
time = QDateTime.currentDateTime()
self.timeDisplay = time.toString('yyyy-MM-dd hh:mm:ss dddd')
timeLabel.setText(self.timeDisplay)
def time(self):
self.timer = QTimer()
self.timer.timeout.connect(lambda: self.showCurrentTime(self.label_2))
self.timer.start()
def resultsave(self):
path_filename = QFileDialog.getExistingDirectory(self, '结果保存')
if path_filename:
self.saveImage = cv2.cvtColor(self.result, cv2.COLOR_RGB2BGR)
cv2.imwrite(path_filename + '/' + self.timeDisplay[:10]
+ '_' + str(10) + '.png', self.saveImage)
# 显示路径
self.PathLineEdit.setText(path_filename)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
cap = cv2.VideoCapture(0)
window = PyQtMainEntry()
window.show()
sys.exit(app.exec_())
运行结果

源代码
边栏推荐
- Homebrew installation in MacOS environment [email protected]
- Pass note 【 dynamic planning 】
- Which platform is the safest for buying stocks and opening accounts? Ask for sharing
- Share three methods of automatic summation in Excel
- Flutter 中 ValueNotifier<List<T>> 监听问题解决
- MacOS环境下使用HomeBrew安装[email protected]
- [hybrid programming JNI] details of JNA in Chapter 11
- Different subsequence problems I
- What is the “ How to remove a custom form list?
- 开放世界机甲游戏-Phantom Galaxies
猜你喜欢

leetcode:6103. 从树中删除边的最小分数【dfs + 联通分量 + 子图的值记录】
![How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]](/img/ec/1c324dcf38d07742a139aac2bab02e.png)
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
![[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]](/img/e8/6342f2dc6e7f06a847852ce4b40719.jpg)
[fundamentals of image processing] GUI image curve adjustment system based on MATLAB [including Matlab source code 1923]

数据清洗工具flashtext,效率直接提升了几十倍数

【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】

DLA model (classification model + improved segmentation model) + deformable convolution

The sharp sword of API management -- eolink

Word chess based on heuristic search

Comprehensive evaluation of online collaboration documents: note, flowus, WOLAI, Feishu, YuQue, Microsoft office, Google Docs, Jinshan docs, Tencent docs, graphite docs, Dropbox paper, nutcloud docs,

Leetcode (452) - detonate the balloon with the minimum number of arrows
随机推荐
Introduction of classic wide & deep model and implementation of tensorflow 2 code
Detailed explanation of nmap parameters
Pass note 【 dynamic planning 】
在Flutter中解析复杂的JSON
Operator介绍
美术向的Unity动画知识
Operator介紹
Vulnhub's DC8
Using C to operate SQLSERVER database through SQL statement tutorial
Which securities company is the most convenient, safe and reliable for opening an account
YOLOv6:又快又准的目標檢測框架開源啦
MacOS環境下使用HomeBrew安裝[email protected]
【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】
nmap参数详解
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
Yolov6: the fast and accurate target detection framework is open source
简述unity的模型动画功能
Unity method for setting material and shader
Is it safe to buy stocks and open accounts through the account QR code of the CICC securities manager? Want to open an account for stock trading