Python pytorch yolov5 google colab 口罩佩戴检测
colab 上的训练在这里

目前有用的网络视频流地址:伊拉克电视台

rtmp://ns8.indexforce.com/home/mystream

新闻视频

https://vd2.bdstatic.com/mda-kbitdw1fp321irs1/v1-cae/sc/mda-kbitdw1fp321irs1.mp4?v_from_s=hkapp-haokan-suzhou&auth_key=1647140033-0-0-5b3b43f203e8f57d4a2e34627da9f137&bcevod_channel=searchbox_feed&pd=1&cd=0&pt=3&logid=1432873267&vid=2466983731079509186&abtest=100815_2-17451_2&klogid=1432873267%20#%20%E5%BA%94%E8%AF%A5%E5%9C%A8%E7%95%8C%E9%9D%A2%E5%90%AF%E5%8A%A8%E7%9A%84%E6%97%B6%E5%80%99%E5%B0%B1%E5%B0%86%E6%A8%A1%E5%9E%8B%E5%8A%A0%E8%BD%BD%E5%87%BA%E6%9D%A5%EF%BC%8C%E8%AE%BE%E7%BD%AEtmp%E7%9A%84%E7%9B%AE%E5%BD%95%E6%9D%A5%E6%94%BE%E4%B8%AD%E9%97%B4%E7%9A%84%E5%A4%84%E7%90%86%E7%BB%93%E6%9E%9C

这可能是最后一次训练了


训练结果参考另一篇的 exp27
本来以为还要重新训练过的,但是改了一下两个阈值发现效果不错,置信度改成 0.5,iou 改成 0.3 左右。这样可以把一些把手当成鼻子背景当成口罩 and 一些一张脸多个框的框给过滤掉,因为这次训练本身就把清楚的人头都能标到 0.9+的置信度,所以置信度阈值其实还可以再提高一些,因为有些背景还是会有大概 0.79 会被识别成口罩。其实可以多加一些环境的负样本,比如手或是一些模糊背景进去再训练,但是本人比较懒,加上手机拍摄照片的格式还得转换成 jpg,且 mac 不能通过修改扩展名来改变格式。。。所以训练暂时告一段落。

2022.4.22 在 colab 上从 0 开始训练 100 轮


!nvidia-smi
import torch
torch.__version__
!pip3 install torchvision
import os
from google.colab import drive
drive.mount('/content/drive')
path = "/content/drive/My Drive"os.chdir(path)
os.listdir(path)
%cd yolov5
!python train.py --data data/data.yaml --cfg models/yolov5s.yaml --weights '' --epochs 100 --batch-size 64

忘记改 Detect 的 thickness 和关闭置信度显示了,所以人多的图有点乱。

!python detect.py --source ../mask4/train/images --weights 'runs/train/exp23/weights/best.pt'

原文前言

目标检测领域19个建议

  • 训练日志在另一篇中。
  • Mac 上,首先读取子目录会有隐藏的.DS_store文件,在终端运行删除这些。
    find ./ -name ".DS_Store" -depth -exec rm {} \;

    • 查找了永久不再生成这个文件的指令,重启后好像并没有用。。所以每次创文件夹还得删一次。
      原文
  • mac 上右键文件按住 option 会出现将此文件拷贝为文件路径可以直接复制文件路径。

labelimg 重新标注时报错超过 index 范围

  • labelImg 的使用注意

    • 修改数据框后再点下一张的时候 Python 直接退出,报错index 超出 list,查看 classes 发现少了一个类,加回去后才能上下查看图片。解决:“修改安装路径下data文件夹下的默认predefined_classes.txt,将其中的类删除换成自己的类”
    • 上面的问题之前是解决了的,但是后来又去修改时还是会报错,不知道是不是只是 mac 的问题,就是我后面想要修改时,如果只在一张图上修改一个标签,那么 classes 的文件就会只出现这一个标签,如果在一张图中修改了两种标签,那么标签的名字会按顺序写入 classes,所以解决方法就是找一张图按顺序添加两个标签,再删掉就行了。(因为我只有两个类,太多的话就 gg 只能第一次就要标注好咯)

代码

#     find ./ -name ".DS_Store" -depth -exec rm {} \;
import numpy as np
import cv2
import os
from tqdm import tqdm
label=np.zeros( (6) )
data_=np.zeros((6,512,512))
array_img = []
def image(path):print("-" * 50)print("训练集读取")'''读取路径下所有子文件夹中的图片并存入list'''train = []dir_counter = 0x=0i=0h=-1for child_dir in os.listdir(path):child_path = os.path.join(path, child_dir)h += 1for dir_image in tqdm(os.listdir(child_path)):img = cv2.imread(child_path + "//" + dir_image, cv2.IMREAD_COLOR)   # 斜杆\改成/#cv2.namedWindow("Image")# cv2.imshow("Image", img)img=cv2.resize(img,(512,512))img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#灰度处理data=np.zeros((512,512))data[0:img.shape[0],0:img.shape[1]] = imgdata_[i, :, :] = data[0:512, 0:512]label[x] = hi += 1x += 1dir_counter += 1train.append(label)train.append(data_)return train
image('/Users/sinkarsenic/Downloads/mask/val/mask/test')
print('data_')
print(data_)
print('label')
print(label)

删完隐藏文件后还是报错,改了参数啥的 也没变好,debug发现是图片没读出来,刚好看到这篇文章给了我启发。
关于 NoneType : object has no attribute shape

把斜杆\改成/就可以了
但是这一段只是打印出标签并没有创建…
所以
mac使用python脚本快捷键创建txt文本
学会了 mac 的自动化操作
发现这段代码可以加点自己写的

def createtext():path_name = os.getcwd()#path_name = raw_input()path_name += '/'text_name = '新文本_' # + str(random.randint(0, 65536))completeName = path_name + text_name + ".txt"file1 = open(completeName , "w")init_text = "Hello world!"file1.write(init_text)file1.close()file_name("/Users/sinkarsenic/Downloads/mask/val/mask/test/nomask")

批量生成YOLO 格式标签

<object-class> <x> <y> <width> <height>
x,y是目标的中心坐标,width,height是目标的宽和高。
这些坐标是通过归一化的,其中x,width是使用原图的width进行归一化;
而y,height是使用原图的height进行归一化。

创建txt 的时候要获取图片名称,把.jpg去掉发现还有个空的数据,不去掉发现又是那个 dsstore!

于是加了个 if 判断处理后的文件长度,排除掉空的再生成 txt。

def file_name(file_dir):for root, dirs, files in os.walk(file_dir):#print(root) #当前目录路径#print(dirs) #当前路径下所有子目录for file in files:file = file.split('.')[0]if len(file)!=0:path_name = rootpath_name += '/'text_name = filecompleteName = path_name + text_name + ".txt"file1 = open(completeName, "w")init_text = "0 0.500952 0.501429 0.998095 0.997143"# 整张图片file1.write(init_text)file1.close()print(len(file)) #当前路径下所有非目录子文件

批量给图片改名字加前缀

  • 源代码是加前缀就行了,但我发现我的数据集原名字有点长,干脆都改成数字。
import os
import syspath = "/Users/sinkarsenic/Downloads/mask/val/mask/test/nomask"
postfix = ".jpg"  #只改后缀为这个的
mark = 'm_'  #'nm_'  # 前缀名def add_prefix_files():oldnames = os.listdir(path)index = 0for oldname in oldnames:if oldname!=sys.argv[0]:#if oldname.endswith(postfix):如果文件夹中只有图片得注释掉不然只会修改固定后缀的图片os.rename(os.path.join(path,oldname),os.path.join(path,mark + str(index) + '.jpg'))print(oldname,"has been renamed successfully! New name is: ",mark+str(index) + '.jpg')index=index+1

