当前位置:网站首页>INRIAPerson数据集转化为yolo训练格式并可视化
INRIAPerson数据集转化为yolo训练格式并可视化
2022-07-17 05:10:00 【Just do it!ට⋆*】
记录贴:将inria行人检测数据集转化为YOLO可以训练的txt格式
inria行人检测数据集解压后有train和test文件,将里面的标注信息提取出来
转化代码
# coding=UTF-8
import os
import re
from PIL import Image
sets=['train']
#需要填写变量image_path、annotations_path、full_path
image_path = r"D:\BaiduNetdiskDownload\59_INRIA Person Dataset\shuju1/" # 图片存放路径,路径固定
annotations_path = r"D:\BaiduNetdiskDownload\59_INRIA Person Dataset\INRIAPerson\Test\annotations/" #文件夹目录 # INRIA标签存放路径
annotations= os.listdir(annotations_path) #得到文件夹下的所有文件名称
# 获取文件夹下所有图片的图片名
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
# 在labels目录下创建每个图片的标签txt文档
def text_create(name,bnd):
full_path = r"D:\BaiduNetdiskDownload\59_INRIA Person Dataset\labels1/%s.txt"%(name)
size = get_size(name + '.png')
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')
# 获取要查询的图片的w,h
def get_size(image_id):
im = Image.open(r'D:\BaiduNetdiskDownload\59_INRIA Person Dataset\INRIAPerson\Test\pos/%s'%(image_id)) # 源图片存放路径
size = im.size
w = size[0]
h = size[1]
return (w,h)
# 将Tagphoto的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] - box[0]
h = box[3] - box[1]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
# 将处理的图片路径放入一个txt文件夹中
for image_set in sets:
if not os.path.exists(r'D:\BaiduNetdiskDownload\59_INRIA Person Dataset\labels1'):
os.makedirs(r'D:\BaiduNetdiskDownload\59_INRIA Person Dataset\labels1') # 生成的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()
s = []
for file in annotations: #遍历文件夹
str_name = file.replace('.txt', '')
if not os.path.isdir(file): #判断是否是文件夹,不是文件夹才打开
with open(annotations_path+"/"+file) as f : #打开文件
iter_f = iter(f); #创建迭代器
for line in iter_f: #遍历文件,一行行遍历,读取文本
str_XY = "(Xmax, Ymax)"
if str_XY in line:
strlist = line.split(str_XY)
strlist1 = "".join(strlist[1:]) # 把list转为str
strlist1 = strlist1.replace(':', '')
strlist1 = strlist1.replace('-', '')
strlist1 = strlist1.replace('(', '')
strlist1 = strlist1.replace(')', '')
strlist1 = strlist1.replace(',', '')
b = strlist1.split()
bnd = (float(b[0]) ,float(b[1]) ,float(b[2]) ,float(b[3]))
text_create(str_name, bnd)
else:
continue
可视化一下
判断转化是否正确,写了一个可视化代码
import os
import cv2
img_path = r'D:\BaiduNetdiskDownload\59_INRIA Person Dataset\INRIAPerson\Train\pos/'
label_path = r'D:\BaiduNetdiskDownload\59_INRIA Person Dataset\labels/'
f = os.listdir(img_path)
def paint(label_file, img_file):
#读取照片
img = cv2.imread(img_file)
img_h, img_w, _ = img.shape
with open(label_file, 'r') as f:
obj_lines = [l.strip() for l in f.readlines()]
for obj_line in obj_lines:
cls, cx, cy, nw, nh = [float(item) for item in obj_line.split(' ')]
color = (0, 0, 255) if cls == 0.0 else (0, 255, 0)
x_min = int((cx - (nw / 2.0)) * img_w)
y_min = int((cy - (nh / 2.0)) * img_h)
x_max = int((cx + (nw / 2.0)) * img_w)
y_max = int((cy + (nh / 2.0)) * img_h)
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color, 2)
cv2.imshow('Ima', img)
cv2.waitKey(0)
for i in f:
label_path_name = label_path + i.replace('png','txt')
img_path_name = img_path + i
print(label_path_name)
print(img_path_name)
paint(label_path_name,img_path_name)
发现这个数据集的多人场景下只标注了几个人
边栏推荐
- Review my first job search trip
- 1. Dongsoft Cross - Border E - commerce Data Warehouse Requirement specification document
- MySQL learning notes (5) -- join join table query, self join query, paging and sorting, sub query and nested query
- 二叉树的先序、中序、后序遍历
- 软件过程与管理复习(七)
- MySQL transactions
- Wxml template syntax in wechat applet
- 8.数据仓库之ODS层搭建
- 常量与常量指针
- Page navigation of wechat applet
猜你喜欢
Minor problems of GCC compiling C language in ubantu
Common components of wechat applet
MySQL学习笔记(4)——(基本CRUD)操作数据库中的表的数据
Use iceberg in CDP to pressurize the data Lake warehouse
微信小程序之计算器
MySQL事务
The future of data Lakehouse - Open
Using Flink SQL to fluidize market data 2: intraday var
Flutter Intl的使用
3. Neusoft cross border e-commerce data warehouse project architecture design
随机推荐
C语言的指针函数
C language & bit field
C language dynamic memory management
电商用户行为实时分析系统(Flink1.10.1)
软件过程与管理复习(六)
微信小程序的常用組件
指针数组&数组指针
3.东软跨境电商数仓项目架构设计
Page navigation of wechat applet
Solve idea new module prompt module XXXX does exits
图片的大小限制显示
Could not locate zlibwapi.dll. Please make sure it is in your library path
Idea import local package
ETL tool -- kettle realizes simple data migration
Ambari2.7.5 integration es6.4.2
Use Flink SQL to transfer market data 1: transfer VWAP
6.数据仓库搭建之数据仓库设计
MySQL queries the data of the current day, this week, this month and last month
Operation of C language files
微信小程序之计算器