CityScapes数据集转voc数据格式训练yolov5(含下载链接)

  • 1、CityScapes数据集数据格式:
  • 2、数据处理
  • 3、CityScapes数据集转VOC格式转换代码

CityScapes数据集需要官网注册才能使用,让人头疼无比,更可气的是国内还有些混蛋竟然将从官方下载好的数据集收费,真可无耻之极,找了好多博客,都是是收费的,看到这些博客,我反手就是一个举报,不为别的,就为它不要脸!!!
这里分享一个CityScapes数据集数据集百度云链接,感谢这个好人的无私分享:分享资料的好人csdn
CityScapes数据集百度云链接
提取码:1uxk

1、CityScapes数据集数据格式:

目录如下所示:

数据集中的类别标签包含如下(种类是真的多,也蛮丰满的):

{'caravan', 'wall', 'person', 'terrain', 'dynamic', 'sky', 'traffic light', 'license plate', 'ground', 'ego vehicle', 'guard rail', 'ridergroup', 'truck', 'sidewalk', 'trailer', 'polegroup', 'rectification border','truckgroup', 'parking', 'building', 'tunnel', 'traffic sign', 'pole', 'bicycle', 'vegetation', 'cargroup', 'motorcyclegroup', 'bus', 'train', 'car', 'static', 'out of roi', 'persongroup', 'bridge', 'rail track', 'fence', 'rider', 'motorcycle', 'bicyclegroup', 'road'}

我主要是打算用该数据集进行行人、车辆相关检测的,所需要的图像在leftImg8bit文件目录里面,对应的标注文件都在压缩文件gtFine,赶文件解压后,如下截图所示,

对,没看错,所需的标注文件都被包含在json格式的文件内:
打开json文件后,标签对应如下所示:

2、数据处理

(1)将leftImg8bit目录内的所有图像与gtFine目录内的所有json分别汇聚在两个目录下面,命名格式如下所示:

json数据可视化查看:

import os, json, cv2, shutil
from tqdm import tqdm
import numpy as np# 转换xml文件
def bboxes2xml(folder, img_name, width, height, gts, xml_save_to):xml_file = open((xml_save_to + '/' + img_name + '.xml'), 'w')xml_file.write('<annotation>\n')xml_file.write('    <folder>' + folder + '</folder>\n')xml_file.write('    <filename>' + str(img_name) + '.jpg' + '</filename>\n')xml_file.write('    <size>\n')xml_file.write('        <width>' + str(width) + '</width>\n')xml_file.write('        <height>' + str(height) + '</height>\n')xml_file.write('        <depth>3</depth>\n')xml_file.write('    </size>\n')for gt in gts:xml_file.write('    <object>\n')xml_file.write('        <name>' + str(gt[0]) + '</name>\n')xml_file.write('        <pose>Unspecified</pose>\n')xml_file.write('        <truncated>0</truncated>\n')xml_file.write('        <difficult>0</difficult>\n')xml_file.write('        <bndbox>\n')xml_file.write('            <xmin>' + str(gt[1]) + '</xmin>\n')xml_file.write('            <ymin>' + str(gt[2]) + '</ymin>\n')xml_file.write('            <xmax>' + str(gt[3]) + '</xmax>\n')xml_file.write('            <ymax>' + str(gt[4]) + '</ymax>\n')xml_file.write('        </bndbox>\n')xml_file.write('    </object>\n')xml_file.write('</annotation>')xml_file.close()def seeImage(windowName, imageData):"""显示图像:param windowName:  窗体名称:param imageData:   图像数据"""cv2.imshow(windowName, imageData)cv2.waitKey(0)# cv2.destroyAllWindows()def drawRectangle(image):"""查看轮廓的宽、高比率:param image: 图像"""contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)imageOne = np.ones((image.shape[0], image.shape[1], 3), dtype=np.uint8) * 255font = cv2.FONT_HERSHEY_SIMPLEX  # 字体样式for index in range(len(contours)):x, y, w, h = cv2.boundingRect(contours[index])ratioW_H = round(float(w) / h, 2)Area = w * htxtInfo = str(Area) + "_" + str(ratioW_H)cv2.putText(imageOne, txtInfo, (x, y), font, 0.4, (255, 255, 0), 1)imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 1)print(txtInfo)seeImage("imageOne", imageOne)def showImage(tempFileDir,tempImageDir):jsonNames = os.listdir(tempFileDir)# labelList = ['person', 'truck', 'trailer', 'bicycle', 'bus', 'train', 'car', 'rider', 'motorcycle']     # 我只需要这几类for jsonName in jsonNames:jsonName_ = os.path.splitext(jsonName)[0]imageName = jsonName_ + ".png"imagePath = os.path.join(tempImageDir, imageName)  # 拼接图像路径jsonPath = os.path.join(tempFileDir, jsonName)  # 拼接json路径imageData = cv2.imread(imagePath)imageOne = np.ones((imageData.shape[0], imageData.shape[1], 3), dtype=np.uint8) * 255filePath = os.path.join(tempFileDir, jsonPath)with open(filePath, "r", encoding="utf-8") as f:info = f.read()infoLoads = json.loads(info)h_image = infoLoads["imgHeight"]  # 图像的高度w_image = infoLoads["imgWidth"]  # 图像的宽度objects = infoLoads["objects"]gts = []for object in objects:temp_gt = []objectName = object["label"]# if objectName not in labelList:#     continueif objectName == "motorcycle":temp_gt.append("motorbike")else:temp_gt.append(objectName)polygon_ = object["polygon"]# print(polygon_)pots = np.asarray(polygon_, np.int32)cv2.polylines(imageOne,[pots],True,(0,255,0),2)seeImage("imageOne",imageOne)if __name__ == '__main__':tempFileDir = r"E:\666\new_json"  # json文件所在目录tempImageDir = r"E:\666\new_image"  # 图像文件所在目录showImage(tempFileDir,tempImageDir)