本来以为要卡一会,没想到 600 多图片转眼就重命名完了。

批量在 txt 内容前面加上标签数字 0 和 1

def add_mark():with open('/Users/sinkarsenic/Downloads/mask/val/mask/test/nomask/m_0.txt', 'r+') as f:content = f.read()f.seek(0, 0)f.write('0'+" "+content)
  • 然后发现我已经打好标签了全都是 0,现在需要的是修改第一个数字而不是加数字。。。

批量将数据集 txt 中的第一个数字替换

def replace_mark():if __name__ == '__main__':txt_list = glob.glob("/Users/sinkarsenic/Downloads/mask/test/test/*.txt")for txt_item in txt_list:with open(txt_item) as f:lines = f.readlines()with open(txt_item, 'w') as f:for line in lines:line_split = line.strip().split()line_split[0] = '1'   #替换的数字f.write(line_split[0] + ' ' +line_split[1] + " " +line_split[2] + " " +line_split[3] + " " +line_split[4]+'\n')passreplace_mark()

然后只修改带有 m_前缀的文件名加一个 if

if mark not in os.path.split(txt_item)[1]:

(attention:not in 是包括 classes.txt 的)

批量将 voc 数据集改成 yolo 格式

  • 网上找的代码好像有点错误…有些框会丢失,所以干脆自己写个自动点击代码,在 labelimg 上直接自己点。
  • 写无限循环的时候千万千万千万要设个条件 break…因为 mac 系统还不知道怎么通过键盘输入来暂停,而且鼠标不在 pycharm 上按也没用…只能算下大概要点多少次设个 break…之前没设的两次真是费了老大劲才关闭(还好只设了间隔 0.5 秒手速快还是能关的…)mac 这玩意连休眠再启动它都还在点的…
import pyautogui
import time
import cv2#time.sleep(4)
x, y = pyautogui.position()
print(x, y)
next = (42,274)
change = (42,446)
save = (42,393)
other = (193,239)
i = 0
while 1:i = i+1pyautogui.leftClick(other)time.sleep(0.1)pyautogui.leftClick(change)time.sleep(0.1)pyautogui.leftClick(save)time.sleep(0.1)pyautogui.leftClick(next)if i == 680:break
  • 用 labelImg 把 xml 转成 txt 后,xml 和 txt 在同一个文件夹,于是又写代码批量将 xml 文件删掉。
import osfilename = "/Users/sinkarsenic/Downloads/mask_detect/archive/annotations"for root, dirs, files in os.walk(filename):#print(root) #当前目录路径#print(dirs) #当前路径下所有子目录for file in files:post = file.split('.')[1]prefix = file.split('.')[0]if post=="xml":#print(prefix)delete_path = os.path.join(filename, prefix + ".xml")#print(delete_path)os.remove(delete_path)

将爬取的文件夹中的所有文件夹内容批量拷贝到一个文件夹中

  • 下载的一个数据集中有很多个以名字命名的文件夹,每个文件夹里有几张图片,想把所有图片都整在一起。
#将爬取的文件夹中的所有文件夹内容批量拷贝到一个文件夹中
import shutil
import os
copypath = "/Users/sinkarsenic/Downloads/mask_detect/mask3/without_mask"
movepath1 = "/Users/sinkarsenic/Downloads/mask_detect/mask3/without"
for root, dirs, files in os.walk(copypath, topdown=False):for name in files:# print(os.path.join(root, name))filepath=os.path.join(root, name) #文件存放路径movepath = os.path.join(movepath1, name)  # movepath:指定移动文件夹shutil.copyfile(filepath,movepath)

YOLOv5 在 colab 上

人脸检测负样本下载可能得科学上

  • 修改 train.py 中的weights(pt)和 data(yaml )
  • 基于官网的预训练模型继续训练
  • Colab运行YOLOv5
$ python train.py --img 640 --batch 16 --epochs 300 --data ../orange-detection1/data.yaml --cfg models/yolov5s.yaml --weights yolov5s.pt
  • 基于训练得到的best.pt模型继续训练
$ python train.py --img 640 --batch 16 --epochs 300 --data ../orange-detection1/data.yaml --cfg models/yolov5s.yaml --weights best.pt

my

!python train.py --data data/data.yaml --cfg models/yolov5s.yaml --weights 'runs/train/exp18/weights/best.pt' --epochs 100 --batch-size 64

中断训练后通过设置 resume 继续训练

  • colab训练太久会断开,没钱升级 pro。

!python train.py --data data/data.yaml --cfg models/yolov5s.yaml
–weights ‘runs/train/exp11/weights/best.pt’ --epochs 150 --batch-size 64

Yolov5如何在训练意外中断后接续训练
PyTorch三种保存方法 模型保存和继续训练的完整例程
How to prevent Google Colab from disconnecting ?

pytorch 保存模型

best_model = '/models/yolov5s.yaml'
PATH = './runs/train/exp5/weights/best.pth'
torch.save(best_model, PATH)

Google Colab 保存和恢复模型(Pytorch)

发现这个PyTorch实现断点继续训练

detect

YOLOV5检测代码detect.py注释与解析

  • 测试
    执行成功后,会显示检测结果保存路径,并在保存路径(yolov5s/runs/detect)下查看检测图片的结果。
python detect.py --weight weights/best.pt --source ../orange-detection1/test/images/图片名

my

!python detect.py --source ../mask/valid/images --weights 'runs/train/exp13/weights/best.pt'

Yolov3模型没有框的解决方案之——提高训练模型的置信度
深度学习中“过拟合”的产生原因和解决方法
YOLOv5 参数介绍
准确率(Precision)、召回率(Recall)以及F值(F-Measure)
理解准确率,精准率,召回率,真正率,假正率,ROC/AUC
使用WBF来提升目标检测性能
基于YOLOV4的人脸口罩佩戴检测(全过程知乎)

yolov5 训练结果解析

yolov5 识别效果不好如何判断原因
与前代差别

一些项目的参考

用python和OpenCV进行动态物体检测

某项目随笔

github 的

这个界面很喜欢但是太复杂了

可视化界面

自留一下,省的爆改后找不到原代码了

'''***上传图片***'''def upload_img(self):# 选择录像文件进行读取global result_pathglobal origin_pathfileName, fileType = QFileDialog.getOpenFileName(self, 'Choose file', '', '*.jpg *.png *.tif *.jpeg')if fileName:suffix = fileName.split(".")[-1]origin_path = osp.join("/Users/sinkarsenic/PycharmProjects/mask_detect/images/origin", "tmp_upload.jpg")    #暂时统一    + suffixresult_path = osp.join("/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp", "tmp_upload.jpg")shutil.copy(fileName, origin_path)# 应该调整一下图片的大小,然后统一放在一起im0 = cv2.imread(origin_path)# resize_scale = 1 #self.output_size / im0.shape[0]# im0 = cv2.resize(im0, (0, 0), fx=resize_scale, fy=resize_scale)# cv2.imwrite(result_path, im0)#self.right_img.setPixmap(QPixmap("images/tmp/single_result.jpg"))self.img2predict = fileNameself.left_img.setPixmap(QPixmap(origin_path))# 上传图片之后右侧的图片重置,self.right_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/UI/right.jpeg"))                               # 改路径def detect_img(self):os.system('python /Users/sinkarsenic/PycharmProjects/mask_detect/detect.py')self.left_img.setPixmap(QPixmap(origin_path))self.right_img.setPixmap(QPixmap(result_path))
  • 1.如果是大的图片,界面上显示的图片不完整。

完整版

