Caltech数据集转为voc格式
1.下载caltech数据集
链接:https://pan.baidu.com/s/1P-qxLyfL8cewtQKUj14_rA
提取码:miao
–来自百度网盘超级会员V1的分享
2.下载后 文件夹如下,表示下载完成了,否则文件不全,有过此处踩坑。
3. 对 set00 set01 set02 set03 set04 set05 set06 set07 set08 set09 set10 进行解压。解压后如下:
4. 我的python是3.7 运行下面代码 ,将.seq文件转为.jpg
import os.path
import fnmatch
import shutildef open_save(file, savepath):# read .seq file and save the images into the savepathf = open(file, 'rb+')string = f.read().decode('latin-1')splitstring = "\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46"# split .seq file into segment with the image prefixstrlist = string.split(splitstring)f.close()count = 0# delete the image folder path if it existsif os.path.exists(savepath):shutil.rmtree(savepath)# create the image folder pathif not os.path.exists(savepath):os.mkdir(savepath)# deal with file segment, every segment is an image except the first onefor img in strlist:filename = str(count) + '.jpg'filenamewithpath = os.path.join(savepath, filename)# abandon the first one, which is filled with .seq headerif count > 0:i = open(filenamewithpath, 'wb+')i.write(splitstring.encode('latin-1'))i.write(img.encode('latin-1'))i.close()count += 1if __name__ == "__main__":rootdir = r"C:\Users\Administrator\Desktop\caltech\set09" #此处分别转化set00 set01 ······ set08 set09 set10 # walk in the rootdir, take down the .seq filename and filepathfor parent, dirnames, filenames in os.walk(rootdir):for filename in filenames:# check .seq file with suffixif fnmatch.fnmatch(filename, '*.seq'):# take down the filename with path of .seq filethefilename = os.path.join(parent, filename)# create the image folder by combining .seq file path with .seq filenamethesavepath = parent + '/' + filename.split('.')[0]print("Filename=" + thefilename)print("Savepath=" + thesavepath)open_save(thefilename, thesavepath)
分别转化set00 set01 set02 set03 set04 set05 set06 set07 set08 set09 set10 文件中的.seq文件。
以 set00为例,得如下格式:其他的一样的转化 方式。
V000中是这样的:V000 中1845张图
5. 将所有的jpg图片摘出来放到一起,需要对图片进行批量重命名,参考我的另一篇博客图片批处理重命名。如下图所示:
6,将annotation文件里面对的vbb文件转化为xml格式文件:
代码如下:
# -*- coding:utf-8 -*-
import os, glob
import cv2
from scipy.io import loadmat
from collections import defaultdict
import numpy as np
from lxml import etree, objectifydef vbb_anno2dict(vbb_file, cam_id):# 通过os.path.basename获得路径的最后部分“文件名.扩展名”# 通过os.path.splitext获得文件名filename = os.path.splitext(os.path.basename(vbb_file))[0]# 定义字典对象annosannos = 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 indexperson_index_list = np.where(np.array(objLbl) == "person")[0] # 只选取类别为‘person’的xmlfor 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_nameannos[frame_name]["label"] = "person"for id, pos, occl in zip(obj['id'][0], obj['pos'][0], obj['occl'][0]):id = int(id[0][0]) - 1 # for matlab start from 1 not 0if not id in person_index_list: # only use bbox whose label is personcontinuepos = 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]print(annos)return annosdef seq2img(annos, seq_file, outdir, cam_id):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()print(ret)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, bbox_type='xyxy'):"""bbox_type: xyxy (xmin, ymin, xmax, ymax); xywh (xmin, ymin, width, height)"""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(640),E.height(480),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 = bboxE = 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, vbb_outputdir):# annotation sub-directories in hda annotation input directoryassert os.path.exists(vbb_inputdir)sub_dirs = os.listdir(vbb_inputdir) # 对应set00,set01...for sub_dir in sub_dirs:print("Parsing annotations of camera: ", sub_dir)cam_id = sub_dir # set00 set01等# 获取某一个子set下面的所有vbb文件vbb_files = glob.glob(os.path.join(vbb_inputdir, sub_dir, "*.vbb"))for vbb_file in vbb_files:# 返回一个vbb文件中所有的帧的标注结果annos = vbb_anno2dict(vbb_file, cam_id)if annos:# 组成xml文件的存储文件夹,形如“/Users/chenguanghao/Desktop/Caltech/xmlresult/”vbb_outdir = vbb_outputdir# 如果不存在if not os.path.exists(vbb_outdir):os.makedirs(vbb_outdir)for filename, anno in sorted(annos.items(), key=lambda x: x[0]):if "bbox" in anno:anno_tree = instance2xml_base(anno)outfile = os.path.join(vbb_outdir, os.path.splitext(filename)[0] + ".xml")print("Generating annotation xml file of picture: ", filename)# 生成最终的xml文件,对应一张图片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)origin = cv2.imread(img_file)# 获取一张图片的所有bboxfor bbox in tree.xpath('//bndbox'):coord = []for corner in bbox.getchildren():coord.append(int(float(corner.text)))print(coord)cv2.rectangle(image, (coord[0], coord[1]), (coord[2], coord[3]), (0, 0, 255), 2)# visualize imagecv2.imshow("test", image)cv2.imshow('origin', origin)cv2.waitKey(0)def main():vbb_inputdir = r"C:\Users\Administrator\Desktop\cal\annotations\annotations"vbb_outputdir = r"C:\Users\Administrator\Desktop\cal\Caltech_VOC\xmlresult"parse_anno_file(vbb_inputdir, vbb_outputdir)if __name__ == "__main__":main()
然后就是整合文件;
Caltech数据集转为voc格式相关推荐
- 将coco数据集转为voc格式代码
点击链接下载 https://download.csdn.net/download/weixin_55775980/24892341
- 【目标检测实验系列】使用yolov3 spp训练西工大遥感数据集NWPU VHR-10(包括如何将NWPU VHR-10转为VOC格式和yolov3 spp实验调试的详细步骤,且附上训练完的权重文件)
目录 1. 文章主要内容 2. 西工大数据集转换为VOC格式数据集 2.1 VOC数据集结构 2.2 西工大数据集 2.3 转换格式 2.3.1 构建与VOC类似的数据集文件结构(文件夹名可以自定义) ...
- 使用PaddleDetection自带脚本将自制labelme数据集转为coco格式
问题 PaddleDetection(ppdet)自带一个能将labelme标注的数据集转为coco格式的脚本,还能设置比例分配train.val和test数据集的比例.当前使用的ppdet版本为ve ...
- KITTI车辆检测数据集转VOC格式(亲测成功,附KITTI云盘连接以及完整格式转换代码)- KITTI车辆检测数据集看着一篇就够了!
KIITI数据集转VOC格式 软件环境 一.准备数据集 1.1下载链接 1.2数据集使用方法 二.txt标注文件准换为VOC格式 2.1目前准换代码存在的问题 2.2博主提供的方法 2.3转换代码 2 ...
- mmdetection实战,训练扑克牌数据集(VOC格式)并测试计算mAP
mmdetection实战,训练扑克牌数据集(VOC格式)并测试计算mAP 一.数据集准备 二.mmdetection的安装 三.修改相关文件 1. 修改class_names.py文件 2. 修改v ...
- 【项目三、车牌检测+识别项目】一、CCPD车牌数据集转为YOLOv5格式和LPRNet格式
目录 前言 一.CCPD数据集介绍 二.CCPD数据集下载 三.划分训练集.验证集和测试集 四.车牌检测数据集制作 五.车牌识别数据集制作 六.我的车牌检测+识别数据集 Reference 前言 马上 ...
- coco数据集转voc格式(附pycocotools下载方法)
1.coco数据集高速下载 我下载的是train2017.val2017和annotations_trainval2017,即coco2017的训练集(118287张图片).测试集(5000张图片)和 ...
- maskrcnn-benchmask训练自己的数据集(voc格式)
去年十二月facebook发布了pytorch1.0,并且基于pytorch1.0的MASK RCNN和FPN实现也开源了.其实现相较于Detectron拥有更快的训练速度和更低的GPU内存占用.具体 ...
- 100种目标检测数据集【voc格式yolo格式json格式coco格式】+YOLO系列算法源码及训练好的模型
提示:本文介绍并分享了应用于各行业.各领域非常有用的目标检测数据集(感谢您的关注+三连,数据集持续更新中-),其中绝大部分数据集作者已应用于各种实际落地项目,数据集整体质量好,标注精确,数据的多样性充 ...
最新文章
- Cisco路由器故障诊断技术专题
- Leetcode 172. 阶乘后的零 解题思路及C++实现
- matplotlib包的学习(二)
- 【jetson nano】两台ubuntu ssh远程连接控制
- (软件工程复习核心重点)第五章详细设计-第五节:程序复杂度的定量度量
- 手机通话记录重复显示怎么处理_手机通话记录怎么找回
- TMGM外汇平台官网最全测评(2022年版)
- 今天在当当上看到一本书,ASP.NET程序员参考手册
- 视频教程-学透JavaScript-JavaScript
- SQL 注入防御方法总结
- 深度置信(信念)网络DBN(Deep Belief Network)
- 微信小程序获取用户信息后的解密操作
- AOSP 源代码标记和 build
- 201226元件_看电阻型号
- 大数据要掌握哪些语言?怎样才能学好大数据?
- 灰色关联分析法无量纲处理方法
- 肠道菌群检测粪便样本的采集保存方法
- 1、HotSpot And OpenJDK And OracleJDK And JER区别
- 一个程序员的睡眠计划
- 数组,es5,字符串,Math和Date