文章目录
Caltech Pedestrian Detection 数据集预处理
一、Caltech 数据集简介
官网:http://www.vision.caltech.edu/Image_Datasets/CaltechPedestrians/
-
Description(Overview):
The Caltech Pedestrian Dataset consists of approximately 10 hours of 640x480 30Hz video taken from a vehicle driving through regular traffic in an urban environment. About 250,000 frames (in 137 approximately minute long segments) with a total of 350,000 bounding boxes and 2300 unique pedestrians were annotated. The annotation includes temporal correspondence between bounding boxes and detailed occlusion labels.加州理工学院的行人数据集包含大约10个小时的 640x480 30Hz视频,该视频取自在城市环境中通过正常交通行驶的车辆。
注释了大约250,000帧(在137个大约一分钟长的段中),总共有350,000个边界框和2300个唯一的行人。 注释包括边界框和详细的遮挡标签之间的时间对应。(30Hz:是每秒采集到30张图片)
-
Caltech Pedestrian Dataset:
训练数据(set00-set05)包含六个训练集(每个约1GB),每个训练集包含6-13个一分钟长的seq文件以及所有注释信息(有关详细信息,请参见本文)。
测试数据(set06-set10)包含五组,每组约1GB。新增:现在还提供了整个数据集的注释。也提供包含所有评估算法的检测结果的输出文件。
-
Seq video format:
seq文件是一系列具有固定大小标头的串联图像帧。可以在Piotr的Matlab工具箱(需要版本3.20或更高版本)中找到用于读取/写入/操作seq文件的Matlab例程。 这些例程还可用于将seq文件提取到图像目录。
(本文在预处理部分将介绍:采用 python 的方式来解析 seq 文件)
-
annotations(标注):
标注使用自定义的 “视频边界框”(vbb)文件格式。
该代码还包含实用程序,可用于查看带有叠加注释的seq文件,用于生成论文中所有ROC图的评估例程,以及用于创建数据集的vbb标签工具(另请参见此过时的视频教程)。
二、数据集预处理
值得说明的是,预处理的代码全部基于python3的语法。
1. Seq 文件转化成 JEPG 图像文件
详细过程可以查看我的另一篇博客:
https://blog.csdn.net/pentiumCM/article/details/108622304
处理的完整代码:
#!/usr/bin/env python
# encoding: utf-8
'''
@Author : pentiumCM
@Email : 842679178@qq.com
@Software: PyCharm
@File : seq_process.py
@Time : 2020/9/7 21:44
@desc : Caltech数据集 seq 类型文件数据集处理
'''
# Deal with .seq format for video sequence
# The .seq file is combined with images,
# so I split the file into several images with the image prefix
# "\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46".
import os.path
import fnmatch
import shutil
def open_save(file, savepath):
"""
read .seq file, and save the images into the savepath
:param file: .seq文件路径
:param savepath: 保存的图像路径
:return:
"""
# 读入一个seq文件,然后拆分成image存入savepath当中
f = open(file, 'rb+')
# 将seq文件的内容转化成str类型
string = f.read().decode('latin-1')
# splitstring是图片的前缀,可以理解成seq是以splitstring为分隔的多个jpg合成的文件
splitstring = "\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46"
# split函数做一个测试,因此返回结果的第一个是在seq文件中是空,因此后面省略掉第一个
"""
>>> a = ".12121.3223.4343"
>>> a.split('.')
['', '12121', '3223', '4343']
"""
# split .seq file into segment with the image prefix
strlist = string.split(splitstring)
f.close()
count = 0
# delete the image folder path if it exists
if os.path.exists(savepath):
shutil.rmtree(savepath)
# create the image folder path
if not os.path.exists(savepath):
os.makedirs(savepath)
# deal with file segment, every segment is an image except the first one
for img in strlist:
filename = str(count) + '.jpg'
filenamewithpath = os.path.join(savepath, filename)
# abandon the first one, which is filled with .seq header
if count > 0:
i = open(filenamewithpath, 'wb+')
i.write(splitstring.encode('latin-1'))
i.write(img.encode('latin-1'))
i.close()
count += 1
if __name__ == "__main__":
rootdir = "F:/experiment/Caltech/data"
saveroot = "F:/experiment/Caltech/VOC_process/JPEGImages"
# walk in the rootdir, take down the .seq filename and filepath
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
# check .seq file with suffix
# fnmatch 全称是 filename match,主要是用来匹配文件名是否符合规则的
if fnmatch.fnmatch(filename, '*.seq'):
# take down the filename with path of .seq file
thefilename = os.path.join(parent, filename)
# create the image folder by combining .seq file path with .seq filename
parent_path = parent
parent_path = parent_path.replace('\\', '/')
thesavepath = saveroot + '/' + parent_path.split('/')[-1] + '/' + filename.split('.')[0]
print("Filename=" + thefilename)
print("Savepath=" + thesavepath)
open_save(thefilename, thesavepath)
2. VBB 标注文件转化为 XML 文件
处理的完整代码:
#!/usr/bin/env python
# encoding: utf-8
'''
@Author : pentiumCM
@Email : 842679178@qq.com
@Software: PyCharm
@File : seq2voc.py
@Time : 2020/9/7 21:44
@desc : Caltech数据集 VBB 标注文件转化为XML文件
'''
import os, glob
import cv2
from scipy.io import loadmat
from collections import defaultdict
import numpy as np
from lxml import etree, objectify
def vbb_anno2dict(vbb_file, cam_id):
# 通过os.path.basename获得路径的最后部分“文件名.扩展名”
# 通过os.path.splitext获得文件名
filename = os.path.splitext(os.path.basename(vbb_file))[0]
# 定义字典对象annos
annos = defaultdict(dict)
vbb = loadmat(vbb_file)
# object info in each frame: id, pos, occlusion, lock, posv
objLists = vbb['A'][0][0][1][0]
objLbl = [str(v[0]) for v in vbb['A'][0][0][4][0]] # 可查看所有类别
# person index
person_index_list = np.where(np.array(objLbl) == "person")[0] # 只选取类别为‘person’的xml
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_name
annos[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 0
if not id in person_index_list: # only use bbox whose label is person
continue
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]
print(annos)
return annos
def seq2img(annos, seq_file, outdir, cam_id):
cap = cv2.VideoCapture(seq_file)
index = 1
# captured frame list
v_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 += 1
continue
if 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.shape
else:
break
index += 1
img_size = (width, height)
return img_size
def 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 = bbox
xmax = xmin + w
ymax = ymin + h
else:
xmin, ymin, xmax, ymax = bbox
E = 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_tree
def parse_anno_file(vbb_inputdir, vbb_outputdir):
"""
:param vbb_inputdir:
:param vbb_outputdir:
:return:
"""
# annotation sub-directories in hda annotation input directory
assert 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
# 获取某一个子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 vbb2voc():
"""
vbb标注文件 转 VOC标注 的 xml 入口函数
:return:
"""
vbb_inputdir = "F:/experiment/Caltech/annotations_org/"
vbb_outputdir = "F:/experiment/Caltech/VOC_process/Annotations_org/"
parse_anno_file(vbb_inputdir, vbb_outputdir)
if __name__ == "__main__":
vbb2voc()