本文主要是介绍极验3图标(2)目标检测+分类模型训练(手把手教学包会),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
由于上次写的图标识别文章不够详细,这次换了一种图标,详细的记录下整个训练过程吧~
首先是目标检测,用的是yolov5,下载地址:
https://github.com/ultralytics/yolov5
还需下载预训练权重文件,下载地址:
链接:https://pan.baidu.com/s/18SQdhqLsQ5ivPH9M7vhCFg?pwd=8f4e 提取码:8f4e
接着使用代码下载100张图片
# -*-coding:utf-8 -*-"""
# File : captcha.py
# Time :2023/04/23 18:28
# Author :ndy
# version :python 3.6
# Description:
"""
import re
import time
import requests
import vthreadheaders = {"Accept": "*/*","Accept-Language": "zh-CN,zh;q=0.9","Referer": "https://signin.hworld.com/","User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
}@vthread.pool(5)
def run():url = "https://www.geetest.com/demo/gt/register-icon?t={}".format(int(time.time()*1000))res = requests.get(url, headers=headers).json()challenge = res['challenge']gt = res['gt']requests.get('https://api.geetest.com/gettype.php?gt={}&callback=geetest_1713230167590'.format(gt), headers=headers)params = {"gt": gt,"challenge": challenge,"lang": "zh-cn","pt": "0","client_type": "web_mobile","w": "","callback": "geetest_1694485799312"}requests.get("https://api.geevisit.com/ajax.php", headers=headers, params=params)url = "https://apiv6.geetest.com/get.php"params = {"is_next": "true","type": "click","gt": gt,"challenge": challenge,"lang": "zh-cn","https": "true","protocol": "https://","offline": "false","product": "float","api_server": "apiv6.geetest.com","isPC": "true","autoReset": "true","width": "100%","callback": "geetest_1713230318961"}res = requests.get(url, headers=headers, params=params).textimage_path = 'https://static.geetest.com'+re.findall('"pic": "(.*?)"',res)[0]print(image_path)image = requests.get(image_path).contentimg_name = image_path.split('/')[-1]with open('./imgs/{}'.format(img_name),'wb') as f:f.write(image)if __name__ == '__main__':for i in range(100):run()
使用labelimg进行如下标注
在data下创建如下几个文件夹(注意: images内为数据集原始图片,Annotations内为标注的xml文件)
并将images内文件复制到JPEGIamges中(代码中并没有用到这部分,可以不创建JPEGImages文件夹)
根目录下创建make_txt.py 文件,代码如下:
import os
import randomtrainval_percent = 0.1
train_percent = 0.9
xmlfilepath = 'data/Annotations'
txtsavepath = 'data/ImageSets'
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftrainval = open('data/ImageSets/trainval.txt', 'w')
ftest = open('data/ImageSets/test.txt', 'w')
ftrain = open('data/ImageSets/train.txt', 'w')
fval = open('data/ImageSets/val.txt', 'w')
for i in list:name = total_xml[i][:-4] + '\n'if i in trainval:ftrainval.write(name)if i in train:ftest.write(name)else:fval.write(name)else:ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
根目录下继续创建 voc_label.py 文件,代码如下:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train', 'test','val']
classes = ['target','char']
def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)
def convert_annotation(image_id):in_file = open('data/Annotations/%s.xml' % (image_id))out_file = open('data/labels/%s.txt' % (image_id), 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
print(wd)
for image_set in sets:if not os.path.exists('data/labels/'):os.makedirs('data/labels/')image_ids = open('data/ImageSets/%s.txt' % (image_set)).read().strip().split()list_file = open('data/%s.txt' % (image_set), 'w')for image_id in image_ids:list_file.write('data/images/%s.jpg\n' % (image_id))convert_annotation(image_id)list_file.close()
在这里要注意: 这里的 class =[‘target’,‘char’] 代表数据集需要标注的类别,单引号的内容需要根据你的数据集确定,有几类就写几类。
修改之后,依次执行上面两个py文件,执行成功是这样的
(1) labels下生成txt文件(显示数据集的具体标注数据)
(2) ImageSets下生成四个txt文件
(3) data下生成三个txt文件(带有图片的路径)
修改yaml文件
这里的yaml和以往的cfg文件是差不多的,但需要配置一份属于自己数据集的yaml文件。
复制data目录下的coco.yaml,我这里命名为my.yaml
主要修改三个地方:
a. 修改train,val,test的路径为自己刚刚生成的路径
b. nc 里的数字代表数据集的类别数
c. names 里为自己数据集标的所有类名称
修改models模型文件
models下有四个模型,smlx需要训练的时间依次增加,按照需求选择一个文件进行修改即可
这里修改了yolov5s.yaml(需要下载权重文件yolo5s.pt),只需要将nc的类别修改为自己需要的即可
训练train.py
这里需要对train.py文件内的参数进行修改,按照我们的需求需改即可
weights,cfg,data按照自己所需文件的路径修改即可
epochs迭代次数自己决定,我这里仅用100次进行测试
batch-size过高可能会影响电脑运行速度,还是要根据自己电脑硬件条件决定增加还是减少
最终训练完成后,导出onnx文件
修改export.py以下对应参数并运行
然后调用目标检测文件,看看效果还是不错的
最后同样的方法,对图片下载切割后分类,起名字真麻烦,最终类型数是110种。
接着就是要进行分类训练,先使用以下脚本划分训练集、测试集、验证集图片
# -*-coding:utf-8 -*-"""
# File : 图片划分.py
# Time : 2024/4/2 15:01
# Author : ndy
# version : 2024
# Description:
"""
import os
from shutil import copy
import randomdef mkfile(file):if not os.path.exists(file):os.makedirs(file)# 获取data文件夹下所有文件夹名(即需要分类的类名)
file_path = r'分类的图片路径'
save_path = '../classify/datasets/data/'
flower_class = [cla for cla in os.listdir(file_path)]
# 划分比例,训练集 : 测试集 :验证= 7:1:2
split_train = 0.7
split_test = 0.1
split_val = 0.2# 遍历所有类别的全部图像并按比例分成训练集和验证集
for cla in flower_class:cla_path = file_path + '/' + cla + '/' # 某一类别的子目录images = os.listdir(cla_path) # iamges 列表存储了该目录下所有图像的名称num = len(images)eval_train = random.sample(images, k=int(num * split_train)) # 从images列表中随机抽取 k 个图像名称eval_val = random.sample(images, k=int(num * split_val))for index, image in enumerate(images):if image in eval_train:image_path = cla_path + imagenew_path = save_path + '/train/' + clamkfile(new_path)copy(image_path, new_path) # 将选中的图像复制到新路径elif image in eval_val:image_path = cla_path + imagenew_path = save_path + '/val/' + clamkfile(new_path)copy(image_path, new_path) # 将选中的图像复制到新路径else:image_path = cla_path + imagenew_path = save_path + '/test/' + clamkfile(new_path)copy(image_path, new_path)print("\r[{}] processing [{}/{}]".format(cla, index + 1, num), end="") # processing barprint()print("processing done!")'''图片尺寸 50*50'''
目录格式如下
然后修改train.py文件如下配置
开始训练……
完成后进行测试下
最后结合目标检测实现图标点选功能
分类图片文件已上传至星球(【即将涨价】),可自行进行训练!
同时已上传极验3文字点选源文件,解压即用!
这篇关于极验3图标(2)目标检测+分类模型训练(手把手教学包会)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!