import xml.etree.ElementTree as ET
import pickle
import os, cv2
from os import listdir, getcwd
from os.path import join
from tqdm import tqdm
sets = ['train', 'test','val']
classes = ['helmet', 'head']
colors = {'helmet': (60, 60, 250), 'head': (250, 60, 60)}
root_path = "helmet_source"
dataSet_path = "%s/dataSet_path" % root_path
image_path = "%s/images" % root_path
Annotations_path = "%s/Annotations" % root_path
ImageSets_path = "%s/ImageSets" % root_path
labels_path = "%s/labels" % root_path
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
# x_center = (box[0]+box[1])/2.0
# y_center = (box[2]+box[3])/2.0
# x = x_center / size[0]
# y = y_center / size[1]
# w = (box[1] - box[0]) / size[0]
# h = (box[3] - box[2]) / size[1]
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open('%s/%s.xml' % (Annotations_path, image_id))
out_file = open('%s/%s.txt' % (labels_path, image_id), 'w')
im = cv2.imread('%s/%s.png' % (image_path, image_id))
print('%s/%s.png' % (image_path, image_id))
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
xmin = xmlbox.find('xmin').text
xmax = xmlbox.find('xmax').text
ymin = xmlbox.find('ymin').text
ymax = xmlbox.find('ymax').text
b = (float(xmin), float(xmax), float(ymin), float(ymax))
b1, b2, b3, b4 = b
# 标注越界修正
if b2 > w:
b2 = w
if b4 > h:
b4 = h
b = (b1, b2, b3, b4)
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
print(cls, colors[cls], xmin, xmax, ymin, ymax)
cv2.rectangle(im, (int(xmin), int(ymin)), (int(xmax), int(ymax)), colors[cls])
cv2.putText(im, cls, (int(xmin), int(ymin) - 3), cv2.FONT_HERSHEY_SIMPLEX, 0.5, colors[cls])
# cv2.imshow('result', im)
# cv2.waitKey(0)
cv2.imwrite('%s/%s_tag.png' % (image_path, image_id), im)
wd = getcwd()
print(wd)
for image_set in sets:
if not os.path.exists(labels_path):
os.makedirs(labels_path)
image_ids = open('%s/%s.txt' % (ImageSets_path, image_set)).read().strip().split()
list_file = open('%s/%s.txt' % (dataSet_path, image_set), 'w')
for image_id in image_ids:
# print(image_id)
list_file.write('%s/%s.png\n' % (image_path, image_id))
convert_annotation(image_id)
list_file.close()
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
yolov5训练安全帽检测所需资源及脚本 (425个子文件)
events.out.tfevents.1659415309.ubuntu.9031.0 983KB
results.csv 120KB
labels_correlogram.jpg 317KB
labels.jpg 234KB
train_batch1.jpg 135KB
train_batch2.jpg 129KB
train_batch0.jpg 120KB
val_batch0_pred.jpg 73KB
val_batch0_labels.jpg 72KB
hard_hat_workers184.png 400KB
hard_hat_workers89.png 360KB
hard_hat_workers164.png 354KB
hard_hat_workers66.png 347KB
hard_hat_workers178.png 343KB
hard_hat_workers143.png 338KB
hard_hat_workers168.png 336KB
hard_hat_workers49.png 335KB
hard_hat_workers197.png 335KB
hard_hat_workers154.png 330KB
hard_hat_workers24.png 330KB
hard_hat_workers153.png 325KB
hard_hat_workers90.png 325KB
hard_hat_workers111.png 322KB
hard_hat_workers58.png 321KB
hard_hat_workers45.png 318KB
hard_hat_workers177.png 318KB
hard_hat_workers193.png 318KB
hard_hat_workers157.png 316KB
hard_hat_workers19.png 315KB
hard_hat_workers118.png 313KB
hard_hat_workers130.png 312KB
hard_hat_workers181.png 312KB
hard_hat_workers136.png 312KB
hard_hat_workers63.png 311KB
hard_hat_workers105.png 311KB
hard_hat_workers34.png 311KB
hard_hat_workers22.png 311KB
hard_hat_workers116.png 310KB
hard_hat_workers42.png 308KB
hard_hat_workers69.png 306KB
hard_hat_workers126.png 305KB
hard_hat_workers17.png 302KB
hard_hat_workers119.png 301KB
hard_hat_workers52.png 300KB
hard_hat_workers159.png 300KB
hard_hat_workers120.png 297KB
hard_hat_workers3.png 297KB
hard_hat_workers1.png 296KB
hard_hat_workers76.png 296KB
hard_hat_workers139.png 295KB
hard_hat_workers141.png 295KB
hard_hat_workers121.png 295KB
hard_hat_workers133.png 292KB
hard_hat_workers15.png 291KB
hard_hat_workers144.png 290KB
hard_hat_workers95.png 289KB
hard_hat_workers161.png 289KB
hard_hat_workers84.png 289KB
hard_hat_workers46.png 287KB
hard_hat_workers56.png 287KB
hard_hat_workers165.png 286KB
hard_hat_workers190.png 286KB
hard_hat_workers81.png 285KB
hard_hat_workers78.png 285KB
hard_hat_workers67.png 284KB
hard_hat_workers98.png 284KB
hard_hat_workers109.png 282KB
hard_hat_workers62.png 280KB
hard_hat_workers173.png 279KB
hard_hat_workers117.png 279KB
hard_hat_workers189.png 278KB
hard_hat_workers73.png 277KB
hard_hat_workers57.png 277KB
hard_hat_workers61.png 277KB
hard_hat_workers158.png 277KB
hard_hat_workers134.png 277KB
hard_hat_workers38.png 276KB
hard_hat_workers39.png 275KB
hard_hat_workers127.png 275KB
hard_hat_workers12.png 273KB
hard_hat_workers92.png 273KB
hard_hat_workers91.png 272KB
hard_hat_workers160.png 270KB
hard_hat_workers48.png 269KB
hard_hat_workers59.png 268KB
hard_hat_workers102.png 268KB
hard_hat_workers93.png 267KB
hard_hat_workers50.png 267KB
hard_hat_workers96.png 267KB
hard_hat_workers131.png 266KB
hard_hat_workers114.png 266KB
hard_hat_workers140.png 265KB
hard_hat_workers148.png 265KB
hard_hat_workers11.png 265KB
hard_hat_workers191.png 265KB
hard_hat_workers188.png 264KB
hard_hat_workers87.png 264KB
hard_hat_workers138.png 264KB
hard_hat_workers172.png 264KB
hard_hat_workers115.png 263KB
共 425 条
- 1
- 2
- 3
- 4
- 5
韩搏
- 粉丝: 311
- 资源: 59
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0