如上图所示,我们的目标是切割出所有的盘子,没个盘子单独储存为一张图片,并且里面的水果也还在该盘子的对应位置。
类似于这样

因为都标注了,有位置信息了,通过大目标框和小目标框的相对位置完全可以切出来。若再标注那工作量就太大了。

处理步骤

  1. 获取大框和小框的位置信息
  2. 检查小框是否在大框内,并坐标转换,依据大小框的位置信息写入yolo格式数据集
  3. 切割大框
  4. 将yolo数据集转为voc数据集

获取大框和小框的位置信息

通过elementTree解析xml文件,没有elementTree基础的可以看下此文档:Python 解析 voc数据集的xml文件

提取test1.xml文件中的所有plate节点,并将相关位置信息存放到字典中

字典结构为:
{plate_1: [xmin,ymin,xmax,ymax,w,h],plate_2: [xmin,ymin,xmax,ymax,w,h]}

file = 'test1.xml'
tree = ET.parse(file)
root = tree.getroot()all_C_sd_n = dict()
file_name = root.find('filename').text[:-4]i = 0
for obj in root.iter('object'):names = obj.find('name')if names.text == 'plate':i += 1sd_set = []box = obj.find('bndbox')# 大框的x,y坐标sd_set.append(int(box.find('xmin').text))sd_set.append(int(box.find('ymin').text))sd_set.append(int(box.find('xmax').text))sd_set.append(int(box.find('ymax').text))# 大框的宽和高w = int(box.find('xmax').text)-int(box.find('xmin').text)h = int(box.find('ymax').text)-int(box.find('ymin').text)sd_set.append(w)sd_set.append(h)# 字典格式为  file_name_1: xmin,ymin,xmax,ymax,w,hall_C_sd_n.update({file_name + '_' + str(i): sd_set})

检查小框是否在大框内

获取小框位置坐标与大框的一样,主要是用大框的xmin ymin肯定要比小框的中心位置小,xmax ymax比小框的中心位置大

坐标转换为

# 将 xmin xmax ymin ymax 转为coco格式的 xwyh
def convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = (box[0] + box[1]) / 2.0 - 1y = (box[2] + box[3]) / 2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn x, y, w, h
for obj in root.iter('object'):names = obj.find('name')if names.text in ['C_sdh_n', 'C_wirerope_n', 'C_wirerope_notbinding']:box = obj.find('bndbox')xm = int(box.find('xmin').text) + (int(box.find('xmax').text) - int(box.find('xmin').text)) / 2ym = int(box.find('ymin').text) + (int(box.find('ymax').text) - int(box.find('ymin').text)) / 2for box_nb in all_C_sd_n.items():# print(box_nb)if box_nb[1][0] < xm < box_nb[1][2] and box_nb[1][1] < ym < box_nb[1][3]:# 这里的坐标为小框相对于大框的坐标xmin = int(box.find('xmin').text) - box_nb[1][0]ymin = int(box.find('ymin').text) - box_nb[1][1]xmax = int(box.find('xmax').text) - box_nb[1][0]ymax = int(box.find('ymax').text) - box_nb[1][1]w = box_nb[1][4]h = box_nb[1][5]b = (xmin, xmax, ymin, ymax)x, y, w, h = convert((w, h), b)print(x, y, w, h)out_file = open('{}.txt'.format(box_nb[0]), 'a')if names.text == 'C_wirerope_n':out_file.write('{} {} {} {} {}\n'.format(0, x, y, w, h))#     print(box_nb[0], 0, x, y, w, h)if names.text == 'C_wirerope_notbinding':out_file.write('{} {} {} {} {}\n'.format(1, x, y, w, h))# #     print(box_nb[0],1, x, y, w, h)if names.text == 'C_sdh_n':# print(box_nb[0],2, x, y, w, h)out_file.write('{} {} {} {} {}\n'.format(2, x, y, w, h))

切割大框

OpenCV的基础操作

