当前位置:   article > 正文

YOLOv8旋转目标检测Yolov8n-obb详细实例+rolabelimg

yolov8n

一、Yolov8环境搭建

首先创建虚拟环境下载安装(其实就是yolov8的环境)再大概写一下步骤,没有想详细的看本人另外一篇:YOLOv8环境搭建_yolov8环境配置-CSDN博客

1、下载安装anaconda

2、创建虚拟环境

conda create -n my_yolov8 python=3.8.8

3、激活刚创建的虚拟环境

activate my_yolov8

4、 到官方网站下载yolo模型 ,下载好后解压

 https://github.com/ultralytics/ultralytics 

4、文件requirements.txt ,安装配置环境

pip install -r 自己的requirements路径\ultralytics-main\requirements.txt

requirements.txt内容:自己可以在下载的ultralytics-main下面新建一个txt

  1. # Ultralytics requirements
  2. # Usage: pip install -r requirements.txt
  3. # Base ----------------------------------------
  4. hydra-core>=1.2.0
  5. matplotlib>=3.2.2
  6. numpy>=1.18.5
  7. opencv-python>=4.1.1
  8. Pillow>=7.1.2
  9. PyYAML>=5.3.1
  10. requests>=2.23.0
  11. scipy>=1.4.1
  12. torch>=1.7.0
  13. torchvision>=0.8.1
  14. tqdm>=4.64.0
  15. # Logging -------------------------------------
  16. tensorboard>=2.4.1
  17. # clearml
  18. # comet
  19. # Plotting ------------------------------------
  20. pandas>=1.1.4
  21. seaborn>=0.11.0
  22. # Export --------------------------------------
  23. # coremltools>=6.0 # CoreML export
  24. # onnx>=1.12.0 # ONNX export
  25. # onnx-simplifier>=0.4.1 # ONNX simplifier
  26. # nvidia-pyindex # TensorRT export
  27. # nvidia-tensorrt # TensorRT export
  28. # scikit-learn==0.19.2 # CoreML quantization
  29. # tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
  30. # tensorflowjs>=3.9.0 # TF.js export
  31. # openvino-dev # OpenVINO export
  32. # Extras --------------------------------------
  33. ipython # interactive notebook
  34. psutil # system utilization
  35. thop>=0.1.1 # FLOPs computation
  36. # albumentations>=1.0.3
  37. # pycocotools>=2.0.6 # COCO mAP
  38. # roboflow
  39. # HUB -----------------------------------------
  40. GitPython>=3.1.24

二、数据集制作

1、安装制作标签软件

激活自己的虚拟环境先下载安装labelimg,要装rolabelimg要先装labelimg

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple  labelImg==1.8.6

在lib-site-packages-下有这两个了。

2.安装rolabelimg

去github下载项目rolabelimg,

链接:https://pan.baidu.com/s/1fthJMm6E0r2zEVaZOiF3Gw 
提取码:xikk,

解压后,进入项目中运行一下命令即可驱动,roLabelImg-master也可以放lib-site-packages

python 路径\roLabelImg.py

 用按键zxcv进行旋转调整角度

 

3、标签可训练格式转换

# 文件名称   :roxml_to_dota.py
# 功能描述   :把rolabelimg标注的xml文件转换成dota能识别的xml文件,
#             再转换成dota格式的txt文件
#            把旋转框 cx,cy,w,h,angle,或者矩形框cx,cy,w,h,转换成四点坐标

