参考博客链接:
- https://blog.csdn.net/a2008301610258/article/details/45873867
- https://github.com/hizhangp/caltech-pedestrian-converter
- http://www.kanadas.com/program-e/2015/06/converting_caltech_pedestrian.html
需要实现的功能:
- 将seq格式的视频文件提取图片,保存为.jpg格式的图片。
- 将vbb格式的bounding box标注文件转换为txt文件,供darknet,caffe-ssd等模型进行训练。
目的:训练yolov3和yolo-v3-tiny,需要大规模的行人检测数据集,因此就选到了Caltech Pedestrian Detection这个数据集。
从seq格式文件提取图片并保存
#-*-coding:utf-8-*-
import os
import numpy as np
import cv2
import os.path
import fnmatch
import shutil
def open_save(file,savepath):
# read .seq file, and save the images into the savepath
f = open(file,'rb')
string = str(f.read())
splitstring = "\xFF\xD8\xFF\xE0\x00\x10\x4A\x46\x49\x46"
# 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.mkdir(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)
i.write(img)
i.close()
count += 1
if __name__=="__main__":
for i in range(11):
rootdir = "./set{:02}/".format(i)
print rootdir
# 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
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
thesavepath = os.path.join(parent, filename.split('.')[0])
print("Filename=" + thefilename)
print("Savepath=" + thesavepath)
open_save(thefilename,thesavepath)
效果:提取出set00/V000/1.jpg这样命令的图片,这里的图片id和下面生成的bbx的id是一一对应的
从vbb格式文件提取出bounding box
clear
clc
root_dir = 'E:/唐圣钦/person/annotations/';
path1 = dir(root_dir);
for i=3:length(path1)
dir1 = path1(i).name;
path2 = dir([ root_dir , dir1])
for j=3:length(path2)
ful_path = [root_dir, dir1, '/', path2(j).name]
temp = strsplit(ful_path, '.');
save_path = strcat(temp(1), '.txt');
save_path = save_path{1}
A = vbb('vbbLoad', ful_path );
c=fopen(save_path, 'w');
for i = 1:A.nFrame
iframe = A.objLists(1,i);
iframe_data = iframe{1,1};
n1length = length(iframe_data);
for j = 1:n1length
iframe_dataj = iframe_data(j);
if iframe_dataj.pos(1) ~= 0 %pos posv
fprintf(c,'%d %f %f %f %f\n', i, iframe_dataj.pos(1),iframe_dataj.pos(2),iframe_dataj.pos(3),iframe_dataj.pos(4));
end
end
end
fclose(c);
end
end
效果:生成annotations/set00/V000.txt这样的txt文件, 格式:id, x, y, w, h (x,y为左上角坐标)