img = cv2.imread("1_001344.742_16_.jpg")
for box in all_C_sd_n.items():# box[1][1]:box[1][3]  ymin——>ymax  box[1][0]:box[1][2] xmax-xmincut_img = img[box[1][1]:box[1][3],box[1][0]:box[1][2]]# cv2.imshow(box[0], cut_img)cv2.imwrite(box[0]+'.png', cut_img)
import xml.etree.ElementTree as ET
import cv2# 将 xmin xmax ymin ymax 转为coco格式的 xwyh
def convert(size, box):dw = 1. / (size[0])dh = 1. / (size[1])x = (box[0] + box[1]) / 2.0 - 1y = (box[2] + box[3]) / 2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn x, y, w, hfile = 'test1.xml'
tree = ET.parse(file)
root = tree.getroot()all_C_sd_n = dict()
file_name = root.find('filename').text[:-4]i = 0
for obj in root.iter('object'):names = obj.find('name')if names.text == 'C_sd_n':i += 1sd_set = []box = obj.find('bndbox')# 大框的x,y坐标sd_set.append(int(box.find('xmin').text))sd_set.append(int(box.find('ymin').text))sd_set.append(int(box.find('xmax').text))sd_set.append(int(box.find('ymax').text))# 大框的宽和高w = int(box.find('xmax').text)-int(box.find('xmin').text)h = int(box.find('ymax').text)-int(box.find('ymin').text)sd_set.append(w)sd_set.append(h)# 字典格式为  file_name_1: xmin,ymin,xmax,ymax,w,hall_C_sd_n.update({file_name + '_' + str(i): sd_set})# 切割C_sd_n框
# img = cv2.imread("1_001344.742_16_.jpg")
# for box in all_C_sd_n.items():
#     # box[1][1]:box[1][3]  ymin——>ymax  box[1][0]:box[1][2] xmax-xmin
#     cut_img = img[box[1][1]:box[1][3],box[1][0]:box[1][2]]
#     # cv2.imshow(box[0], cut_img)
#     cv2.imwrite(box[0]+'.png', cut_img)for obj in root.iter('object'):names = obj.find('name')if names.text in ['C_sdh_n', 'C_wirerope_n', 'C_wirerope_notbinding']:box = obj.find('bndbox')xm = int(box.find('xmin').text) + (int(box.find('xmax').text) - int(box.find('xmin').text)) / 2ym = int(box.find('ymin').text) + (int(box.find('ymax').text) - int(box.find('ymin').text)) / 2for box_nb in all_C_sd_n.items():# print(box_nb)if box_nb[1][0] < xm < box_nb[1][2] and box_nb[1][1] < ym < box_nb[1][3]:# print('xmin','ymin','xmax','ymax')# print(int(box.find('xmin').text),int(box.find('ymin').text),int(box.find('xmax').text),int(box.find('ymax').text))# print(b[0],xm, ym)# 这里的坐标为小框相对于大框的坐标xmin = int(box.find('xmin').text) - box_nb[1][0]ymin = int(box.find('ymin').text) - box_nb[1][1]xmax = int(box.find('xmax').text) - box_nb[1][0]ymax = int(box.find('ymax').text) - box_nb[1][1]w = box_nb[1][4]h = box_nb[1][5]# print('x1, y1, x2, y2, w, h')# print(x1, y1, x2, y2, w, h)b = (xmin, xmax, ymin, ymax)# b = (int(box.find('xmin').text),# int(box.find('xmax').text),# int(box.find('ymin').text),# int(box.find('ymax').text))x, y, w, h = convert((w, h), b)print(x, y, w, h)out_file = open('{}.txt'.format(box_nb[0]), 'a')if names.text == 'C_wirerope_n':out_file.write('{} {} {} {} {}\n'.format(0, x, y, w, h))#     print(box_nb[0], 0, x, y, w, h)if names.text == 'C_wirerope_notbinding':out_file.write('{} {} {} {} {}\n'.format(1, x, y, w, h))# #     print(box_nb[0],1, x, y, w, h)if names.text == 'C_sdh_n':# print(box_nb[0],2, x, y, w, h)out_file.write('{} {} {} {} {}\n'.format(2, x, y, w, h))

yolo格式数据集转cov