3.1 roxml2dotaxml2txt

  1. x1,y1,x2,y2,x3,y3,x4,y4
  2. import os
  3. import xml.etree.ElementTree as ET
  4. import math
  5. cls_list = ['1', 'gj', 'ladder'] #修改为自己的标签
  6. def edit_xml(xml_file, dotaxml_file):
  7. """
  8. 修改xml文件
  9. :param xml_file:xml文件的路径
  10. :return:
  11. """
  12. # dxml_file = open(xml_file,encoding='gbk')
  13. # tree = ET.parse(dxml_file).getroot()
  14. tree = ET.parse(xml_file)
  15. objs = tree.findall('object')
  16. for ix, obj in enumerate(objs):
  17. x0 = ET.Element("x0") # 创建节点
  18. y0 = ET.Element("y0")
  19. x1 = ET.Element("x1")
  20. y1 = ET.Element("y1")
  21. x2 = ET.Element("x2")
  22. y2 = ET.Element("y2")
  23. x3 = ET.Element("x3")
  24. y3 = ET.Element("y3")
  25. # obj_type = obj.find('bndbox')
  26. # type = obj_type.text
  27. # print(xml_file)
  28. if (obj.find('robndbox') == None):
  29. obj_bnd = obj.find('bndbox')
  30. obj_xmin = obj_bnd.find('xmin')
  31. obj_ymin = obj_bnd.find('ymin')
  32. obj_xmax = obj_bnd.find('xmax')
  33. obj_ymax = obj_bnd.find('ymax')
  34. # 以防有负值坐标
  35. xmin = max(float(obj_xmin.text), 0)
  36. ymin = max(float(obj_ymin.text), 0)
  37. xmax = max(float(obj_xmax.text), 0)
  38. ymax = max(float(obj_ymax.text), 0)
  39. obj_bnd.remove(obj_xmin) # 删除节点
  40. obj_bnd.remove(obj_ymin)
  41. obj_bnd.remove(obj_xmax)
  42. obj_bnd.remove(obj_ymax)
  43. x0.text = str(xmin)
  44. y0.text = str(ymax)
  45. x1.text = str(xmax)
  46. y1.text = str(ymax)
  47. x2.text = str(xmax)
  48. y2.text = str(ymin)
  49. x3.text = str(xmin)
  50. y3.text = str(ymin)
  51. else:
  52. obj_bnd = obj.find('robndbox')
  53. obj_bnd.tag = 'bndbox' # 修改节点名
  54. obj_cx = obj_bnd.find('cx')
  55. obj_cy = obj_bnd.find('cy')
  56. obj_w = obj_bnd.find('w')
  57. obj_h = obj_bnd.find('h')
  58. obj_angle = obj_bnd.find('angle')
  59. cx = float(obj_cx.text)
  60. cy = float(obj_cy.text)
  61. w = float(obj_w.text)
  62. h = float(obj_h.text)
  63. angle = float(obj_angle.text)
  64. obj_bnd.remove(obj_cx) # 删除节点
  65. obj_bnd.remove(obj_cy)
  66. obj_bnd.remove(obj_w)
  67. obj_bnd.remove(obj_h)
  68. obj_bnd.remove(obj_angle)
  69. x0.text, y0.text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
  70. x1.text, y1.text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
  71. x2.text, y2.text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
  72. x3.text, y3.text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
  73. # obj.remove(obj_type) # 删除节点
  74. obj_bnd.append(x0) # 新增节点
  75. obj_bnd.append(y0)
  76. obj_bnd.append(x1)
  77. obj_bnd.append(y1)
  78. obj_bnd.append(x2)
  79. obj_bnd.append(y2)
  80. obj_bnd.append(x3)
  81. obj_bnd.append(y3)
  82. tree.write(dotaxml_file, method='xml', encoding='utf-8') # 更新xml文件
  83. # 转换成四点坐标
  84. def rotatePoint(xc, yc, xp, yp, theta):
  85. xoff = xp - xc;
  86. yoff = yp - yc;
  87. cosTheta = math.cos(theta)
  88. sinTheta = math.sin(theta)
  89. pResx = cosTheta * xoff + sinTheta * yoff
  90. pResy = - sinTheta * xoff + cosTheta * yoff
  91. return str(int(xc + pResx)), str(int(yc + pResy))
  92. def totxt(xml_path, out_path):
  93. # 想要生成的txt文件保存的路径,这里可以自己修改
  94. files = os.listdir(xml_path)
  95. i = 0
  96. for file in files:
  97. tree = ET.parse(xml_path + os.sep + file)
  98. root = tree.getroot()
  99. name = file.split('.')[0]
  100. output = out_path + '\\' + name + '.txt'
  101. file = open(output, 'w')
  102. i = i + 1
  103. objs = tree.findall('object')
  104. for obj in objs:
  105. cls = obj.find('name').text
  106. box = obj.find('bndbox')
  107. x0 = int(float(box.find('x0').text))
  108. y0 = int(float(box.find('y0').text))
  109. x1 = int(float(box.find('x1').text))
  110. y1 = int(float(box.find('y1').text))
  111. x2 = int(float(box.find('x2').text))
  112. y2 = int(float(box.find('y2').text))
  113. x3 = int(float(box.find('x3').text))
  114. y3 = int(float(box.find('y3').text))
  115. if x0 < 0:
  116. x0 = 0
  117. if x1 < 0:
  118. x1 = 0
  119. if x2 < 0:
  120. x2 = 0
  121. if x3 < 0:
  122. x3 = 0
  123. if y0 < 0:
  124. y0 = 0
  125. if y1 < 0:
  126. y1 = 0
  127. if y2 < 0:
  128. y2 = 0
  129. if y3 < 0:
  130. y3 = 0
  131. for cls_index, cls_name in enumerate(cls_list):
  132. if cls == cls_name:
  133. file.write("{} {} {} {} {} {} {} {} {} {}\n".format(x0, y0, x1, y1, x2, y2, x3, y3, cls, cls_index))
  134. file.close()
  135. # print(output)
  136. print(i)
  137. if __name__ == '__main__':
  138. # -----**** 第一步:把xml文件统一转换成旋转框的xml文件 ****-----
  139. roxml_path = r'E:\CodeProject\ultralytics-main-OBB\data_transfor\org_xml'
  140. dotaxml_path = r'E:\CodeProject\ultralytics-main-OBB\data_transfor\dota_xml'
  141. out_path = r'E:\CodeProject\ultralytics-main-OBB\data_transfor\dota_txt'
  142. filelist = os.listdir(roxml_path)
  143. for file in filelist:
  144. edit_xml(os.path.join(roxml_path, file), os.path.join(dotaxml_path, file))
  145. # -----**** 第二步:把旋转框xml文件转换成txt格式 ****-----
  146. totxt(dotaxml_path, out_path)

三、配置文件设置

Yolov8_OBB斜框训练自己的数据集手把手教学_yolov8 obb-CSDN博客

四、训练

下载模型预训练权重:

训练:

yolo obb train data=路径\datasets\my-dota8-obb.yaml model=yolov8s-obb.pt epochs=20 imgsz=640 device=0,1,2,3 

参考:

windows下python3安装rolabelimg或者labelimg2标注斜框-CSDN博客

Yolov8_obb(prob loss) 基于anchor_free的旋转框目标检测,剪枝,跟踪(ByteTracker)_yolov8 obb-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/967329
推荐阅读
  

闽ICP备14008679号