利用以上代码将json中的内容读出后,物体轮廓是这样滴:是不是很性感,像极了爱情的颜色!!!

3、CityScapes数据集转VOC格式转换代码

接下来进入正题,开始将json格式的数据转为训练所需的xml格式

import os, json, cv2, shutil
from tqdm import tqdm
import numpy as np# 转换xml文件
def bboxes2xml(folder, img_name, width, height, gts, xml_save_to):xml_file = open((xml_save_to + '/' + img_name + '.xml'), 'w')xml_file.write('<annotation>\n')xml_file.write('    <folder>' + folder + '</folder>\n')xml_file.write('    <filename>' + str(img_name) + '.jpg' + '</filename>\n')xml_file.write('    <size>\n')xml_file.write('        <width>' + str(width) + '</width>\n')xml_file.write('        <height>' + str(height) + '</height>\n')xml_file.write('        <depth>3</depth>\n')xml_file.write('    </size>\n')for gt in gts:xml_file.write('    <object>\n')xml_file.write('        <name>' + str(gt[0]) + '</name>\n')xml_file.write('        <pose>Unspecified</pose>\n')xml_file.write('        <truncated>0</truncated>\n')xml_file.write('        <difficult>0</difficult>\n')xml_file.write('        <bndbox>\n')xml_file.write('            <xmin>' + str(gt[1]) + '</xmin>\n')xml_file.write('            <ymin>' + str(gt[2]) + '</ymin>\n')xml_file.write('            <xmax>' + str(gt[3]) + '</xmax>\n')xml_file.write('            <ymax>' + str(gt[4]) + '</ymax>\n')xml_file.write('        </bndbox>\n')xml_file.write('    </object>\n')xml_file.write('</annotation>')xml_file.close()def seeImage(windowName, imageData):"""显示图像:param windowName:  窗体名称:param imageData:   图像数据"""cv2.imshow(windowName, imageData)cv2.waitKey(0)# cv2.destroyAllWindows()def drawRectangle(image):"""查看轮廓的宽、高比率:param image: 图像"""contours, hierarchy = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)imageOne = np.ones((image.shape[0], image.shape[1], 3), dtype=np.uint8) * 255font = cv2.FONT_HERSHEY_SIMPLEX  # 字体样式for index in range(len(contours)):x, y, w, h = cv2.boundingRect(contours[index])ratioW_H = round(float(w) / h, 2)Area = w * htxtInfo = str(Area) + "_" + str(ratioW_H)cv2.putText(imageOne, txtInfo, (x, y), font, 0.4, (255, 255, 0), 1)imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 1)print(txtInfo)seeImage("imageOne", imageOne)def dooption(tempFileDir,tempImageDir,new_dir):jsonNames = os.listdir(tempFileDir)for jsonName in jsonNames:jsonName_ = os.path.splitext(jsonName)[0]imageName = jsonName_ + ".png"imagePath = os.path.join(tempImageDir, imageName)  # 拼接图像路径jsonPath = os.path.join(tempFileDir, jsonName)  # 拼接json路径imageData = cv2.imread(imagePath)# seeImage("imageData",imageData)print(imagePath)imageOne = np.ones((imageData.shape[0], imageData.shape[1], 3), dtype=np.uint8) * 255# seeImage("imageOne",imageOne)filePath = os.path.join(tempFileDir, jsonPath)with open(filePath, "r", encoding="utf-8") as f:info = f.read()infoLoads = json.loads(info)h_image = infoLoads["imgHeight"]  # 图像的高度w_image = infoLoads["imgWidth"]  # 图像的宽度objects = infoLoads["objects"]gts = []for object in objects:temp_gt = []objectName = object["label"]if objectName == "motorcycle":temp_gt.append("motorbike")else:temp_gt.append(objectName)polygon_ = object["polygon"]# print(polygon_)pots = np.asarray(polygon_, np.int32)# cv2.polylines(imageOne,[pots],True,(0,255,0),1)x, y, w, h = cv2.boundingRect(pots)# imageOne = cv2.rectangle(imageOne, (x, y), (x + w, y + h), (0, 0, 255), 2)xmax = int(x) + int(w)ymax = int(y) + int(h)temp_gt = temp_gt + [x, y, xmax, ymax]gts.append(temp_gt)print(gts)folder = "images"img_name = imageName.split(".")[0]width = w_imageheight = h_imagexml_save_to = new_dirbboxes2xml(folder, img_name, width, height, gts, xml_save_to)if __name__ == '__main__':tempFileDir = r"E:\666\new_json"  # json文件存放目录tempImageDir = r"E:\666\new_image"  # 图像文件存放目录new_dir = r"E:\666\temp"  # 转化得到的xml文件存放目录dooption(tempFileDir, tempImageDir, new_dir)

