yoloV4mosaic数据增强,同步Pascal VOC格式的XML标注信息

  • talk is cheap, show code.

talk is cheap, show code.

from PIL import Image, ImageDraw
import numpy as np
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
import mathimport xml.dom.minidom
import numpy as np
import sys
import cv2
import glob
import os
import xml.dom.minidom
import argparse
import randomfrom xml.etree.ElementTree import ElementTree,Element,parse
from xml.dom import minidom
import xml.etree.ElementTree as ET
import xml.dom.minidom as DOCimage_path = "/home/hs/important-demo/SKU110K/sku_test/retail_face_data/images1000_1000/"
path_origin_xml = "/home/hs/important-demo/SKU110K/sku_test/retail_face_data/annotation1000_1000/"
out_root_path = "/home/hs/important-demo/SKU110K/sku_test/retail_face_data/xml"# 从xml文件中提取bounding box信息, 格式为[[x_min, y_min, x_max, y_max, name]]
def readxml(image_file):# file_path = os.path.join(xml_path, xmlFile)# dom = parse(xml_path)# root = dom.getroot()#xmls_list = os.listdir(path_origin_xml)#nums = len(xmls_list)#coords = list()#for i in range(nums):#xml_path = os.path.join(path_origin_xml, image_file.replace('jpg', 'xml'))if image_file.split(".")[1] == 'png':xml_path = os.path.join(path_origin_xml, image_file.replace('png', 'xml'))else:xml_path = os.path.join(path_origin_xml, image_file.replace('jpg', 'xml'))root = ET.parse(xml_path).getroot()bb = []for obj in root.iter('object'):  # 获取object节点中的name子节点bbox = obj.find('bndbox')
#        name = obj.find('name').textxmin = int(float(bbox.find('xmin').text.strip()))ymin = int(float(bbox.find('ymin').text.strip()))xmax = int(float(bbox.find('xmax').text.strip()))ymax = int(float(bbox.find('ymax').text.strip()))bb.append(np.array([xmin, ymin, xmax, ymax, 1]))return np.array(bb)def CreatXml(imgPath, results, xmlPath):img = cv2.imread(imgPath)imgSize = img.shapeimgName = imgPath.split('/')[-1]impl = xml.dom.minidom.getDOMImplementation()dom = impl.createDocument(None, 'annotation', None)root = dom.documentElementfolder = dom.createElement('folder')root.appendChild(folder)name_folfer = dom.createTextNode('Unknown')folder.appendChild(name_folfer)filename = dom.createElement('filename')root.appendChild(filename)name_img = dom.createTextNode(os.path.splitext(imgName)[0])filename.appendChild(name_img)filepath = dom.createElement('path')root.appendChild(filepath)path_img = dom.createTextNode(imgPath)filepath.appendChild(path_img)source = dom.createElement('source')root.appendChild(source)database = dom.createElement('database')database_name = dom.createTextNode('Unknown')database.appendChild(database_name)source.appendChild(database)img_size = dom.createElement('size')root.appendChild(img_size)width = dom.createElement('width')width_num = dom.createTextNode(str(int(imgSize[1])))width.appendChild(width_num)height = dom.createElement('height')height_num = dom.createTextNode(str(int(imgSize[0])))height.appendChild(height_num)depth = dom.createElement('depth')depth_num = dom.createTextNode(str(int(imgSize[2])))depth.appendChild(depth_num)img_size.appendChild(width)img_size.appendChild(height)img_size.appendChild(depth)segmented = dom.createElement('segmented')root.appendChild(segmented)segmented_num = dom.createTextNode('0')segmented.appendChild(segmented_num)for i in range(len(results)):img_object = dom.createElement('object')root.appendChild(img_object)label_name = dom.createElement('name')namecls = dom.createTextNode(results[i]['name'])label_name.appendChild(namecls)pose = dom.createElement('pose')pose_name = dom.createTextNode('Unspecified')pose.appendChild(pose_name)truncated = dom.createElement('truncated')truncated_num = dom.createTextNode('0')truncated.appendChild(truncated_num)difficult = dom.createElement('difficult')difficult_num = dom.createTextNode('0')difficult.appendChild(difficult_num)bndbox = dom.createElement('bndbox')xmin = dom.createElement('xmin')xmin_num = dom.createTextNode(str(int(results[i]['bbox'][0])))xmin.appendChild(xmin_num)ymin = dom.createElement('ymin')ymin_num = dom.createTextNode(str(int(results[i]['bbox'][1])))ymin.appendChild(ymin_num)xmax = dom.createElement('xmax')xmax_num = dom.createTextNode(str(int(results[i]['bbox'][2])))xmax.appendChild(xmax_num)ymax = dom.createElement('ymax')ymax_num = dom.createTextNode(str(int(results[i]['bbox'][3])))ymax.appendChild(ymax_num)bndbox.appendChild(xmin)bndbox.appendChild(ymin)bndbox.appendChild(xmax)bndbox.appendChild(ymax)img_object.appendChild(label_name)img_object.appendChild(pose)img_object.appendChild(truncated)img_object.appendChild(difficult)img_object.appendChild(bndbox)f = open(xmlPath, 'w')dom.writexml(f, addindent='  ', newl='\n')f.close()def get_result(box_data):classes = ['object', 'scissors', 'lighter', 'zippooil', 'pressure', 'slingshot', 'handcuffs', 'nailpolish', 'powerbank', 'firecrackers']results = []for obj in box_data:result = {}obj = [int(i) for i in obj]box = obj[:4]name = classes[obj[-1]]result["name"] = nameresult["bbox"] = boxresults.append(result)return resultsdef rand(a=0, b=1):return np.random.rand()*(b-a) + adef merge_bboxes(bboxes, cutx, cuty):merge_bbox = []for i in range(len(bboxes)):for box in bboxes[i]:tmp_box = []x1,y1,x2,y2 = box[0], box[1], box[2], box[3]if i == 0:if y1 > cuty or x1 > cutx:continueif y2 >= cuty and y1 <= cuty:y2 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x2 = cutxif x2-x1 < 5:continueif i == 1:if y2 < cuty or x1 > cutx:continueif y2 >= cuty and y1 <= cuty:y1 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x2 = cutxif x2-x1 < 5:continueif i == 2:if y2 < cuty or x2 < cutx:continueif y2 >= cuty and y1 <= cuty:y1 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x1 = cutxif x2-x1 < 5:continueif i == 3:if y1 > cuty or x2 < cutx:continueif y2 >= cuty and y1 <= cuty:y2 = cutyif y2-y1 < 5:continueif (x2 >= cutx) and (x1 <= cutx):x1 = cutxif x2-x1 < 5:continuetmp_box.append(x1)tmp_box.append(y1)tmp_box.append(x2)tmp_box.append(y2)tmp_box.append(box[-1])merge_bbox.append(tmp_box)return merge_bboxdef get_random_data(annotation_line, input_shape, random=True, hue=.1, sat=1.5, val=1.5, proc_img=True):'''random preprocessing for real-time data augmentation'''print("image:",annotation_line)h, w = input_shapemin_offset_x = 0.4min_offset_y = 0.4scale_low = 1-min(min_offset_x,min_offset_y)scale_high = scale_low+0.2image_datas = []box_datas = []index = 0place_x = [0,0,int(w*min_offset_x),int(w*min_offset_x)]place_y = [0,int(h*min_offset_y),int(w*min_offset_y),0]for line in annotation_line:# 每一行进行分割#line_content = line.split()# 打开图片image = Image.open(os.path.join(image_path,line))image = image.convert("RGB")# 图片的大小iw, ih = image.sizebox = readxml(line)# 保存框的位置#box = np.array([np.array(list(map(int,box.split(',')))) for box in line_content[1:]])# image.save(str(index)+".jpg")# 是否翻转图片flip = rand()<.5if flip and len(box)>0:image = image.transpose(Image.FLIP_LEFT_RIGHT)box[:, [0,2]] = iw - box[:, [2,0]]# 对输入进来的图片进行缩放new_ar = w/hscale = rand(scale_low, scale_high)if new_ar < 1:nh = int(scale*h)nw = int(nh*new_ar)else:nw = int(scale*w)nh = int(nw/new_ar)image = image.resize((nw,nh), Image.BICUBIC)# 进行色域变换hue = rand(-hue, hue)sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)val = rand(1, val) if rand()<.5 else 1/rand(1, val)x = rgb_to_hsv(np.array(image)/255.)x[..., 0] += huex[..., 0][x[..., 0]>1] -= 1x[..., 0][x[..., 0]<0] += 1x[..., 1] *= satx[..., 2] *= valx[x>1] = 1x[x<0] = 0image = hsv_to_rgb(x)image = Image.fromarray((image*255).astype(np.uint8))# 将图片进行放置,分别对应四张分割图片的位置dx = place_x[index]dy = place_y[index]new_image = Image.new('RGB', (w,h), (128,128,128))new_image.paste(image, (dx, dy))image_data = np.array(new_image)/255index = index + 1box_data = []# 对box进行重新处理if len(box)>0:np.random.shuffle(box)box[:, [0,2]] = box[:, [0,2]]*nw/iw + dxbox[:, [1,3]] = box[:, [1,3]]*nh/ih + dybox[:, 0:2][box[:, 0:2]<0] = 0box[:, 2][box[:, 2]>w] = wbox[:, 3][box[:, 3]>h] = hbox_w = box[:, 2] - box[:, 0]box_h = box[:, 3] - box[:, 1]box = box[np.logical_and(box_w>1, box_h>1)]box_data = np.zeros((len(box),5))box_data[:len(box)] = boximage_datas.append(image_data)box_datas.append(box_data)img = Image.fromarray((image_data*255).astype(np.uint8))#for j in range(len(box_data)):#    thickness = 3#    left, top, right, bottom = box_data[j][0:4]#    draw = ImageDraw.Draw(img)#    for i in range(thickness):#        draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))#img.show()# 将图片分割,放在一起cutx = np.random.randint(int(w*min_offset_x), int(w*(1 - min_offset_x)))cuty = np.random.randint(int(h*min_offset_y), int(h*(1 - min_offset_y)))print("cutx:",cutx)print("cuty:",cuty)print("h:",h)print("w:",w)new_image = np.zeros([h,w,3])new_image[:cuty, :cutx, :] = image_datas[0][:cuty, :cutx, :]new_image[cuty:, :cutx, :] = image_datas[1][cuty:, :cutx, :]new_image[cuty:, cutx:, :] = image_datas[2][cuty:, cutx:, :]new_image[:cuty, cutx:, :] = image_datas[3][:cuty, cutx:, :]# 对框进行进一步的处理new_boxes = merge_bboxes(box_datas, cutx, cuty)return new_image, new_boxesdef normal_(annotation_line, input_shape):'''random preprocessing for real-time data augmentation'''line = annotation_line.split()image = Image.open(line[0])box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])iw, ih = image.sizeimage = image.transpose(Image.FLIP_LEFT_RIGHT)box[:, [0,2]] = iw - box[:, [2,0]]return image, boxif __name__ == "__main__":lines = []for filename in os.listdir(os.path.join(image_path, '')):if filename.endswith(".jpg") or filename.endswith(".JPG") or filename.endswith(".png"):lines.append(filename)print(lines)list1 = list(range(0,len(lines)))print("list1:",list1)for j in range(5000):#a = np.random.randint(0,len(lines))#line = lines[a:a+4]tem = []for i in random.sample(list1, 4):tem.append(lines[i])line = tem#try:image_data, box_data = get_random_data(line,[1000,1000])img = Image.fromarray((image_data*255).astype(np.uint8))img_path = "/home/hs/important-demo/SKU110K/sku_test/retail_face_data/img/%s.jpg" % jimg.save(img_path)results = get_result(box_data)xml_path = "/home/hs/important-demo/SKU110K/sku_test/retail_face_data/xml/%s.xml" % jCreatXml(img_path, results, xml_path)#except:#continue

