• yolov8训练初体验


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

    一、数据打标签并转为txt

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

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

    1. import json
    2. import os
    3. #矩形框时
    4. def labelme_to_yolo(label_me_json_file, cls2id_dict):
    5. label_me_json = json.load(open(label_me_json_file, mode='r', encoding='UTF-8'))
    6. shapes = label_me_json['shapes']
    7. img_width, img_height = label_me_json['imageWidth'], label_me_json['imageHeight']
    8. img_path = label_me_json['imagePath']
    9. img_data = label_me_json['imageData'] if 'imageData' in label_me_json else ''
    10. labels = []
    11. for s in shapes:
    12. s_type = s['shape_type']
    13. s_type = s_type.lower()
    14. if s_type == 'rectangle':
    15. pts = s['points']
    16. x1, y1 = pts[0] # left corner
    17. x2, y2 = pts[1] # right corner
    18. x = (x1 + x2) / 2 / img_width
    19. y = (y1 + y2) / 2 / img_height
    20. w = abs(x2 - x1) / img_width
    21. h = abs(y2 - y1) / img_height
    22. cid = cls2id_dict[s['label']]
    23. labels.append(f'{cid} {x} {y} {w} {h}')
    24. return labels
    25. def write_label2txt(save_txt_path, label_list):
    26. f = open(save_txt_path, "w", encoding="UTF-8")
    27. for label in label_list:
    28. temp_list = label.split(" ")
    29. f.write(temp_list[0])
    30. f.write(" ")
    31. f.write(temp_list[1])
    32. f.write(" ")
    33. f.write(temp_list[2])
    34. f.write(" ")
    35. f.write(temp_list[3])
    36. f.write(" ")
    37. f.write(temp_list[4])
    38. f.write("\n")
    39. if __name__ == '__main__':
    40. # 原始图片文件夹路径
    41. img_dir = r"D:\pic\pic"
    42. # 原始JSON标签文件夹路径
    43. json_dir = r"D:\pic\label_json"
    44. # 生成保存TXT文件夹路径
    45. save_dir = r"D:\pic\label_txt"
    46. # 类别和序号的映射字典
    47. cls2id_dict = {"building1": "0"}
    48. if not os.path.exists(save_dir):
    49. os.makedirs(save_dir)
    50. for json_name in os.listdir(json_dir):
    51. json_path = os.path.join(json_dir, json_name)
    52. txt_name = json_name.split(".")[0] + ".txt"
    53. save_txt_path = os.path.join(save_dir, txt_name)
    54. labels = labelme_to_yolo(json_path, cls2id_dict)
    55. write_label2txt(save_txt_path, labels)
    1. # 处理 X-Anylabeling 多边形矩阵的标注 json 转化 txt,提取点
    2. import json
    3. import os
    4. name2id = { '球体' : 0,
    5. '立方体': 1,
    6. '圆锥体': 2,
    7. '圆柱体': 3,
    8. '多面体': 4
    9. } # 修改你的类别并且赋与 index
    10. def decode_json(json_floder_path, txt_outer_path, json_name):
    11. txt_name = os.path.join(txt_outer_path,json_name[:-5]) + '.txt'
    12. with open(txt_name, 'a') as f:
    13. json_path = os.path.join(json_floder_path, json_name)
    14. data = json.load(open(json_path, 'r', encoding='utf8', errors='ignore'))
    15. img_w = data['imageWidth']
    16. img_h = data['imageHeight']
    17. isshape_type = data['shapes'][0]['shape_type']
    18. print(isshape_type)
    19. dw = 1. / (img_w)
    20. dh = 1. / (img_h)
    21. for i in data['shapes']:
    22. label_name = i['label']
    23. if (i['shape_type'] == 'polygon'):
    24. point = []
    25. for lk in range(len(i['points'])):
    26. x = float(i['points'][lk][0])
    27. y = float(i['points'][lk][1])
    28. point_x = x * dw
    29. point_y = y * dh
    30. point.append(point_x)
    31. point.append(point_y)
    32. try:
    33. formatted_line = f"{name2id[label_name]} {' '.join(str(a) for a in point)}\n"
    34. f.write(formatted_line)
    35. except KeyError:
    36. print(f"Warning: Label name '{label_name}' not found in name2id mapping.")
    37. f.close()
    38. if __name__ == "__main__":
    39. json_floder_path = r'D:\pic\label_json' # 存放 json 的文件夹的绝对路径
    40. txt_outer_path = r'D:\pic\label_txt' # 存放 txt 的文件夹绝对路径
    41. json_names = os.listdir(json_floder_path)
    42. flagcount = 0
    43. for json_name in json_names:
    44. decode_json(json_floder_path, txt_outer_path, json_name)
    45. flagcount += 1
    46. print('-----------转化完毕------------')

    二、使用yolov8训练

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

    2.2创建yaml文件

    trian为训练的图片

    val为预测的图片

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

    2.3 创建训练代码

    YOLOv8文档

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

    训练结果

  • 相关阅读:
    第2周学习:卷积神经网络基础
    嵌入式学习笔记(33)S5PV210的第二阶段处理过程
    阿里也出手了!Spring CloudAlibaba AI问世了
    CMS-织梦[dede]-通用免登发布插件
    Vue笔记-vue中使用JS创建的函数
    Semantic Kernel 入门系列:💾Native Function
    SQL语句记了又忘?常用的SQL语句,配语句和图解超详细o
    pmp是什么意思啊?
    基于php 进行每半小时钉钉预警
    stm32定时器之简单封装
  • 原文地址:https://blog.csdn.net/weixin_45249411/article/details/139889138