CUHK 行人資料集 及 轉為yolo訓練標籤格式

NO IMAGE

部分轉載:https://zhuanlan.zhihu.com/p/31836357

11.CUHK Occlusion Dataset

該資料集出自於香港中文大學,可應用於行為分析和行人檢測。包含了1063張行人圖片。

行人檢測資料集打包下載連結:https://pan.baidu.com/s/1o8aanoQ

密碼:xkka

轉換資料格式程式碼:

# coding=UTF-8
import os
from PIL import Image
from os import listdir, getcwd
image_path = "/data/yolo3/scripts/VOCdevkit/VOC2007/JPEGImages"                          # 圖片存放路徑,路徑固定
# 我們的標籤格式的路徑
filelist = open("./annotations_CUHK/CUHK.txt", "r")   # 1,我們的標籤存放路徑
sets=[('train')]
lines = filelist.readlines()
# 將CUHK的x,y,w,h格式轉換成yolo的X,Y,W,H
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = box[0]   box[2]/2.0
y = box[1]   box[3]/2.0
w = box[2]
h = box[3]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
# 獲取要查詢的圖片的w,h
def get_size(image_id):
im = Image.open('./VOCdevkit/VOC2007/JPEGImages/%s'%(image_id))   # 2,源圖片存放路徑
size = im.size
w = size[0]
h = size[1]
return (w,h)
# 在labels目錄下建立每個圖片的標籤txt文件
def text_create(name,bnd):
full_path = "./VOCdevkit/VOC2007/labels/%s.txt"%(name)
size = get_size(name   '.jpg')
convert_size = convert(size, bnd)
file = open(full_path, 'a')
file.write('0 '   str(convert_size[0])   ' '   str(convert_size[1])   ' '   str(convert_size[2])   ' '   str(convert_size[3]) )
file.write('\n')
# 獲取資料夾下所有圖片的圖片名
def get_name(file_dir):
list_file=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
# splitext()將路徑拆分為檔名 副檔名,例如os.path.splitext(“E:/lena.jpg”)將得到”E:/lena“ ".jpg"
if os.path.splitext(file)[1] == '.jpg':
list_file.append(os.path.join(root, file))
return list_file
# 當前工作環境下的絕對路徑
wd = getcwd()
for image_set in sets:
if not os.path.exists('/data/yolo3/scripts/VOCdevkit/VOC2007/labels'):
os.makedirs('/data/yolo3/scripts/VOCdevkit/VOC2007/labels')                     # 生成的yolo3標籤存放路徑,路徑固定
image_names = get_name(image_path)
list_file = open('2007_%s.txt'%(image_set), 'w')
for image_name in image_names:
list_file.write('%s\n'%(image_name))
list_file.close()
for line in lines:
str_names = line.split()
str_name = str_names[0]
b = line.split()[1:]
bnd = (float(b[0]) ,float(b[1]) ,float(b[2]) ,float(b[3]))
text_create(str_name, bnd)