yoloV4mosaic数据增强,同步Pascal VOC格式的XML标注信息相关推荐

  1. 后缀为labels的文件_txt标注文档转换为labelmg VOC格式的xml标注文件

    清理库存8~ #! /usr/bin/python # -*- coding:UTF-8 -*- import os, sys import glob from PIL import Image ## ...

  2. 英文数据集txt_如何用自己的数据制作 Pascal VOC 格式的数据集 详细教程(文中有所有代码)...

    目前object detection这块主流的数据集主要就是COCO和Pascal VOC格式的.github上现成的检测算法基本都是自带VOC格式数据集的输入接口的,所以想要跑起来一个算法,我们需要 ...

  3. 建立自己的voc数据集_一次将自己的数据集制作成PASCAL VOC格式的惨痛经历

    因为准备训练keras-yolo3,开源代码上给出了voc_annotation.py文件,只要将自己的数据格式处理成PASCAL VOC格式,那么运行voc_annotation.py就可以将自己的 ...

  4. 玩转肺癌目标检测数据集Lung-PET-CT-Dx ——④转换成PASCAL VOC格式数据集

    文章目录 关于PASCAL VOC数据集 目录结构 ①创建VOC数据集的几个相关目录 XML文件的形式 ②读取dcm文件与xml文件的配对关系 ③创建VOC格式数据集 ④创建训练.验证集 本文所用代码 ...

  5. labelimg标注的VOC格式标签xml文件和yolo格式标签txt文件相互转换

    目录 1 labelimg标注VOC格式和yolo格式介绍 1.1 voc格式 1.2 yolo数据格式介绍 2 voc格式数据和yolo格式数据相互转换 2.1 voc转yolo代码 2.2 yol ...

  6. 多个xml文件转coco格式、coco转VOC格式(xml)

    1.多个xml文件转coco格式 bug已改,已经试过可以用. 只需要修改三个地方: 将 xml_folder 变量中的路径改为你自己的 xml 文件所在文件夹路径. 将 class_name 变量中 ...

  7. 把一个dataset的表放在另一个dataset里面_视频自监督一. STCR: 一个基于数据增强的简单有效正则项 (降低静态信息的影响)...

    视频自监督一. STCR: 一个基于数据增强的简单有效正则项 (降低静态信息的影响) 今天介绍一个我们近期做的关于视频自监督的简单工作: Self-supervised learning using ...

  8. yolo数据增强以及批量修改图片和xml名

    记录下打完标签对数据集进行扩增,数据增强后的图片及标签名字进行修改,重点在代码只需更改文件名就可使用 无论数据增强还是修改名称,标签框位置都会跟着改变!!! 前人之鉴,最好还是数据增强后再去打标签,千 ...

  9. 使用python将COCO格式的json文件转化为VOC格式的xml文件

    起因是得到一批标准的COCO数据集(json文件),自己后来又手动标了一批数据,但是标注出来的文件是VOC格式,现在想要把这些数据合并到一起,再转成标准的COCO数据集用于训练. 个人觉得json转x ...

