mirror of
https://gitee.com/nanjing-yimao-information/ieemoo-ai-gift.git
synced 2025-08-18 21:30:25 +00:00
update
This commit is contained in:
69
datasets_preprocess/split_train_val_.py
Normal file
69
datasets_preprocess/split_train_val_.py
Normal file
@ -0,0 +1,69 @@
|
||||
# 加入负样本,根据图片数量划分数据集
|
||||
import os
|
||||
import random
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--img_path', default='/home/lc/data_center/gift/ori_image/images', type=str,
|
||||
help='input xml label path') # 图片存放地址
|
||||
# 数据集的划分,地址选择自己数据下的ImageSets/Main
|
||||
parser.add_argument('--txt_path', default='/home/lc/data_center/gift/yolov10_data/Main', type=str,
|
||||
help='output txt label path')
|
||||
opt = parser.parse_args()
|
||||
|
||||
trainval_percent = 1.0
|
||||
train_percent = 0.8
|
||||
val_percent = 1.0
|
||||
# train_percent = 1.0
|
||||
# val_percent = 0.0
|
||||
|
||||
imgfilepath = opt.img_path
|
||||
txtsavepath = opt.txt_path
|
||||
total_img = os.listdir(imgfilepath)
|
||||
if not os.path.exists(txtsavepath):
|
||||
os.makedirs(txtsavepath)
|
||||
|
||||
num = len(total_img)
|
||||
print("all num:", num)
|
||||
list_index = range(num)
|
||||
tv = int(num * trainval_percent)
|
||||
tr = int(tv * train_percent)
|
||||
trainval = random.sample(list_index, tv)
|
||||
train = random.sample(trainval, tr)
|
||||
|
||||
val_test = []
|
||||
for i in trainval:
|
||||
if i not in train:
|
||||
val_test.append(i)
|
||||
num_ = len(val_test)
|
||||
print("val-test num:", num_)
|
||||
list_index_ = range(num_)
|
||||
va = int(num_ * val_percent)
|
||||
val = random.sample(val_test, va)
|
||||
|
||||
file_trainval = open(txtsavepath + '/trainval.txt', 'w')
|
||||
file_test = open(txtsavepath + '/test.txt', 'w')
|
||||
file_train = open(txtsavepath + '/train.txt', 'w')
|
||||
file_val = open(txtsavepath + '/val.txt', 'w')
|
||||
|
||||
addtrain_path = ""
|
||||
for i in list_index:
|
||||
name = total_img[i][:-4] + '\n'
|
||||
addimg_name = name.strip() + ".jpg"
|
||||
# print(addimg_name,type(addimg_name),len(addimg_name))
|
||||
if i in trainval:
|
||||
file_trainval.write(name)
|
||||
# if addimg_name in os.listdir(addtrain_path):#把某些数据加入训练集中
|
||||
# print("addimg_name:",addimg_name)
|
||||
# file_train.write(name)
|
||||
if i in train:
|
||||
file_train.write(name)
|
||||
if i in val:
|
||||
file_val.write(name)
|
||||
if (i not in train) and (i not in val):
|
||||
file_test.write(name)
|
||||
|
||||
file_trainval.close()
|
||||
file_train.close()
|
||||
file_val.close()
|
||||
file_test.close()
|
101
datasets_preprocess/voc_label_.py
Normal file
101
datasets_preprocess/voc_label_.py
Normal file
@ -0,0 +1,101 @@
|
||||
# 标签xml转txt以及无xml的负样本空txt
|
||||
import xml.etree.ElementTree as ET
|
||||
import os, cv2
|
||||
from os import getcwd
|
||||
|
||||
sets = ['train', 'val', 'test']
|
||||
|
||||
classes = ['tag', 'bandage']
|
||||
|
||||
|
||||
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
|
||||
y = y * dh
|
||||
w = w * dw
|
||||
h = h * dh
|
||||
return x, y, w, h
|
||||
|
||||
|
||||
def convert_annotation(image_id, imgname_list, label_path, Annotation_path, image_path):
|
||||
out_file = open(label_path + '%s.txt' % (image_id), 'w')
|
||||
if os.path.exists(Annotation_path + '%s.xml' % (image_id)):
|
||||
try:
|
||||
in_file = open(Annotation_path + '%s.xml' % (image_id), encoding='UTF-8')
|
||||
tree = ET.parse(in_file)
|
||||
root = tree.getroot()
|
||||
size = root.find('size')
|
||||
# if size!=None:
|
||||
# w = int(size.find('width').text)
|
||||
# h = int(size.find('height').text)
|
||||
# else:
|
||||
img_name = image_id + ".jpg"
|
||||
img_path = image_path + f"{img_name}"
|
||||
img_data_ = cv2.imread(img_path)
|
||||
h, w = img_data_.shape[0], img_data_.shape[1]
|
||||
|
||||
for obj in root.iter('object'):
|
||||
# difficult = obj.find('difficult').text
|
||||
# difficult = obj.find('Difficult').text
|
||||
cls = obj.find('name').text
|
||||
# cls = cls[:13]
|
||||
# print("cls:", cls, len(cls))
|
||||
# if cls not in classes or int(difficult) == 1:
|
||||
# continue
|
||||
try:
|
||||
cls_id = classes.index(cls)
|
||||
xmlbox = obj.find('bndbox')
|
||||
b = (
|
||||
float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
|
||||
float(xmlbox.find('ymax').text))
|
||||
|
||||
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')
|
||||
|
||||
except Exception as e:
|
||||
print(e, image_id)
|
||||
imgname_list.append(image_id)
|
||||
except Exception as e:
|
||||
print('error:', e, image_id)
|
||||
else:
|
||||
out_file.write('\n')
|
||||
|
||||
|
||||
# abs_path = os.getcwd()
|
||||
|
||||
|
||||
image_path = '/home/lc/data_center/gift/ori_image/images/' # img实际存放地址
|
||||
Annotation_path = '/home/lc/data_center/gift/ori_image/xmls/' # xml实际存放地址
|
||||
label_path = '/home/lc/data_center/gift/ori_image/labels/' # 保存路径
|
||||
|
||||
# wd = getcwd()
|
||||
imgname_list = []
|
||||
for image_set in sets:
|
||||
if not os.path.exists(label_path):
|
||||
os.makedirs(label_path)
|
||||
# image_ids = open('data/ImageSets/Main/%s.txt' % (image_set), encoding='gbk').read().strip().split()
|
||||
# image_ids = open('data/ImageSets/Main_0820/%s.txt' % (image_set)).read().strip().split()
|
||||
image_ids = open('/home/lc/data_center/gift/yolov10_data/Main/%s.txt' % (image_set)).read().split('\n')
|
||||
list_file = open('/home/lc/data_center/gift/yolov10_data/%s.txt' % (image_set), 'w')
|
||||
for image_id in image_ids:
|
||||
list_file.write(image_path + '%s.jpg\n' % (image_id))
|
||||
# print(image_id, "Converting...")
|
||||
convert_annotation(image_id, imgname_list, label_path, Annotation_path, image_path)
|
||||
|
||||
list_file.close()
|
||||
with open('error_img.txt', 'w+') as f:
|
||||
for i_name in imgname_list:
|
||||
f.write(i_name + '\n')
|
||||
print("error imgname_list:", imgname_list)
|
Reference in New Issue
Block a user