# -*- coding: utf-8 -*-
# 再整个输入 url 下载视频检测
# https://vd2.bdstatic.com/mda-kbitdw1fp321irs1/v1-cae/sc/mda-kbitdw1fp321irs1.mp4?v_from_s=hkapp-haokan-suzhou&amp;auth_key=1647140033-0-0-5b3b43f203e8f57d4a2e34627da9f137&amp;bcevod_channel=searchbox_feed&amp;pd=1&amp;cd=0&amp;pt=3&amp;logid=1432873267&amp;vid=2466983731079509186&amp;abtest=100815_2-17451_2&amp;klogid=1432873267
# 应该在界面启动的时候就将模型加载出来,设置tmp的目录来放中间的处理结果
import shutil
import PyQt5.QtCore
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import threading
import argparse
import os
import sys
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
import os.path as osp
import detectFILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory :  /Users/sinkarsenic/PycharmProjects/mask_detect
if str(ROOT) not in sys.path:sys.path.append(str(ROOT))  # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))  # relativefrom models.common import DetectMultiBackend
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, time_sync# 添加一个界面
# 窗口主类
class MainWindow(QTabWidget):# 基本配置不动,然后只动第三个界面def __init__(self):# 初始化界面super().__init__()self.setWindowTitle('Target detection system')self.resize(1200, 800)    #1200, 800self.setWindowIcon(QIcon("images/UI/qq.png"))# 图片读取进程self.output_size = "480"self.img2predict = "/Users/sinkarsenic/PycharmProjects/mask_detect/images/origin/tmp_upload.jpg"self.device = 'cpu'# # 初始化视频读取线程self.vid_source = '0'  # 初始设为摄像头self.stopEvent = threading.Event()self.webcam = Trueself.stopEvent.clear()self.model = self.model_load(weights="/Users/sinkarsenic/Downloads/mask_detect/exp16/weights/best.pt",device=self.device)  # 指明模型加载的位置self.initUI()self.reset_vid()'''***模型初始化***'''@torch.no_grad()def model_load(self, weights="/Users/sinkarsenic/Downloads/mask_detect/exp16/weights/best.pt",  # model.pt path(s)device='cpu',  # cuda device, i.e. 0 or 0,1,2,3 or cpuhalf=False,  # use FP16 half-precision inferencednn=False,  # use OpenCV DNN for ONNX inference):device = select_device(device)half &= device.type != 'cpu'  # half precision only supported on CUDAdevice = select_device(device)model = DetectMultiBackend(weights, device=device, dnn=dnn)stride, names, pt, jit, onnx = model.stride, model.names, model.pt, model.jit, model.onnx# Halfhalf &= pt and device.type != 'cpu'  # half precision only supported by PyTorch on CUDAif pt:model.model.half() if half else model.model.float()print("模型加载完成!")return model'''***界面初始化***'''def initUI(self):# 图片检测子界面global mid_img_widgetfont_title = QFont('楷体', 16)font_main = QFont('楷体', 14)# 图片识别界面, 两个按钮,上传图片和显示结果img_detection_widget = QWidget()img_detection_layout = QVBoxLayout()img_detection_title = QLabel("图片识别功能")img_detection_title.setFont(font_title)mid_img_widget = QWidget()mid_img_layout = QHBoxLayout()self.left_img = QLabel()self.right_img = QLabel()#self.left_img.setFixedSize(700, 500)self.left_img.setPixmap(QPixmap("images/UI/mask1.jpg"))#self.right_img.setPixmap(QPixmap("images/UI/mask2.jpg"))self.left_img.setAlignment(Qt.AlignCenter)#self.right_img.setAlignment(Qt.AlignCenter)mid_img_layout.addWidget(self.left_img)mid_img_layout.addStretch(0)mid_img_layout.addWidget(self.right_img)mid_img_widget.setLayout(mid_img_layout)up_img_button = QPushButton("上传图片")det_img_button = QPushButton("开始检测")up_img_button.clicked.connect(self.upload_img)       #连接到函数det_img_button.clicked.connect(self.detect_img)up_img_button.setFont(font_main)det_img_button.setFont(font_main)up_img_button.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")det_img_button.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")img_detection_layout.addWidget(img_detection_title, alignment=Qt.AlignCenter)img_detection_layout.addWidget(mid_img_widget, alignment=Qt.AlignCenter)img_detection_layout.addWidget(up_img_button)img_detection_layout.addWidget(det_img_button)img_detection_widget.setLayout(img_detection_layout)# todo 视频识别界面# 视频识别界面的逻辑比较简单,基本就从上到下的逻辑vid_detection_widget = QWidget()vid_detection_layout = QVBoxLayout()vid_title = QLabel("视频检测功能")vid_title.setFont(font_title)self.vid_img = QLabel()self.vid_img.setPixmap(QPixmap("images/UI/up.jpeg"))vid_title.setAlignment(Qt.AlignCenter)self.vid_img.setAlignment(Qt.AlignCenter)self.webcam_detection_btn = QPushButton("摄像头实时监测")self.mp4_detection_btn = QPushButton("视频文件检测")self.vid_stop_btn = QPushButton("停止检测")self.webcam_detection_btn.setFont(font_main)self.mp4_detection_btn.setFont(font_main)self.vid_stop_btn.setFont(font_main)self.webcam_detection_btn.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")self.mp4_detection_btn.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")self.vid_stop_btn.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")self.webcam_detection_btn.clicked.connect(self.open_cam)self.mp4_detection_btn.clicked.connect(self.open_mp4)self.vid_stop_btn.clicked.connect(self.close_vid)# 添加组件到布局上vid_detection_layout.addWidget(vid_title)vid_detection_layout.addWidget(self.vid_img)vid_detection_layout.addWidget(self.webcam_detection_btn)vid_detection_layout.addWidget(self.mp4_detection_btn)vid_detection_layout.addWidget(self.vid_stop_btn)vid_detection_widget.setLayout(vid_detection_layout)# todo 关于界面about_widget = QWidget()about_layout = QVBoxLayout()about_title = QLabel('欢迎使用目标检测系统\n\n 2022')  # todo 修改欢迎词语about_title.setFont(QFont('楷体', 18))about_title.setAlignment(Qt.AlignCenter)about_img = QLabel()about_img.setPixmap(QPixmap('images/UI/qq.png'))about_img.setScaledContents(True)  # 让图片自适应label大小about_img.setAlignment(Qt.AlignCenter)label_super = QLabel()  # 作者信息label_super.setText("<a href='https://blog.csdn.net/qq_44846756/article/details/122792878'>-->Sink Arsenic</a>")label_super.setFont(QFont('楷体', 16))label_super.setOpenExternalLinks(True)# label_super.setOpenExternalLinks(True)label_super.setAlignment(Qt.AlignRight)about_layout.addWidget(about_title)about_layout.addStretch()about_layout.addWidget(about_img)about_layout.addStretch()about_layout.addWidget(label_super)about_widget.setLayout(about_layout)self.left_img.setAlignment(Qt.AlignCenter)self.addTab(img_detection_widget, '图片检测')self.addTab(vid_detection_widget, '视频检测')self.addTab(about_widget, '联系我')# 三个小图标self.setTabIcon(0, QIcon('images/UI/qq.png'))self.setTabIcon(1, QIcon('images/UI/2021.jpg'))self.setTabIcon(2, QIcon('images/UI/qq.png'))'''***上传图片***'''def upload_img(self):# 选择录像文件进行读取global result_pathglobal origin_pathfileName, fileType = QFileDialog.getOpenFileName(self, 'Choose file', '', '*.jpg *.png *.tif *.jpeg')if fileName:suffix = fileName.split(".")[-1]origin_path = osp.join("/Users/sinkarsenic/PycharmProjects/mask_detect/images/origin", "tmp_upload.jpg")    #暂时统一    + suffixresult_path = osp.join("/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp", "tmp_upload.jpg")shutil.copy(fileName, origin_path)# 应该调整一下图片的大小,然后统一放在一起im0 = cv2.imread(origin_path)im0 = cv2.resize(im0, (640, 640))  # (0, 0), fx=resize_scale, fy=resize_scale)cv2.imwrite(origin_path, im0)# resize_scale = 1.2 #self.output_size / im0.shape[0]# im0 = cv2.resize(im0, (0, 0), fx=resize_scale, fy=resize_scale)#self.right_img.setPixmap(QPixmap("images/tmp/single_result.jpg"))#self.img2predict = fileNameself.left_img.setPixmap(QPixmap(origin_path))#self.left_img.setFixedSize(700, 500)self.left_img.setScaledContents(True)  # 让图片自适应label大小# todo 上传图片之后右侧的图片重置,self.right_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/UI/right.jpeg"))                               # 改路径self.right_img.setScaledContents(True)  # 让图片自适应label大小# def detect_img(self):#      os.system('python /Users/sinkarsenic/PycharmProjects/mask_detect/detect.py')#      self.left_img.setPixmap(QPixmap(origin_path))#      self.left_img.setScaledContents(True)#      self.right_img.setPixmap(QPixmap(result_path)) #.scaled(1000, 400))#      self.right_img.setScaledContents(True)'''***检测图片***'''def detect_img(self):model = self.modeloutput_size = self.output_sizesource = self.img2predict  # file/dir/URL/glob, 0 for webcamimgsz = [640,640]  # inference size (pixels)conf_thres = 0.25  # confidence thresholdiou_thres = 0.45  # NMS IOU thresholdmax_det = 1000  # maximum detections per imagedevice = self.device  # cuda device, i.e. 0 or 0,1,2,3 or cpuview_img = False  # show resultssave_txt = False  # save results to *.txtsave_conf = False  # save confidences in --save-txt labelssave_crop = False  # save cropped prediction boxesnosave = False  # do not save images/videosclasses = None  # filter by class: --class 0, or --class 0 2 3agnostic_nms = False  # class-agnostic NMSaugment = False  # ugmented inferencevisualize = False  # visualize featuresline_thickness = 3  # bounding box thickness (pixels)hide_labels = False  # hide labelshide_conf = False  # hide confidenceshalf = False  # use FP16 half-precision inferencednn = False  # use OpenCV DNN for ONNX inferenceprint(source)if source == "":QMessageBox.warning(self, "请上传", "请先上传图片再进行检测")else:source = str(source)device = select_device(self.device)webcam = Falsestride, names, pt, jit, onnx = model.stride, model.names, model.pt, model.jit, model.onnximgsz = check_img_size(imgsz, s=stride)  # check image sizesave_img = not nosave and not source.endswith('.txt')  # save inference images# Dataloaderif webcam:view_img = check_imshow()cudnn.benchmark = True  # set True to speed up constant image size inferencedataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = len(dataset)  # batch_sizeelse:dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = 1  # batch_sizevid_path, vid_writer = [None] * bs, [None] * bs# Run inferenceif pt and device.type != 'cpu':model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.model.parameters())))  # warmupdt, seen = [0.0, 0.0, 0.0], 0for path, im, im0s, vid_cap, s in dataset:t1 = time_sync()im = torch.from_numpy(im).to(device)im = im.half() if half else im.float()  # uint8 to fp16/32im /= 255  # 0 - 255 to 0.0 - 1.0if len(im.shape) == 3:im = im[None]  # expand for batch dimt2 = time_sync()dt[0] += t2 - t1# Inference# visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else Falsepred = model(im, augment=augment, visualize=visualize)t3 = time_sync()dt[1] += t3 - t2# NMSpred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)dt[2] += time_sync() - t3# Second-stage classifier (optional)# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)# Process predictionsfor i, det in enumerate(pred):  # per imageseen += 1if webcam:  # batch_size >= 1p, im0, frame = path[i], im0s[i].copy(), dataset.counts += f'{i}: 'else:p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)p = Path(p)  # to Paths += '%gx%g ' % im.shape[2:]  # print stringgn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwhimc = im0.copy() if save_crop else im0  # for save_cropannotator = Annotator(im0, line_width=line_thickness, example=str(names))if len(det):# Rescale boxes from img_size to im0 sizedet[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()# Print resultsfor c in det[:, -1].unique():n = (det[:, -1] == c).sum()  # detections per classs += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string# Write resultsfor *xyxy, conf, cls in reversed(det):if save_txt:  # Write to filexywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywhline = (cls, *xywh, conf) if save_conf else (cls, *xywh)  # label format# with open(txt_path + '.txt', 'a') as f:#     f.write(('%g ' * len(line)).rstrip() % line + '\n')if save_img or save_crop or view_img:  # Add bbox to imagec = int(cls)  # integer classlabel = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')annotator.box_label(xyxy, label, color=colors(c, True))# if save_crop:#     save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg',#                  BGR=True)# Print time (inference-only)LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)')# Stream resultsim0 = annotator.result()# if view_img:#     cv2.imshow(str(p), im0)#     cv2.waitKey(1)  # 1 millisecond# Save results (image with detections)#resize_scale = 1.2   #output_size / im0.shape[0]#im0 = cv2.resize(im0, (0, 0), fx=resize_scale, fy=resize_scale)cv2.imwrite(result_path, im0)self.left_img.setPixmap(QPixmap(origin_path))self.left_img.setScaledContents(True)self.right_img.setPixmap(QPixmap(result_path)) #.scaled(1000, 400))self.right_img.setScaledContents(True)# 视频检测,逻辑基本一致,有两个功能,分别是检测摄像头的功能和检测视频文件的功能,先做检测摄像头的功能。'''### 界面关闭事件 ### '''def closeEvent(self, event):reply = QMessageBox.question(self,'quit',"Are you sure?",QMessageBox.Yes | QMessageBox.No,QMessageBox.No)if reply == QMessageBox.Yes:self.close()event.accept()else:event.ignore()'''### 视频关闭 ### '''def open_cam(self):self.webcam_detection_btn.setEnabled(False)self.mp4_detection_btn.setEnabled(False)self.vid_stop_btn.setEnabled(True)self.vid_source = '0'self.webcam = True# 把按钮给他重置了# print("GOGOGO")th = threading.Thread(target=self.detect_vid)th.start()'''### 开启视频文件检测 ### '''def open_mp4(self):fileName, fileType = QFileDialog.getOpenFileName(self, 'Choose file', '', '*.mp4 *.avi')if fileName:self.webcam_detection_btn.setEnabled(False)self.mp4_detection_btn.setEnabled(False)# self.vid_stop_btn.setEnabled(True)self.vid_source = fileNameself.webcam = Falseth = threading.Thread(target=self.detect_vid)th.start()'''### 视频开启 ### '''# 视频和摄像头的主函数是一样的,不过是传入的source不同罢了def detect_vid(self):# passmodel = self.modeloutput_size = self.output_size# source = self.img2predict  # file/dir/URL/glob, 0 for webcamimgsz = [640, 640]  # inference size (pixels)conf_thres = 0.25  # confidence thresholdiou_thres = 0.45  # NMS IOU thresholdmax_det = 1000  # maximum detections per image# device = self.device  # cuda device, i.e. 0 or 0,1,2,3 or cpuview_img = False  # show resultssave_txt = False  # save results to *.txtsave_conf = False  # save confidences in --save-txt labelssave_crop = False  # save cropped prediction boxesnosave = False  # do not save images/videosclasses = None  # filter by class: --class 0, or --class 0 2 3agnostic_nms = False  # class-agnostic NMSaugment = False  # ugmented inferencevisualize = False  # visualize featuresline_thickness = 3  # bounding box thickness (pixels)hide_labels = False  # hide labelshide_conf = False  # hide confidenceshalf = False  # use FP16 half-precision inferencednn = False  # use OpenCV DNN for ONNX inferencesource = str(self.vid_source)webcam = self.webcamdevice = select_device(self.device)stride, names, pt, jit, onnx = model.stride, model.names, model.pt, model.jit, model.onnximgsz = check_img_size(imgsz, s=stride)  # check image sizesave_img = not nosave and not source.endswith('.txt')  # save inference images# Dataloaderif webcam:view_img = check_imshow()cudnn.benchmark = True  # set True to speed up constant image size inferencedataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = len(dataset)  # batch_sizeelse:dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = 1  # batch_sizevid_path, vid_writer = [None] * bs, [None] * bs# Run inferenceif pt and device.type != 'cpu':model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.model.parameters())))  # warmupdt, seen = [0.0, 0.0, 0.0], 0for path, im, im0s, vid_cap, s in dataset:t1 = time_sync()im = torch.from_numpy(im).to(device)im = im.half() if half else im.float()  # uint8 to fp16/32im /= 255  # 0 - 255 to 0.0 - 1.0if len(im.shape) == 3:im = im[None]  # expand for batch dimt2 = time_sync()dt[0] += t2 - t1# Inference# visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else Falsepred = model(im, augment=augment, visualize=visualize)t3 = time_sync()dt[1] += t3 - t2# NMSpred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)dt[2] += time_sync() - t3# Second-stage classifier (optional)# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)# Process predictionsfor i, det in enumerate(pred):  # per imageseen += 1if webcam:  # batch_size >= 1p, im0, frame = path[i], im0s[i].copy(), dataset.counts += f'{i}: 'else:p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)p = Path(p)  # to Path# save_path = str(save_dir / p.name)  # im.jpg# txt_path = str(save_dir / 'labels' / p.stem) + (#     '' if dataset.mode == 'image' else f'_{frame}')  # im.txts += '%gx%g ' % im.shape[2:]  # print stringgn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwhimc = im0.copy() if save_crop else im0  # for save_cropannotator = Annotator(im0, line_width=line_thickness, example=str(names))if len(det):# Rescale boxes from img_size to im0 sizedet[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()# Print resultsfor c in det[:, -1].unique():n = (det[:, -1] == c).sum()  # detections per classs += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string# Write resultsfor *xyxy, conf, cls in reversed(det):if save_txt:  # Write to filexywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywhline = (cls, *xywh, conf) if save_conf else (cls, *xywh)  # label format# with open(txt_path + '.txt', 'a') as f:#     f.write(('%g ' * len(line)).rstrip() % line + '\n')if save_img or save_crop or view_img:  # Add bbox to imagec = int(cls)  # integer classlabel = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')annotator.box_label(xyxy, label, color=colors(c, True))# if save_crop:#     save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg',#                  BGR=True)# Print time (inference-only)LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)')# Stream results# Save results (image with detections)im0 = annotator.result()frame = im0resize_scale = 1.2frame_resized = cv2.resize(frame, (0, 0), fx=resize_scale, fy=resize_scale)cv2.imwrite('/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp/tmp_upload.jpg', frame_resized)                        #地址self.vid_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp/tmp_upload.jpg"))           #地址self.vid_img.setScaledContents(True)# self.vid_img# if view_img:# cv2.imshow(str(p), im0)# self.vid_img.setPixmap(QPixmap("images/tmp/single_result_vid.jpg"))# cv2.waitKey(1)  # 1 millisecondif cv2.waitKey(25) & self.stopEvent.is_set() == True:self.stopEvent.clear()self.webcam_detection_btn.setEnabled(True)self.mp4_detection_btn.setEnabled(True)self.reset_vid()break# self.reset_vid()'''### 界面重置 ### '''def reset_vid(self):self.webcam_detection_btn.setEnabled(True)self.mp4_detection_btn.setEnabled(True)# 视频封面图self.vid_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/UI/2021.jpg"))                                     #图片路径self.vid_img.setScaledContents(True)self.vid_source = '0'self.webcam = True'''### 视频重置 ### '''def close_vid(self):self.stopEvent.set()self.reset_vid()if __name__ == "__main__":app = QApplication(sys.argv)mainWindow = MainWindow()mainWindow.show()sys.exit(app.exec_())

