做一件代发网站,巨久科技网站建设,湖南企业建网站公司,网站策划建设阶段的推广FLIR在2022.1.19发布了新版的FLIR_ADAS_v2#xff0c;有着更多的类别和数量更丰富的图像。数据集同步注释热图像和无注释RGB图像供参考。本文章主要介绍如何使用FLIR_ADAS_v2中的rgb图像和thermal图像来制作yolo格式数据集。 
1.官方数据集下载#xff1a;FLIR_ADAS_v2数据集…FLIR在2022.1.19发布了新版的FLIR_ADAS_v2有着更多的类别和数量更丰富的图像。数据集同步注释热图像和无注释RGB图像供参考。本文章主要介绍如何使用FLIR_ADAS_v2中的rgb图像和thermal图像来制作yolo格式数据集。 
1.官方数据集下载FLIR_ADAS_v2数据集下载 
官方下载链接 
2. 将单个json文件转换为多个xml文件 
需要注意的是代码里面的文件位置是当时把官方文件下载的 COCO 数据集所在目录读取的json 文件所在位置是看需要用的是rgb图像或者thermal图像的train或者val。 
另外生成的xml文件存放位置一定要和json文件相对应也就是说新建的文件夹里一定包含data文件夹只这样才能生成xml文件不然会报错filenotfounderror: [errno 2] no such file or directory: ...。即保存文件里面的data文件夹一定要新建好再运行代码。 python代码如下 
# translate coco_json to xml
import os
import time
import json
import pandas as pd
from tqdm import tqdm
from pycocotools.coco import COCOdef trans_id(category_id):names  []namesid  []for i in range(0, len(cats)):names.append(cats[i][name])namesid.append(cats[i][id])# print(id:{1}\t {0}.format(names[i], namesid[i]))index  namesid.index(category_id)return indexroot  rG:/红外数据集-FLIR2/FLIR_ADAS_v2/images_thermal_train  # 你下载的 COCO 数据集所在目录
dataType  2017
anno  rG:/红外数据集-FLIR2/FLIR_ADAS_v2/images_thermal_train/coco.json  # annotation json 文件所在位置
xml_dir  rG:/红外数据集-FLIR2/FLIR2_yolo_xml/images_thermal_train  # 导出的xml文件所在的位置coco  COCO(anno)  # 读文件
cats  coco.loadCats(coco.getCatIds())  # 这里loadCats就是coco提供的接口获取类别# Create anno dir
dttm  time.strftime(%Y%m%d%H%M%S, time.localtime())
# if os.path.exists(xml_dir):
#     os.rename(xml_dir, xml_dir  dttm)
# os.mkdir(xml_dir)with open(anno, r) as load_f:f  json.load(load_f)imgs  f[images]  # json文件的img_id和图片对应关系 imgs列表表示多少张图cat  f[categories]
df_cate  pd.DataFrame(f[categories])  # json中的类别
df_cate_sort  df_cate.sort_values([id], ascendingTrue)  # 按照类别id排序
categories  list(df_cate_sort[name])  # 获取所有类别名称
print(categories  , categories)
df_anno  pd.DataFrame(f[annotations])  # json中的annotationfor i in tqdm(range(len(imgs))):  # 大循环是images所有图片xml_content  []file_name  imgs[i][file_name]  # 通过img_id找到图片的信息height  imgs[i][height]img_id  imgs[i][id]width  imgs[i][width]# xml文件添加属性xml_content.append(annotation)xml_content.append(	folderVOC2007/folder)xml_content.append(	filename  file_name.split(/)[1].split(.)[0]  .jpg  /filename)xml_content.append(	size)xml_content.append(		width  str(width)  /width)xml_content.append(		height  str(height)  /height)xml_content.append(	/size)xml_content.append(	segmented0/segmented)# 通过img_id找到annotationsannos  df_anno[df_anno[image_id].isin([img_id])]  # (2,8)表示一张图有两个框for index, row in annos.iterrows():  # 一张图的所有annotation信息bbox  row[bbox]category_id  row[category_id]# cate_name  categories[trans_id(category_id)]cate_name  cat[category_id - 1][name]# add new objectxml_content.append(object)xml_content.append(name  cate_name  /name)xml_content.append(poseUnspecified/pose)xml_content.append(truncated0/truncated)xml_content.append(difficult0/difficult)xml_content.append(bndbox)xml_content.append(xmin  str(int(bbox[0]))  /xmin)xml_content.append(ymin  str(int(bbox[1]))  /ymin)xml_content.append(xmax  str(int(bbox[0]  bbox[2]))  /xmax)xml_content.append(ymax  str(int(bbox[1]  bbox[3]))  /ymax)xml_content.append(/bndbox)xml_content.append(/object)xml_content.append(/annotation)x  xml_contentxml_content  [x[i] for i in range(0, len(x)) if x[i] ! \n]### list存入文件xml_path  os.path.join(xml_dir, file_name.replace(.jpg, .xml))with open(xml_path, w, encodingutf8) as f:f.write(\n.join(xml_content))xml_content[:]  [] 
成功运行 3. 将多个xml文件转换为YOLO所需的txt文件 
里面的类别看Readme文件其中thermal类别比rgb类别多了dog和deer生成txt注意区分 
不过为了后续类别对应我统一都写成thermal的类别了 
# xml_to_yolo_txt.py
# 此代码和VOC_KITTI文件夹同目录
import os
import xml.etree.ElementTree as ET
# 这里的类名为我们xml里面的类名顺序a按照Readme文件,或者也可以不考虑顺序
# 其中thermal类别比rgb类别多了dog和deer生成txt注意区分
class_names  [person,bike,car,motor, bus, train,truck,light,hydrant, sign,dog,deer,skateboard,stroller, scooter, other vehicle]
# class_names  [person,bike,car,motor, bus, train,truck,light,hydrant, sign,
#                skateboard,stroller,scooter,other vehicle ]
# xml文件路径
path  G:/红外数据集-FLIR2/FLIR2_yolo_xml/images_rgb_train/data/
# 转换一个xml文件为txt
def single_xml_to_txt(xml_file):tree  ET.parse(os.path.join(path, xml_file))root  tree.getroot()# 保存的txt文件路径txt_file  os.path.join(G:/红外数据集-FLIR2/FLIR2_yolo/images_rgb_train/data/, xml_file.split(.)[0].txt)with open(txt_file, w) as txt_file:for member in root.findall(object):#filename  root.find(filename).textpicture_width  int(root.find(size)[0].text)picture_height  int(root.find(size)[1].text)class_name  member[0].text# 类名对应的indexclass_num  class_names.index(class_name)box_x_min  int(member[4][0].text) # 左上角横坐标box_y_min  int(member[4][1].text) # 左上角纵坐标box_x_max  int(member[4][2].text) # 右下角横坐标box_y_max  int(member[4][3].text) # 右下角纵坐标# 转成相对位置和宽高x_center  float(box_x_min  box_x_max) / (2 * picture_width)y_center  float(box_y_min  box_y_max) / (2 * picture_height)width  float(box_x_max - box_x_min) /  picture_widthheight  float(box_y_max - box_y_min) /  picture_height# print(class_num, x_center, y_center, width, height)txt_file.write(str(class_num)     str(x_center)     str(y_center)     str(width)     str(height)  \n)
# 转换文件夹下的所有xml文件为txt
def dir_xml_to_txt(path):files  os.listdir(path)for xml_file in files:single_xml_to_txt(xml_file)
dir_xml_to_txt(path)4整理成yolo的txt格式 
一般yolo的格式如下 因此将上面生成的文件分别整理成图片的格式然后仿照./data/文件夹下的yaml文件自己写一个数据yaml此时的class类别必须和上面生成txt的类别顺序一致。 
train: G:/yolo_FLIR2/FLIR2_yolo_thermal/images/train
val: G:/yolo_FLIR2/FLIR2_yolo_thermal/images/val
test: G:/yolo_FLIR2/FLIR2_yolo_thermal/images/test# Classes
names:0: person1: bike 2: car3: motor4: bus5: train6: truck7: light8: hydrant9: sign10: dog11: deer12: skateboard13: stroller 14: scooter 15: other vehicle