当前位置: 首页 > news >正文

yolov8训练初体验

最近在爬一些数据,有些网址的验证码比较难搞,于是使用yolov8来解决。

一、数据打标签并转为txt

使用的软件为X-AnyLabeling。内置各种模型,方便打标。

打标完成后由于是json格式,所以我们使用python转换即可


import json
import os#矩形框时def labelme_to_yolo(label_me_json_file, cls2id_dict):label_me_json = json.load(open(label_me_json_file, mode='r', encoding='UTF-8'))shapes = label_me_json['shapes']img_width, img_height = label_me_json['imageWidth'], label_me_json['imageHeight']img_path = label_me_json['imagePath']img_data = label_me_json['imageData'] if 'imageData' in label_me_json else ''labels = []for s in shapes:s_type = s['shape_type']s_type = s_type.lower()if s_type == 'rectangle':pts = s['points']x1, y1 = pts[0]  # left cornerx2, y2 = pts[1]  # right cornerx = (x1 + x2) / 2 / img_widthy = (y1 + y2) / 2 / img_heightw = abs(x2 - x1) / img_widthh = abs(y2 - y1) / img_heightcid = cls2id_dict[s['label']]labels.append(f'{cid} {x} {y} {w} {h}')return labelsdef write_label2txt(save_txt_path, label_list):f = open(save_txt_path, "w", encoding="UTF-8")for label in label_list:temp_list = label.split(" ")f.write(temp_list[0])f.write(" ")f.write(temp_list[1])f.write(" ")f.write(temp_list[2])f.write(" ")f.write(temp_list[3])f.write(" ")f.write(temp_list[4])f.write("\n")if __name__ == '__main__':# 原始图片文件夹路径img_dir = r"D:\pic\pic"# 原始JSON标签文件夹路径json_dir = r"D:\pic\label_json"# 生成保存TXT文件夹路径save_dir = r"D:\pic\label_txt"# 类别和序号的映射字典cls2id_dict = {"building1": "0"}if not os.path.exists(save_dir):os.makedirs(save_dir)for json_name in os.listdir(json_dir):json_path = os.path.join(json_dir, json_name)txt_name = json_name.split(".")[0] + ".txt"save_txt_path = os.path.join(save_dir, txt_name)labels = labelme_to_yolo(json_path, cls2id_dict)write_label2txt(save_txt_path, labels)
# 处理 X-Anylabeling 多边形矩阵的标注 json 转化 txt,提取点
import json
import osname2id = { '球体'  : 0,'立方体': 1,'圆锥体': 2,'圆柱体': 3,'多面体': 4}  # 修改你的类别并且赋与 indexdef decode_json(json_floder_path, txt_outer_path, json_name):txt_name = os.path.join(txt_outer_path,json_name[:-5]) + '.txt'with open(txt_name, 'a') as f:json_path = os.path.join(json_floder_path, json_name)data = json.load(open(json_path, 'r', encoding='utf8', errors='ignore'))img_w = data['imageWidth']img_h = data['imageHeight']isshape_type = data['shapes'][0]['shape_type']print(isshape_type)dw = 1. / (img_w)dh = 1. / (img_h)for i in data['shapes']:label_name = i['label']if (i['shape_type'] == 'polygon'):point = []for lk in range(len(i['points'])):x = float(i['points'][lk][0])y = float(i['points'][lk][1])point_x = x * dwpoint_y = y * dhpoint.append(point_x)point.append(point_y)try:formatted_line = f"{name2id[label_name]} {' '.join(str(a) for a in point)}\n"f.write(formatted_line)except KeyError:print(f"Warning: Label name '{label_name}' not found in name2id mapping.")f.close()if __name__ == "__main__":json_floder_path = r'D:\pic\label_json'  # 存放 json 的文件夹的绝对路径txt_outer_path = r'D:\pic\label_txt'  # 存放 txt 的文件夹绝对路径json_names = os.listdir(json_floder_path)flagcount = 0for json_name in json_names:decode_json(json_floder_path, txt_outer_path, json_name)flagcount += 1print('-----------转化完毕------------')

二、使用yolov8训练

2.1 将图片和标签分别放在datasets目录下

2.2创建yaml文件

trian为训练的图片

val为预测的图片


train: D:\\software\\PyCharm\\workspace\\ultralytics\\datasest\\mypic\\images 
val: D:\\software\\PyCharm\\workspace\\SomeTry\\yanzhengma\\val names:0: '球体'1: '立方体'2: '圆锥体'3: '圆柱体'4: '多面体'

2.3 创建训练代码

YOLOv8文档

from ultralytics import YOLOmodel = YOLO('yolov8n.pt')
model.train(data='mypic.yaml', epochs=100, imgsz=640, batch=8)#用训练后的模型进行预测
yolo predict model=runs/detect/train/weights/best.pt source=D:\\software\\PyCharm\\workspace\\SomeTry\\yanzhengma\\val\\1719060455810geetest_image.jpg

训练结果

相关文章:

  • CSS的 text-decoration
  • 八大排序浅入浅出
  • Instagram APIj接口——快速获取Ins帖子媒体内容下载链接
  • python脚本获取本机IP的方式
  • Flume基础教程
  • 使用python下载图片且批量将图片插入word文档
  • 如何设置MySQL远程访问权限?
  • [学习笔记]-MyBatis-Plus简介
  • Minillama3->dpo训练
  • OCR识别
  • 【IEEE独立出版、有确定的ISBN号】第三届能源与电力系统国际学术会议 (ICEEPS 2024)
  • php百度云账户余额查询API示例
  • 文件操作<C语言>
  • 【Ruby爬虫01】某吃瓜网站图片数据采集
  • 全面理解-Flutter(万字长文,深度解析)
  • 深入了解以太坊
  • 《深入 React 技术栈》
  • 【剑指offer】让抽象问题具体化
  • JavaScript实现分页效果
  • JS学习笔记——闭包
  • OSS Web直传 (文件图片)
  • Shadow DOM 内部构造及如何构建独立组件
  • SpiderData 2019年2月25日 DApp数据排行榜
  • underscore源码剖析之整体架构
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 工作踩坑系列——https访问遇到“已阻止载入混合活动内容”
  • ------- 计算机网络基础
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 浏览器缓存机制分析
  • 漂亮刷新控件-iOS
  • 手机app有了短信验证码还有没必要有图片验证码?
  • 微信小程序开发问题汇总
  • 微信小程序填坑清单
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 自动记录MySQL慢查询快照脚本
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • Linux权限管理(week1_day5)--技术流ken
  • 带你开发类似Pokemon Go的AR游戏
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • (4.10~4.16)
  • (C++)八皇后问题
  • (C语言)fread与fwrite详解
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (Oracle)SQL优化技巧(一):分页查询
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (过滤器)Filter和(监听器)listener
  • (免费领源码)python+django+mysql线上兼职平台系统83320-计算机毕业设计项目选题推荐
  • (强烈推荐)移动端音视频从零到上手(下)
  • (四)图像的%2线性拉伸
  • (算法)Game
  • (一)C语言之入门:使用Visual Studio Community 2022运行hello world
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (一)VirtualBox安装增强功能