第三版界面

import shutil
import PyQt5.QtCore
from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import threading
import argparse
import os
import sys
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
import os.path as osp
import detectFILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory :  /Users/sinkarsenic/PycharmProjects/mask_detect
if str(ROOT) not in sys.path:sys.path.append(str(ROOT))  # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))  # relativefrom models.common import DetectMultiBackend
from utils.datasets import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from utils.general import (LOGGER, check_file, check_img_size, check_imshow, check_requirements, colorstr,increment_path, non_max_suppression, print_args, scale_coords, strip_optimizer, xyxy2xywh)
from utils.plots import Annotator, colors, save_one_box
from utils.torch_utils import select_device, time_sync# 添加一个界面
# 窗口主类
class MainWindow(QTabWidget):# 基本配置不动,然后只动第三个界面def __init__(self):# 初始化界面super().__init__()self.setWindowTitle('Target detection system')self.resize(1200, 800)    #1200, 800self.setWindowIcon(QIcon("images/UI/qq.png"))# 图片读取进程self.output_size = "480"self.img2predict = "/Users/sinkarsenic/PycharmProjects/mask_detect/images/origin/tmp_upload.jpg"self.device = 'cpu'# # 初始化视频读取线程self.vid_source = '0'  # 初始设置为摄像头self.stopEvent = threading.Event()self.webcam = Trueself.stopEvent.clear()self.model = self.model_load(weights="/Users/sinkarsenic/Downloads/mask_detect/权重/best.pt",device=self.device)  # todo 指明模型加载的位置的设备self.initUI()self.reset_vid()'''***模型初始化***'''@torch.no_grad()def model_load(self, weights="/Users/sinkarsenic/Downloads/mask_detect/权重/best.pt",  # model.pt path(s)device='cpu',  # cuda device, i.e. 0 or 0,1,2,3 or cpuhalf=False,  # use FP16 half-precision inferencednn=False,  # use OpenCV DNN for ONNX inference):device = select_device(device)half &= device.type != 'cpu'  # half precision only supported on CUDAdevice = select_device(device)model = DetectMultiBackend(weights, device=device, dnn=dnn)stride, names, pt, jit, onnx = model.stride, model.names, model.pt, model.jit, model.onnx# Halfhalf &= pt and device.type != 'cpu'  # half precision only supported by PyTorch on CUDAif pt:model.model.half() if half else model.model.float()print("模型加载完成!")return model'''***界面初始化***'''def initUI(self):# 图片检测子界面global mid_img_widgetfont_title = QFont('楷体', 16)font_main = QFont('楷体', 14)# 图片识别界面, 两个按钮,上传图片和显示结果img_detection_widget = QWidget()img_detection_layout = QVBoxLayout()img_detection_title = QLabel("图片识别")img_detection_title.setFont(font_title)mid_img_widget = QWidget()mid_img_layout = QHBoxLayout()self.left_img = QLabel()self.right_img = QLabel()#self.left_img.setFixedSize(700, 500)self.left_img.setPixmap(QPixmap("images/UI/mask1.jpg"))#self.right_img.setPixmap(QPixmap("images/UI/mask2.jpg"))self.left_img.setAlignment(Qt.AlignCenter)#self.right_img.setAlignment(Qt.AlignCenter)mid_img_layout.addWidget(self.left_img)mid_img_layout.addStretch(0)mid_img_layout.addWidget(self.right_img)mid_img_widget.setLayout(mid_img_layout)up_img_button = QPushButton("Upload Image")det_img_button = QPushButton("Start")up_img_button.clicked.connect(self.upload_img)       #连接到函数det_img_button.clicked.connect(self.detect_img)up_img_button.setFont(font_main)det_img_button.setFont(font_main)up_img_button.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")det_img_button.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")img_detection_layout.addWidget(img_detection_title, alignment=Qt.AlignCenter)img_detection_layout.addWidget(mid_img_widget, alignment=Qt.AlignCenter)img_detection_layout.addWidget(up_img_button)img_detection_layout.addWidget(det_img_button)img_detection_widget.setLayout(img_detection_layout)# todo 视频识别界面# 视频识别界面的逻辑比较简单,基本就从上到下的逻辑vid_detection_widget = QWidget()vid_detection_layout = QVBoxLayout()vid_title = QLabel("视频检测")vid_title.setFont(font_title)self.vid_img = QLabel()self.vid_img.setPixmap(QPixmap("images/UI/up.jpeg"))vid_title.setAlignment(Qt.AlignCenter)self.vid_img.setAlignment(Qt.AlignCenter)self.webcam_detection_btn = QPushButton("Camara Detect")self.mp4_detection_btn = QPushButton("Video Detect")self.vid_stop_btn = QPushButton("Stop")self.webcam_detection_btn.setFont(font_main)self.mp4_detection_btn.setFont(font_main)self.vid_stop_btn.setFont(font_main)self.webcam_detection_btn.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")self.mp4_detection_btn.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")self.vid_stop_btn.setStyleSheet("QPushButton{color:white}""QPushButton:hover{background-color: rgb(2,110,180);}""QPushButton{background-color:rgb(48,124,208)}""QPushButton{border:2px}""QPushButton{border-radius:5px}""QPushButton{padding:5px 5px}""QPushButton{margin:5px 5px}")self.webcam_detection_btn.clicked.connect(self.open_cam)self.mp4_detection_btn.clicked.connect(self.open_mp4)self.vid_stop_btn.clicked.connect(self.close_vid)# 添加组件到布局上vid_detection_layout.addWidget(vid_title)vid_detection_layout.addWidget(self.vid_img)vid_detection_layout.addWidget(self.webcam_detection_btn)vid_detection_layout.addWidget(self.mp4_detection_btn)vid_detection_layout.addWidget(self.vid_stop_btn)vid_detection_widget.setLayout(vid_detection_layout)# todo 关于界面about_widget = QWidget()about_layout = QVBoxLayout()about_title = QLabel('Welcome!\n\n 2022')  # todo 修改欢迎词语about_title.setFont(QFont('楷体', 18))about_title.setAlignment(Qt.AlignCenter)about_img = QLabel()about_img.setPixmap(QPixmap('images/UI/qq.png'))about_img.setScaledContents(True)  # 让图片自适应label大小about_img.setAlignment(Qt.AlignCenter)# label4.setText("<a href='https://oi.wiki/wiki/学习率的调整'>如何调整学习率</a>")label_super = QLabel()  # todo 更换作者信息label_super.setText("<a href='https://blog.csdn.net/qq_44846756/article/details/122792878'>-->Sink Arsenic</a>")label_super.setFont(QFont('楷体', 16))label_super.setOpenExternalLinks(True)# label_super.setOpenExternalLinks(True)label_super.setAlignment(Qt.AlignRight)about_layout.addWidget(about_title)about_layout.addStretch()about_layout.addWidget(about_img)about_layout.addStretch()about_layout.addWidget(label_super)about_widget.setLayout(about_layout)self.left_img.setAlignment(Qt.AlignCenter)self.addTab(img_detection_widget, 'Image')self.addTab(vid_detection_widget, 'Video')self.addTab(about_widget, 'find me')# 三个小图标self.setTabIcon(0, QIcon('images/UI/qq.png'))self.setTabIcon(1, QIcon('images/UI/2021.jpg'))self.setTabIcon(2, QIcon('images/UI/qq.png'))'''***上传图片***'''def upload_img(self):# 选择录像文件进行读取global result_pathglobal origin_pathfileName, fileType = QFileDialog.getOpenFileName(self, 'Choose file', '', '*.jpg *.png *.tif *.jpeg')if fileName:suffix = fileName.split(".")[-1]origin_path = osp.join("/Users/sinkarsenic/PycharmProjects/mask_detect/images/origin", "tmp_upload.jpg")    #暂时统一    + suffixresult_path = osp.join("/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp", "tmp_upload.jpg")shutil.copy(fileName, origin_path)# 应该调整一下图片的大小,然后统一放在一起im0 = cv2.imread(origin_path)im0 = cv2.resize(im0, (640, 640))  # (0, 0), fx=resize_scale, fy=resize_scale)cv2.imwrite(origin_path, im0)# resize_scale = 1.2 #self.output_size / im0.shape[0]# im0 = cv2.resize(im0, (0, 0), fx=resize_scale, fy=resize_scale)#self.right_img.setPixmap(QPixmap("images/tmp/single_result.jpg"))#self.img2predict = fileNameself.left_img.setPixmap(QPixmap(origin_path))#self.left_img.setFixedSize(700, 500)self.left_img.setScaledContents(True)  # 让图片自适应label大小# todo 上传图片之后右侧的图片重置,self.right_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/UI/right.jpeg"))                               # 改路径self.right_img.setScaledContents(True)  # 让图片自适应label大小# def detect_img(self):#      os.system('python /Users/sinkarsenic/PycharmProjects/mask_detect/detect.py')#      self.left_img.setPixmap(QPixmap(origin_path))#      self.left_img.setScaledContents(True)#      self.right_img.setPixmap(QPixmap(result_path)) #.scaled(1000, 400))#      self.right_img.setScaledContents(True)'''***检测图片***'''def detect_img(self):model = self.modeloutput_size = self.output_sizesource = self.img2predict  # file/dir/URL/glob, 0 for webcamimgsz = [640,640]  # inference size (pixels)conf_thres = 0.25  # confidence thresholdiou_thres = 0.45  # NMS IOU thresholdmax_det = 1000  # maximum detections per imagedevice = self.device  # cuda device, i.e. 0 or 0,1,2,3 or cpuview_img = False  # show resultssave_txt = False  # save results to *.txtsave_conf = False  # save confidences in --save-txt labelssave_crop = False  # save cropped prediction boxesnosave = False  # do not save images/videosclasses = None  # filter by class: --class 0, or --class 0 2 3agnostic_nms = False  # class-agnostic NMSaugment = False  # ugmented inferencevisualize = False  # visualize featuresline_thickness = 3  # bounding box thickness (pixels)hide_labels = False  # hide labelshide_conf = False  # hide confidenceshalf = False  # use FP16 half-precision inferencednn = False  # use OpenCV DNN for ONNX inferenceprint(source)if source == "":QMessageBox.warning(self, "请上传", "请先上传图片再进行检测")else:source = str(source)device = select_device(self.device)webcam = Falsestride, names, pt, jit, onnx = model.stride, model.names, model.pt, model.jit, model.onnximgsz = check_img_size(imgsz, s=stride)  # check image sizesave_img = not nosave and not source.endswith('.txt')  # save inference images# Dataloaderif webcam:view_img = check_imshow()cudnn.benchmark = True  # set True to speed up constant image size inferencedataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = len(dataset)  # batch_sizeelse:dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = 1  # batch_sizevid_path, vid_writer = [None] * bs, [None] * bs# Run inferenceif pt and device.type != 'cpu':model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.model.parameters())))  # warmupdt, seen = [0.0, 0.0, 0.0], 0for path, im, im0s, vid_cap, s in dataset:t1 = time_sync()im = torch.from_numpy(im).to(device)im = im.half() if half else im.float()  # uint8 to fp16/32im /= 255  # 0 - 255 to 0.0 - 1.0if len(im.shape) == 3:im = im[None]  # expand for batch dimt2 = time_sync()dt[0] += t2 - t1# Inference# visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else Falsepred = model(im, augment=augment, visualize=visualize)t3 = time_sync()dt[1] += t3 - t2# NMSpred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)dt[2] += time_sync() - t3# Second-stage classifier (optional)# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)# Process predictionsfor i, det in enumerate(pred):  # per imageseen += 1if webcam:  # batch_size >= 1p, im0, frame = path[i], im0s[i].copy(), dataset.counts += f'{i}: 'else:p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)p = Path(p)  # to Paths += '%gx%g ' % im.shape[2:]  # print stringgn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwhimc = im0.copy() if save_crop else im0  # for save_cropannotator = Annotator(im0, line_width=line_thickness, example=str(names))if len(det):# Rescale boxes from img_size to im0 sizedet[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()# Print resultsfor c in det[:, -1].unique():n = (det[:, -1] == c).sum()  # detections per classs += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string# Write resultsfor *xyxy, conf, cls in reversed(det):if save_txt:  # Write to filexywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywhline = (cls, *xywh, conf) if save_conf else (cls, *xywh)  # label format# with open(txt_path + '.txt', 'a') as f:#     f.write(('%g ' * len(line)).rstrip() % line + '\n')if save_img or save_crop or view_img:  # Add bbox to imagec = int(cls)  # integer classlabel = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')annotator.box_label(xyxy, label, color=colors(c, True))# if save_crop:#     save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg',#                  BGR=True)# Print time (inference-only)LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)')# Stream resultsim0 = annotator.result()# if view_img:#     cv2.imshow(str(p), im0)#     cv2.waitKey(1)  # 1 millisecond# Save results (image with detections)#resize_scale = 1.2   #output_size / im0.shape[0]#im0 = cv2.resize(im0, (0, 0), fx=resize_scale, fy=resize_scale)cv2.imwrite(result_path, im0)self.left_img.setPixmap(QPixmap(origin_path))self.left_img.setScaledContents(True)self.right_img.setPixmap(QPixmap(result_path)) #.scaled(1000, 400))self.right_img.setScaledContents(True)# 视频检测,逻辑基本一致,有两个功能,分别是检测摄像头的功能和检测视频文件的功能,先做检测摄像头的功能。'''### 界面关闭事件 ### '''def closeEvent(self, event):reply = QMessageBox.question(self,'quit',"Are you sure?",QMessageBox.Yes | QMessageBox.No,QMessageBox.No)if reply == QMessageBox.Yes:self.close()event.accept()else:event.ignore()'''### 视频关闭事件 ### '''def open_cam(self):self.webcam_detection_btn.setEnabled(False)self.mp4_detection_btn.setEnabled(False)self.vid_stop_btn.setEnabled(True)self.vid_source = '0'self.webcam = True# 把按钮给他重置了# print("GOGOGO")th = threading.Thread(target=self.detect_vid)th.start()'''### 开启视频文件检测事件 ### '''def open_mp4(self):fileName, fileType = QFileDialog.getOpenFileName(self, 'Choose file', '', '*.mp4 *.avi')if fileName:self.webcam_detection_btn.setEnabled(False)self.mp4_detection_btn.setEnabled(False)# self.vid_stop_btn.setEnabled(True)self.vid_source = fileNameself.webcam = Falseth = threading.Thread(target=self.detect_vid)th.start()'''### 视频开启事件 ### '''# 视频和摄像头的主函数是一样的,不过是传入的source不同罢了def detect_vid(self):# passmodel = self.modeloutput_size = self.output_size# source = self.img2predict  # file/dir/URL/glob, 0 for webcamimgsz = [640, 640]  # inference size (pixels)conf_thres = 0.25  # confidence thresholdiou_thres = 0.45  # NMS IOU thresholdmax_det = 1000  # maximum detections per image# device = self.device  # cuda device, i.e. 0 or 0,1,2,3 or cpuview_img = False  # show resultssave_txt = False  # save results to *.txtsave_conf = False  # save confidences in --save-txt labelssave_crop = False  # save cropped prediction boxesnosave = False  # do not save images/videosclasses = None  # filter by class: --class 0, or --class 0 2 3agnostic_nms = False  # class-agnostic NMSaugment = False  # ugmented inferencevisualize = False  # visualize featuresline_thickness = 3  # bounding box thickness (pixels)hide_labels = False  # hide labelshide_conf = False  # hide confidenceshalf = False  # use FP16 half-precision inferencednn = False  # use OpenCV DNN for ONNX inferencesource = str(self.vid_source)webcam = self.webcamdevice = select_device(self.device)stride, names, pt, jit, onnx = model.stride, model.names, model.pt, model.jit, model.onnximgsz = check_img_size(imgsz, s=stride)  # check image sizesave_img = not nosave and not source.endswith('.txt')  # save inference images# Dataloaderif webcam:view_img = check_imshow()cudnn.benchmark = True  # set True to speed up constant image size inferencedataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = len(dataset)  # batch_sizeelse:dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt and not jit)bs = 1  # batch_sizevid_path, vid_writer = [None] * bs, [None] * bs# Run inferenceif pt and device.type != 'cpu':model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.model.parameters())))  # warmupdt, seen = [0.0, 0.0, 0.0], 0for path, im, im0s, vid_cap, s in dataset:t1 = time_sync()im = torch.from_numpy(im).to(device)im = im.half() if half else im.float()  # uint8 to fp16/32im /= 255  # 0 - 255 to 0.0 - 1.0if len(im.shape) == 3:im = im[None]  # expand for batch dimt2 = time_sync()dt[0] += t2 - t1# Inference# visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else Falsepred = model(im, augment=augment, visualize=visualize)t3 = time_sync()dt[1] += t3 - t2# NMSpred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)dt[2] += time_sync() - t3# Second-stage classifier (optional)# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)# Process predictionsfor i, det in enumerate(pred):  # per imageseen += 1if webcam:  # batch_size >= 1p, im0, frame = path[i], im0s[i].copy(), dataset.counts += f'{i}: 'else:p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)p = Path(p)  # to Path# save_path = str(save_dir / p.name)  # im.jpg# txt_path = str(save_dir / 'labels' / p.stem) + (#     '' if dataset.mode == 'image' else f'_{frame}')  # im.txts += '%gx%g ' % im.shape[2:]  # print stringgn = torch.tensor(im0.shape)[[1, 0, 1, 0]]  # normalization gain whwhimc = im0.copy() if save_crop else im0  # for save_cropannotator = Annotator(im0, line_width=line_thickness, example=str(names))if len(det):# Rescale boxes from img_size to im0 sizedet[:, :4] = scale_coords(im.shape[2:], det[:, :4], im0.shape).round()# Print resultsfor c in det[:, -1].unique():n = (det[:, -1] == c).sum()  # detections per classs += f"{n} {names[int(c)]}{'s' * (n > 1)}, "  # add to string# Write resultsfor *xyxy, conf, cls in reversed(det):if save_txt:  # Write to filexywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywhline = (cls, *xywh, conf) if save_conf else (cls, *xywh)  # label format# with open(txt_path + '.txt', 'a') as f:#     f.write(('%g ' * len(line)).rstrip() % line + '\n')if save_img or save_crop or view_img:  # Add bbox to imagec = int(cls)  # integer classlabel = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')annotator.box_label(xyxy, label, color=colors(c, True))# if save_crop:#     save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg',#                  BGR=True)# Print time (inference-only)LOGGER.info(f'{s}Done. ({t3 - t2:.3f}s)')# Stream results# Save results (image with detections)im0 = annotator.result()frame = im0resize_scale = 1.2frame_resized = cv2.resize(frame, (0, 0), fx=resize_scale, fy=resize_scale)cv2.imwrite('/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp/tmp_upload.jpg', frame_resized)                        #地址self.vid_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/tmp/tmp_upload.jpg"))           #地址self.vid_img.setScaledContents(True)# self.vid_img# if view_img:# cv2.imshow(str(p), im0)# self.vid_img.setPixmap(QPixmap("images/tmp/single_result_vid.jpg"))# cv2.waitKey(1)  # 1 millisecondif cv2.waitKey(25) & self.stopEvent.is_set() == True:self.stopEvent.clear()self.webcam_detection_btn.setEnabled(True)self.mp4_detection_btn.setEnabled(True)self.reset_vid()break# self.reset_vid()'''### 界面重置事件 ### '''def reset_vid(self):self.webcam_detection_btn.setEnabled(True)self.mp4_detection_btn.setEnabled(True)# 视频封面图self.vid_img.setPixmap(QPixmap("/Users/sinkarsenic/PycharmProjects/mask_detect/images/UI/2021.jpg"))                                     #图片路径self.vid_img.setScaledContents(True)self.vid_source = '0'self.webcam = True'''### 视频重置事件 ### '''def close_vid(self):self.stopEvent.set()self.reset_vid()
if __name__ == "__main__":app = QApplication(sys.argv)mainWindow = MainWindow()mainWindow.show()sys.exit(app.exec_())