最新文章

  1. 青龙羊毛——内容改版
  2. mysql-3 检索数据(1)
  3. 分布式与人工智能课程(part16)--深度学习
  4. mybatis报错Type interface xxx.Dao is not...
  5. 计算机网络流媒体播放,流媒体播放方式包含以下哪几种方式
  6. android 字体点击变色,Android TextView 中实现部分文字变色以及点击事件
  7. json解析 spark_PySpark算子处理空间数据全解析(8):构造空间数据的RDD(2)
  8. ModuleNotFoundError: No module named ‘pygame’——Python3.6安装pip并下载pygame模块
  9. 华为NP课程笔记26-VXLAN概述
  10. android mms 广播,Android-如何成为第一个接收WAP PUSH(MMS)的人
  11. 4071 国际象棋(枚举)
  12. windows无法完成系统配置。若要尝试恢复配置,请重新启动计算机。出现此情况怎么处理,Win7封装Windows无法完成系统配置解决方案...
  13. 平板连接远程Linux,如何从Android平板电脑远程控制Ubuntu | MOS86
  14. linux、ubuntu如何查看网速
  15. 100道积分公式证明(41-50)
  16. 最小二乘法及应用实例
  17. PHPMyWind支持ppt上传
  18. 区块链安全性(区块链应用)
  19. 苹果正式发秋季发布会邀请函:9月10日乔布斯剧院
  20. pptv网络电视2014 v3.5.2.0017 免费版

热门文章

  1. AVP的商业化之路还有多远?
  2. ArcGIS教程:山地风景区景观规划中的可视性分析
  3. xcode 项目 was compiled with optimization -stepping may behave oddly ;variable may not be available
  4. yt-dlp教程如何下载高清视频
  5. [数学知识][几何]求三角形面积的几种方法
  6. CentOS 7输入startx无法启动图形化界面
  7. 福利:学生免费注册使用JB全家桶
  8. 浅尝webSocket
  9. 红亚2015-3月杯季赛 CTF题部分writeup
  10. opj线性表Placing apples 题解