使用labelimg软件可以看到如下所示:

CityScapes数据集转voc数据格式训练yolov5(含下载链接)相关推荐

  1. 人脸检测和人体检测(行人检测)1:人脸检测和人体检测数据集(含下载链接)

    人脸检测和人体检测(行人检测)1:人脸检测和人体检测数据集(含下载链接) 目录 人脸检测和人体检测(行人检测)1:人脸检测和人体检测数据集(含下载链接) 1. 前言 2. VOC数据集 3. COCO ...

  2. 戴眼镜检测和识别1:戴眼镜检测数据集(含下载链接)

    戴眼镜检测和识别1:戴眼镜检测数据集(含下载链接) 目录 戴眼镜检测和识别1:戴眼镜检测数据集(含下载链接) 1. 前言 2.Eyeglasses-Dataset数据集说明 3.Eyeglasses- ...

  3. 疲劳驾驶检测和识别1: 疲劳驾驶检测和识别数据集(含下载链接)

    疲劳驾驶检测和识别1: 疲劳驾驶检测和识别数据集(含下载链接) 目录 疲劳驾驶检测和识别1: 疲劳驾驶检测和识别数据集(含下载链接) 1. 前言 2. 疲劳驾驶类别说明 3. 疲劳驾驶检测数据集: ( ...

  4. 【对话生成】常见对话生成数据集整理,含下载链接(更新至2022.06.04)

    [对话生成]常见对话生成数据集整理,含下载链接(持续更新) 前言 用于对话理解的对话数据集 IEMOCAP SEMAINE Mastodon MELD EMOTyDA MEmoR M3^33ED CP ...

  5. 面部表情识别1:表情识别数据集(含下载链接)

    面部表情识别1:表情识别数据集(含下载链接) 目录 面部表情识别1:表情识别数据集(含下载链接) 1.前言 2.表情识别数据集介绍 1.JAFFE数据集 2.KDEF(Karolinska Direc ...

  6. 天庭最牛系统 推荐下载_PPT中有哪些特别好用的插件?(含下载链接)

    无数的前辈告诉我们,毕业是一件体力活.一遍遍地修改毕业论文,自认为完美,却仍旧打动不了导师和评委的心.好不容易得到了首肯,从无限循环的修改大浪中爬出来,一个答辩PPT分分钟将你打回海底. 一份合格的P ...

  7. coco数据集转voc格式(附pycocotools下载方法)

    1.coco数据集高速下载 我下载的是train2017.val2017和annotations_trainval2017,即coco2017的训练集(118287张图片).测试集(5000张图片)和 ...

  8. 中小学青少年编程创意机器人相关技术等级考试资料(含下载链接)

    青少年编程(创意艺术设计-人工智能-机器人)教育十分重要! 科技进步发展十分迅速,自动驾驶越来越成熟,物联网已经广泛应用于工业领域,人工智能在移动互联网不断发展成长. 这一切对人才提出了更高的要求! ...

  9. 分享一个百度云资源不限速下载工具(含下载链接)

    想必大家也深受百度云限速之苦了,明明几十兆宽带,居然跟我们说下载速度只有几十kb/s ???? 因此给大家分享个某大神的良心开源工具:proxyee-down,不限速下载百度云网盘的多线程下载工具! ...

最新文章

  1. 双目立体视觉 I:标定和校正
  2. Java基础-初识面向对象编程(Object-Oriented-Programming)
  3. 11相机不流畅_小米11最新售价确定,标准版价格亲民,网友:幸福来得真突然...
  4. 开发函数计算的正确姿势 —— 安装第三方依赖
  5. matconvnet 在 win7 64 位下的安装
  6. 强化学习用于电力系统决策与控制(一)——频率调整
  7. 使用PHP+MYSQL搭建的一款直播电商源码和大家分享一下
  8. nadcházející basketbalové boty velkoobchodní další
  9. 《Dreamweaver CS6 完全自学教程》笔记 第二章:Dreamweaver CS6 入门
  10. dnf服务器维护2018,2018年更新dnf游戏登陆不上 | 手游网游页游攻略大全
  11. 关于Adams仿真过程中问题的解决记录
  12. Aryaka荣获2016年度全球最具影响力SD-WAN解决方案奖
  13. linux dd创建大文件模拟磁盘不足监控
  14. java 连接池配置_【Java】java数据库连接池配置的几种方法
  15. 爱上一个人的七种表现
  16. 1241.外卖店优先级
  17. python getsize函数_Python getsizeof()和getsize()区分详解
  18. 速度收藏!史上最全Spring 面试题 92 问!【附答案】高清PDF下载
  19. 怎么办理质量管理体系认证证书ISO9001?
  20. 使用 Echarts 插件完成中国地图

热门文章

  1. UI设计行业适合女生学习吗?
  2. 国内 android wear,2018年最佳Android Wear智能手表TOP3
  3. 宇树科技Go1进阶版四足机器人开发记录
  4. 学计算机的电脑用i5还是i7好,家用电脑,选酷睿i7还是选i5,这些常识让你不纠结...
  5. 知行合一,持续输出,以不变应万变
  6. 华为Nova5性价比较荣耀20高,同室操戈?
  7. 【第二章 数据预处理】袁博《数据挖掘:理论与算法》
  8. 成都产科生产日记(十六)(建档、入院生产、上户、少儿互助金、疫苗、儿保)全...
  9. 有关我跟计算机专业的作文,我和电脑的关系作文(六篇)
  10. 【reactjs】requestIdleCallback 和 requestAnimationFrame对比