from xml.dom.minidom import Document
import os
import cv2def makexml(picPath, txtPath, xmlPath):  # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml"""dic = {'0': "apple",  # 创建字典用来对类型进行转换'1': "orange",  # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致}files = os.listdir(txtPath)for i, name in enumerate(files):xmlBuilder = Document()annotation = xmlBuilder.createElement("annotation")  # 创建annotation标签xmlBuilder.appendChild(annotation)txtFile = open(txtPath + name)txtList = txtFile.readlines()img = cv2.imread(picPath + name[0:-4] + ".png")# print()Pheight, Pwidth, Pdepth = img.shapefolder = xmlBuilder.createElement("folder")  # folder标签foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")folder.appendChild(foldercontent)annotation.appendChild(folder)  # folder标签结束filename = xmlBuilder.createElement("filename")  # filename标签filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".png")filename.appendChild(filenamecontent)annotation.appendChild(filename)  # filename标签结束size = xmlBuilder.createElement("size")  # size标签width = xmlBuilder.createElement("width")  # size子标签widthwidthcontent = xmlBuilder.createTextNode(str(Pwidth))width.appendChild(widthcontent)size.appendChild(width)  # size子标签width结束height = xmlBuilder.createElement("height")  # size子标签heightheightcontent = xmlBuilder.createTextNode(str(Pheight))height.appendChild(heightcontent)size.appendChild(height)  # size子标签height结束depth = xmlBuilder.createElement("depth")  # size子标签depthdepthcontent = xmlBuilder.createTextNode(str(Pdepth))depth.appendChild(depthcontent)size.appendChild(depth)  # size子标签depth结束annotation.appendChild(size)  # size标签结束for j in txtList:oneline = j.strip().split(" ")object = xmlBuilder.createElement("object")  # object 标签picname = xmlBuilder.createElement("name")  # name标签namecontent = xmlBuilder.createTextNode(dic[oneline[0]])picname.appendChild(namecontent)object.appendChild(picname)  # name标签结束pose = xmlBuilder.createElement("pose")  # pose标签posecontent = xmlBuilder.createTextNode("Unspecified")pose.appendChild(posecontent)object.appendChild(pose)  # pose标签结束truncated = xmlBuilder.createElement("truncated")  # truncated标签truncatedContent = xmlBuilder.createTextNode("0")truncated.appendChild(truncatedContent)object.appendChild(truncated)  # truncated标签结束difficult = xmlBuilder.createElement("difficult")  # difficult标签difficultcontent = xmlBuilder.createTextNode("0")difficult.appendChild(difficultcontent)object.appendChild(difficult)  # difficult标签结束bndbox = xmlBuilder.createElement("bndbox")  # bndbox标签xmin = xmlBuilder.createElement("xmin")  # xmin标签mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)xminContent = xmlBuilder.createTextNode(str(mathData))xmin.appendChild(xminContent)bndbox.appendChild(xmin)  # xmin标签结束ymin = xmlBuilder.createElement("ymin")  # ymin标签mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)yminContent = xmlBuilder.createTextNode(str(mathData))ymin.appendChild(yminContent)bndbox.appendChild(ymin)  # ymin标签结束xmax = xmlBuilder.createElement("xmax")  # xmax标签mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)xmaxContent = xmlBuilder.createTextNode(str(mathData))xmax.appendChild(xmaxContent)bndbox.appendChild(xmax)  # xmax标签结束ymax = xmlBuilder.createElement("ymax")  # ymax标签mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)ymaxContent = xmlBuilder.createTextNode(str(mathData))ymax.appendChild(ymaxContent)bndbox.appendChild(ymax)  # ymax标签结束object.appendChild(bndbox)  # bndbox标签结束annotation.appendChild(object)  # object标签结束f = open(xmlPath + name[0:-4] + ".xml", 'w')xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()if __name__ == "__main__":picPath = "img/"  # 图片所在文件夹路径,后面的/一定要带上txtPath = "txt/"  # txt所在文件夹路径,后面的/一定要带上xmlPath = "Annotations/"  # xml文件保存路径,后面的/一定要带上makexml(picPath, txtPath, xmlPath)