export 并通过 netron 查看网络结构图

本地可以 pip install netron 但也可以在网页上提交模型并查看
网页查看

export 修改 data 、weight 的路径,并且要安装好 onnx 和 coremltools才能运行成功,然后在权重的路径下找就行了。

在实时显示的画面中用 puttext添加信息

# Write resultsfor *xyxy, conf, cls in reversed(det):if save_txt:  # Write to filexywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist()  # normalized xywhline = (cls, *xywh, conf) if save_conf else (cls, *xywh)  # label format# with open(txt_path + '.txt', 'a') as f:#     f.write(('%g ' * len(line)).rstrip() % line + '\n')if save_img or save_crop or view_img:  # Add bbox to imagec = int(cls)  # integer classlabel = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')annotator.box_label(xyxy, label, color=colors(c, True))# if save_crop:#     save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg',#                  BGR=True)cv2.putText(im0, s, (100, 400), 0, 2, (0,0,0), 2)

各参数的含义

WARNING: Ignoring corrupted image

用手机拍了几张图片想送进去训练,但是显示图片错误,mac 上改变拓展名似乎不能改变图片本来的格式,只能改变打开的方式…只能使用预览后编辑导出,压缩后的图片就可以正常使用了。注:原图也是不能被 Detect 的。

优化 v5

