将caltech数据集转换成VOC格式

2023-10-21 11:50

本文主要是介绍将caltech数据集转换成VOC格式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目的:将Caltech行人数据集转换为Pascal VOC格式

  • 参考来源https://www.cnblogs.com/arkenstone/p/7337077.html 但是这里面的代码有一些问题,我在其中修改了一些
  • 操作步骤如下:
  1. 将下载好的caltech pedestrian dataset解压,数据集下载地址,并按如下格式存放:(最好是按照下图的格式存放,不然容易报错)                                                                                                                                                                                                        
  2.                             

运行程序如下:

import os, glob, argparse
import cv2
from scipy.io import loadmat
from collections import defaultdict
import numpy as np
from lxml import etree, objectify
"""
将caltech数据集格式转换成VOC格式,即.seq转换成.jpg; .vbb转换成xml
"""def vbb_anno2dict(vbb_file, cam_id, person_types=None):"""Parse caltech vbb annotation file to dictArgs:vbb_file: input vbb file pathcam_id: camera idperson_types: list of person type that will be used (total 4 types: person, person-fa, person?, people).If None, all will be used:Return:Annotation info dict with filename as key and anno info as value"""filename = os.path.splitext(os.path.basename(vbb_file))[0]annos = defaultdict(dict)vbb = loadmat(vbb_file)# object info in each frame: id, pos, occlusion, lock, posvobjLists = vbb['A'][0][0][1][0]objLbl = [str(v[0]) for v in vbb['A'][0][0][4][0]]# person indexif not person_types:person_types = ["person", "person-fa", "person?", "people"]person_index_list = [x for x in range(len(objLbl)) if objLbl[x] in person_types]for frame_id, obj in enumerate(objLists):if len(obj) > 0:frame_name = str(cam_id) + "_" + str(filename) + "_" + str(frame_id+1) + ".jpg"annos[frame_name] = defaultdict(list)annos[frame_name]["id"] = frame_namefor fid, pos, occl in zip(obj['id'][0], obj['pos'][0], obj['occl'][0]):fid = int(fid[0][0]) - 1  # for matlab start from 1 not 0if not fid in person_index_list:  # only use bbox whose label is given person typecontinueannos[frame_name]["label"] = objLbl[fid]pos = pos[0].tolist()occl = int(occl[0][0])annos[frame_name]["occlusion"].append(occl)annos[frame_name]["bbox"].append(pos)if not annos[frame_name]["bbox"]:del annos[frame_name]return annosdef seq2img(annos, seq_file, outdir, cam_id):"""Extract frames in seq files to given output directoriesArgs:annos: annos dict returned from parsed vbb fileseq_file: seq file pathoutdir: frame save dircam_id: camera idReturns:camera captured image size"""cap = cv2.VideoCapture(seq_file)index = 1# captured frame listv_id = os.path.splitext(os.path.basename(seq_file))[0]cap_frames_index = np.sort([int(os.path.splitext(id)[0].split("_")[2]) for id in annos.keys()])while True:ret, frame = cap.read()if ret:if not index in cap_frames_index:index += 1continueif not os.path.exists(outdir):os.makedirs(outdir)outname = os.path.join(outdir, str(cam_id)+"_"+v_id+"_"+str(index)+".jpg")print("Current frame: ", v_id, str(index))cv2.imwrite(outname, frame)height, width, _ = frame.shapeelse:breakindex += 1img_size = (width, height)return img_sizedef instance2xml_base(anno, img_size, bbox_type='xyxy'):"""Parse annotation data to VOC XML formatArgs:anno: annotation info returned by vbb_anno2dict functionimg_size: camera captured image sizebbox_type: bbox coordinate record format: xyxy (xmin, ymin, xmax, ymax); xywh (xmin, ymin, width, height)Returns:Annotation xml info tree"""assert bbox_type in ['xyxy', 'xywh']E = objectify.ElementMaker(annotate=False)anno_tree = E.annotation(E.folder('VOC2014_instance/person'),E.filename(anno['id']),E.source(E.database('Caltech pedestrian'),E.annotation('Caltech pedestrian'),E.image('Caltech pedestrian'),E.url('None')),E.size(E.width(img_size[0]),E.height(img_size[1]),E.depth(3)),E.segmented(0),)for index, bbox in enumerate(anno['bbox']):bbox = [float(x) for x in bbox]if bbox_type == 'xyxy':xmin, ymin, w, h = bboxxmax = xmin+wymax = ymin+helse:xmin, ymin, xmax, ymax = bboxxmin = int(xmin)ymin = int(ymin)xmax = int(xmax)ymax = int(ymax)if xmin < 0:xmin = 0if xmax > img_size[0] - 1:xmax = img_size[0] - 1if ymin < 0:ymin = 0if ymax > img_size[1] - 1:ymax = img_size[1] - 1if ymax <= ymin or xmax <= xmin:continueE = objectify.ElementMaker(annotate=False)anno_tree.append(E.object(E.name(anno['label']),E.bndbox(E.xmin(xmin),E.ymin(ymin),E.xmax(xmax),E.ymax(ymax)),E.difficult(0),E.occlusion(anno["occlusion"][index])))return anno_treedef parse_anno_file(vbb_inputdir, seq_inputdir, vbb_outputdir, seq_outputdir, person_types=None):"""Parse Caltech data stored in seq and vbb files to VOC xml formatArgs:vbb_inputdir: vbb file saved pthseq_inputdir: seq file saved pathvbb_outputdir: vbb data converted xml file saved pathseq_outputdir: seq data converted frame image file saved pathperson_types: list of person type that will be used (total 4 types: person, person-fa, person?, people).If None, all will be used:"""# annotation sub-directories in hda annotation input directoryassert os.path.exists(vbb_inputdir)sub_dirs = os.listdir(vbb_inputdir)for sub_dir in sub_dirs:print("Parsing annotations of camera: ", sub_dir)cam_id = sub_dirvbb_files = glob.glob(os.path.join(vbb_inputdir, sub_dir, "*.vbb"))for vbb_file in vbb_files:annos = vbb_anno2dict(vbb_file, cam_id, person_types=person_types)if annos:vbb_outdir = os.path.join(vbb_outputdir, "annotations", sub_dir, "bbox")# extract frames from seqseq_file = os.path.join(seq_inputdir, sub_dir, os.path.splitext(os.path.basename(vbb_file))[0]+".seq")seq_outdir = os.path.join(seq_outputdir, sub_dir, "frame")if not os.path.exists(vbb_outdir):os.makedirs(vbb_outdir)if not os.path.exists(seq_outdir):os.makedirs(seq_outdir)img_size = seq2img(annos, seq_file, seq_outdir, cam_id)for filename, anno in sorted(annos.items(), key=lambda x: x[0]):if "bbox" in anno:anno_tree = instance2xml_base(anno, img_size)outfile = os.path.join(vbb_outdir, os.path.splitext(filename)[0]+".xml")print("Generating annotation xml file of picture: ", filename)etree.ElementTree(anno_tree).write(outfile, pretty_print=True)def visualize_bbox(xml_file, img_file):import cv2tree = etree.parse(xml_file)# load imageimage = cv2.imread(img_file)# get bboxfor bbox in tree.xpath('//bndbox'):coord = []for corner in bbox.getchildren():coord.append(int(float(corner.text)))# draw rectangle# coord = [int(x) for x in coord]image = cv2.rectangle(image, (coord[0], coord[1]), (coord[2], coord[3]), (0, 0, 255), 2)# visualize imagecv2.imshow("test", image)cv2.waitKey(0)#注意!以下输入输出目录地址请参照上文示意图,确保seq文件被正确读入,不然可能会产生空文件夹或报错def main():# parser = argparse.ArgumentParser()# parser.add_argument("seq_dir", default="C:\\workspace\\data\\images", help="Caltech dataset seq data root directory")# parser.add_argument("vbb_dir", default="C:\\workspace\\data\\annotations", help="Caltech dataset vbb data root directory")# parser.add_argument("output_dir",default="C:\\workspace\\data\\VOCdevkit2007\\Caltech", help="Root saving path for frame and annotation files")# # parser.add_argument("person_type", default="person", type=str, help="Person type extracted within 4 options: "# #                                                   "'person', 'person-fa', 'person?', 'people'. If multiple type used,"# #                                                   "separated with comma",# #                     choices=["person", "person-fa", "person?", "people"])# parser.add_argument("person_type", default="person", type=str)# args = parser.parse_args()outdir = "/VOCdevkit2007"#outdir = args.output_dirframe_out = os.path.join(outdir, "frame")anno_out = os.path.join(outdir, "annotation")# person_type = args.person_type.split(",")person_type = "person"seq_dir = "/Caltech"vbb_dir = "/Caltech/annotations"parse_anno_file(vbb_dir, seq_dir, anno_out, frame_out, person_type)print("Generating done!")
#
# def test():
#     seq_inputdir = "/startdt_data/caltech_pedestrian_dataset"
#     vbb_inputdir = "/startdt_data/caltech_pedestrian_dataset/annotations"
#     seq_outputdir = "/startdt_data/caltech_pedestrian_dataset/test"
#     vbb_outputdir = "/startdt_data/caltech_pedestrian_dataset/test"
#     person_types = ["person"]
#     parse_anno_file(vbb_inputdir, seq_inputdir, vbb_outputdir, seq_outputdir, person_types=person_types)
#
#     # xml_file = "/startdt_data/caltech_pedestrian_dataset/annotations/set00/bbox/set00_V013_1511.xml"
#     # img_file = "/startdt_data/caltech_pedestrian_dataset/set00/frame/set00_V013_1511.jpg"
#     # visualize_bbox(xml_file, img_file)if __name__ == "__main__":main()

 

  • 出现的一个错误:UnboundLocalError: local variable 'width' referenced before assignment

  • 原因:.seq文件没有读到导致的,要检查一下输入目录https://github.com/CasiaFan/Dataset_to_VOC_converter/issues/1

 

 有一个问题值得注意:caltech中与行人有关的类有四种:person_types = ["person", "person-fa", "person?", "people"]

如果不特别设置,直接用一些现成的代码去转换.seq文件为.jpg是把所有frame都截取出来,而一些代码中转换vbb文件为xml文件时却只把person类的标注出来,就会导致images和annotation最后的总数不一致,如:https://blog.csdn.net/qq_33297776/article/details/79869813

我参考的代码设置了.seq和.vbb均只截取person类,所以数量一致

 

 

这篇关于将caltech数据集转换成VOC格式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/254197

相关文章

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内