yolo数据集剪裁:切割目标框并将该框内的其他目标一并提取并转为可用数据集相关推荐

  1. 对象检测目标小用什么模型好_自动驾驶目标检测- YOLO v3 深入解析

    从2016年 Joseph Redmon 发布第一代YOLO开始,YOLO已经更新四代了,凭借着在一个网络模型中完成对图像中所有对象边界框和类别预测的独特创新,成为现今使用最广泛也是最快的对象检测算法 ...

  2. YOLO 超详细入门(含开源代码)——网络结构、细节、目标损失函数、优点

    文章目录 前言 背景 一.YOLO的核心原理预览 二.网络结构 为什么每个网格有固定的B个Bounding Boes?(即B=2) 三.网络细节 3.1 网络单元(grid) 3.1.1 作用 3.1 ...

  3. 探讨:COCO2017数据集中包含很多目标被标注为一个框bbox,是标注错误吗?

    文章目录: 1 问题场景 2 使用labelImg查看COCO2017数据会出现很多目标被标注为一个框bbox 3 相关探讨 1 问题场景 由于COCO2017数据集具有: 类别多样性,有80个类别 ...

  4. yolo v3制作自己的数据_小白也能弄懂的目标检测之YOLO系列 第一期

    大家好,上期分享了电脑端几个免费无广告且实用的录屏软件,这期想给大家来讲解YOLO这个算法,从零基础学起,并最终学会YOLOV3的Pytorch实现,并学会自己制作数据集进行模型训练,然后用自己训练好 ...

  5. ECCV18 Oral | CornerNet目标检测开启预测“边界框”到预测“点对”的新思路

    性能超过所有one-stage类型目标检测算法,训练和测试代码均已开源. 本文来自ECCV2018 Oral论文<CornerNet: Detecting Objects as Paired K ...

  6. ICCV2021 新增数据集汇总 | 含时序动作定位、时空行为检测、弱光目标检测等!...

    点击下方"计算机视觉联盟",一起进步!重磅干货,第一时间送达 ICCV 2021 数据集汇总 https://github.com/DWCTOD/ICCV2021-Papers-w ...

  7. 带你读AI论文丨用于目标检测的高斯检测框与ProbIoU

    摘要:本文解读了<Gaussian Bounding Boxes and Probabilistic Intersection-over-Union for Object Detection&g ...

  8. Windows下一个可与其他数据集控件结合的通用的搜索框GUSIconEdit

    当初在两年多前开始做现在这个项目时,我们最初决定花长时间来升级现有的游戏引擎,包括服务器和客户端,我负责了一部分的客户端的功能升级和配套工具集的开发和升级,我们这套引擎的工具集包含工具比较多,每个工具 ...

  9. R语言入门之创建数据集——向量、矩阵、数组、数据框和列表

    码字不易,转发请注明出处:http://blog.csdn.net/qq_28945021/article/details/52100765 摘要 随着大数据的火爆发展,适合数据分析及生成图表的R语言 ...

最新文章

  1. 后台取得客户端控件的值(ListBox)
  2. eeglab中文教程系列(10)-利用光谱选项绘制ERP图像
  3. MySQL的binary类型操作
  4. myeclipse10安装了activiti插件后创建BPMN 文件时报错,
  5. html 查询表单,如何让我的HTML表单查询适用于所有表单元素?
  6. 细谈C语言中的strcpy,strncpy,memcpy,memmove,memset函数
  7. MOSS2007 无法上传超过30M或者50M的大文件解决办法 (转)
  8. 批处理命令无法连续执行
  9. Grub2主题修改和美化--------LinuxWindows
  10. Eigen 矩阵计算工具
  11. 关于Qt::WA_TransparentForMouseEvents的一些记录
  12. linux进入cbq文件夹,Linux流量控制(SFQ/TBFPRIO/CBQ/HTB原理介绍)
  13. 软件工程_东师站_总结
  14. 这款耳机亲测,性价比堪比 AirPods
  15. 阿里云国际站和阿里云国内站有什么区别?
  16. dubbo整体设计整理
  17. STM32开发GPRS传输的GPS定位器 C#编写服务器转发程序,客户端显示轨迹
  18. 当前发布的sku信息中包含疑似与商品无关的字段,请核实修改后重新提交
  19. 一个高中生是怎么玩自媒体的?
  20. 企业管理常用缩写术语之中英文对照表(含解释)

热门文章

  1. UE4 无需切线空间应用凹凸贴图
  2. 视觉SLAM十四讲笔记-第四讲 李群与李代数
  3. dva介绍--Dva概念(二)
  4. 如何保存卡住的wps_我告诉你wps卡住了怎么办
  5. Nacos的连接拒接丶解决方案
  6. 订单系统设计 —— 订单管理
  7. 网站图片定位代码html5,CSS中背景图片的定位
  8. 王道程序员求职宝典 pdf
  9. 使用excel进行数据挖掘(4)---- 突出显示异常值
  10. 什么手势使用电子计算机比较快,手势功能