其实数据的选取才是最重要的,网络结构修改感觉变化不是很大。

  • exp24 基于 exp23 将loss.py的 139行一大段objectness注释掉 然后改成
tobj[b, a, gj, gi] =1

未尝试1
未尝试 2
参考 1
参考 2

通过visualize查看检测过程的特征图

参考
像 colab 上那样用命令行的话加上这个就行

--visualize

直接运行脚本的话,加一个 default。

parser.add_argument('--visualize', default=True,action='store_true', help='visualize features')

处理口罩图片数据(yolov5)中碰到的问题(macOS)相关推荐

  1. python导入图片数据_Python中读取图片的6种方式

    Python进行图片处理,第一步就是读取图片,这里给大家整理了6种图片的读取方式,并将读取的图片装换成numpy.ndarray()格式.首先需要准备一张照片,假如你有女朋友的话,可以用女朋友的,没有 ...

  2. 深度学习中图片数据增强方法

    简 介: 在深度学习中需要对图像进行不同的处理.本文对比了基于Numpy以及Paddle.vision.transforms函数中对于图片处理的效果. 关键词: 图像预处理,cv2,paddle #m ...

  3. layui单元格鼠标样式_Layui实现数据表格中鼠标悬浮图片放大效果,离开时恢复原图的方法...

    如下所示: var tableIns = window.demoTable = table .render({ elem : '#idTest', id : 'idTest', url : '/par ...

  4. 如何在数据表中存取图片 - 回复 三足乌 的问题

    问题来源: http://www.cnblogs.com/del/archive/2009/05/28/1491186.html#1801853 准备工作: 1.在空白窗体上添加: ClientDat ...

  5. 爬虫图片mysql_爬取微博图片数据存到Mysql中遇到的各种坑\爬取微博图片\Mysql存储图片\微博爬虫...

    本人长期出售超大量微博数据.旅游网站评论数据,并提供各种指定数据爬取服务,Message to YuboonaZhang@Yahoo.com.同时欢迎加入社交媒体数据交流群:99918768 前言 由 ...

  6. 爬取图片到mysql数据库_爬取微博图片数据存到Mysql中遇到的各种坑\mysql存储图片\爬取微博图片...

    前言 由于硬件等各种原因需要把大概170多万2t左右的微博图片数据存到Mysql中.之前存微博数据一直用的非关系型数据库mongodb,由于对Mysql的各种不熟悉,踩了无数坑,来来回回改了3天才完成 ...

  7. layui数据表格中包含图片的处理方式

    layui数据表格中包含图片时,图片可能显示不全. 搜索后网上的解决方法: <style>.layui-table-cell {height: 100%;max-width: 100%;} ...

  8. mysql存储爬虫图片_爬取微博图片数据存到Mysql中遇到的各种坑\爬取微博图片\Mysql存储图片\微博爬虫...

    本人长期出售超大量微博数据.旅游网站评论数据,并提供各种指定数据爬取服务,Message to YuboonaZhang@Yahoo.com.同时欢迎加入社交媒体数据交流群:99918768 前言 由 ...

  9. python爬取图片存入mysql_python爬取微博图片数据存到Mysql中遇到的各种坑

    前言 由于硬件等各种原因需要把大概170多万2t左右的微博图片数据存到Mysql中.之前存微博数据一直用的非关系型数据库mongodb,由于对Mysql的各种不熟悉,踩了无数坑,来来回回改了3天才完成 ...

最新文章

  1. Python Numpy包安装
  2. 【云上ELK系列】Logstash迁移Elasticsearch数据方法解读
  3. boost::type_erasure::typeid_of相关的测试程序
  4. 宁波镇海2021年高考成绩查询,最新!2021年,宁波镇海区的这14所中小学“爆了...
  5. ajax上传文件到servlet
  6. c++future 配合httplib post 高级技巧
  7. JavaScript学习(四十五)—练习题
  8. 如果我是推荐算法面试官,我会问哪些问题?
  9. PHP数组学习(一)
  10. 洛谷2543AHOI2005]航线规划 (树剖+线段树+割边思路)
  11. 解析字符串获取路径_node学习--path 路径模块
  12. c语言 软件编程入门自学,软件编程入门自学
  13. 如何计算当地的中央子午线?全国各地中央子午线【转载】
  14. python中def fun()是什么意思_python里的def 方法中-代表什么意思?
  15. 加快黑群晖套件中心的套件下载速度
  16. DCGAN生成动漫头像(附代码)
  17. LeetCode 89 双周赛
  18. Java系列之:var关键字
  19. html 轮播图自适应,JavaScript 自适应轮播图
  20. vue 组件 Vue.component 用法

热门文章

  1. 信道编码和交织的有效总结和理解
  2. Ubuntu中完全卸载MySQL所有相关文件
  3. STM32F4驱动NEC协议的红外接收头
  4. 红旗linux桌面版6.0 sp3,红旗Linux桌面版6.0SP1发布及下载
  5. Axon Framework架构概述
  6. iptables 查看客户端流量情况
  7. 001_Whetting Your Appetite_引言
  8. MSP432学习笔记:ADC14
  9. 数据库管理工具heidiSQL的基本使用
  10. 网站日访问量,在线用户数,等如何统计?