# -*- coding: utf-8 -*-
import json
import os
import random
import shutil
def json2yolo(json_folder, class_dic):
"""讲labelme的json文件转成txt文件 {"notch": '0'}"""
file_list = os.listdir(json_folder)
txt_folder = os.path.dirname(json_folder) + "\labels_txt"
if not os.path.exists(txt_folder):
os.makedirs(txt_folder)
json_file_list = [x for x in file_list if ".json" in x] # 获取所有json文件的路径
for p in json_file_list:
json_file = os.path.join(json_folder, p)
with open(json_file, encoding='utf-8') as f:
data = json.loads(f.read())
img_w = data["imageWidth"] # 获取json文件里图片的宽高
img_h = data["imageHeight"]
all_line = ''
for i in data["shapes"]:
# 归一化坐标点。并得到cx,cy,w,h
[[x1, y1], [x2, y2]] = i['points']
x1, x2 = x1 / img_w, x2 / img_w
y1, y2 = y1 / img_h, y2 / img_h
cx = (x1 + x2) / 2
cy = (y1 + y2) / 2
w = abs(x2 - x1)
h = abs(y2 - y1)
# 将数据组装成yolo格式
line = f"%s %.4f %.4f %.4f %.4f\n" % (class_dic[i['label']], cx, cy, w, h) # 生成txt文件里每行的内容
all_line += line
# print(all_line)
txt_file = os.path.join(txt_folder, p.replace('json', 'txt'))
with open(txt_file, 'w', encoding='utf-8') as fh:
fh.write(all_line)
# json2yolo(r'D:\Web_Decryption\SEASON\0527\test', {"殊":"0","乡":"1","趋":"2",})
def shape2id(json_folder):
classes2id = {}
classes_all = set()
num = 0
jsons = os.listdir(json_folder)
for i in jsons:
json_path = os.path.join(json_folder, i)
with open(json_path, 'r', encoding="utf-8") as f:
json_data = json.load(f)
# print(json_data['shapes'])
for j in json_data['shapes']:
if j['label'] not in classes2id:
classes2id[j['label']] = num
num += 1
classes_all.add(j['label'])
print(classes2id)
print("所有分类", classes_all)
return classes2id
# shape2id(r'D:\day_soft\Labelme\custom_model\lably')
def split_images_floder(img_dir, label_dir, img_suffix='.png'):
"""
将数据集划分为训练集,验证集,测试集
"""
# 1.确定原图片数据集路径
datasetimg_dir = img_dir
# 确定原label数据集路径
datasetlabel_dir = label_dir
# 2.确定数据集划分后保存的路径
_f = os.path.dirname(img_dir)
split_dir = os.path.join(_f, "dataset")
train_dir = os.path.join(split_dir, "train")
valid_dir = os.path.join(split_dir, "valid")
test_dir = os.path.join(split_dir, "test")
dir_list = [train_dir, valid_dir, test_dir]
image_label = ['images', 'labels']
for i in range(len(dir_list)):
for j in range(len(image_label)):
new_dir = os.path.join(dir_list[i], image_label[j])
if not os.path.exists(new_dir):
os.makedirs(new_dir)
# 3.确定将数据集划分为训练集,验证集,测试集的比例
train_pct = 0.9
valid_pct = 0.1
test_pct = 0
# 4.划分
imgs = os.listdir(datasetimg_dir) # 展示目标文件夹下所有的文件名
imgs = list(filter(lambda x: x.endswith(img_suffix), imgs)) # 取到所有以.png结尾的文件,如果改了图片格式,这里需要修改
random.shuffle(imgs) # 乱序路径
img_count = len(imgs) # 计算图片数量
train_point = int(img_count * train_pct) # 0:train_pct
valid_point = int(img_count * (train_pct + valid_pct)) # train_pct:valid_pct
for i in range(img_count):
if i < train_point: # 保存0-train_point的图片到训练集
out_dir = os.path.join(train_dir, 'images')
label_out_dir = os.path.join(train_dir, 'labels')
elif i < valid_point: # 保存train_point-valid_point的图片到验证集
out_dir = os.path.join(valid_dir, 'images')
label_out_dir = os.path.join(valid_dir, 'labels')
else: # 保存test_point-结束的图片到测试集
out_dir = os.path.join(test_dir, 'images')
label_out_dir = os.path.join(test_dir, 'labels')
target_path = os.path.join(out_dir, imgs[i]) # 指定目标保存路径
src_path = os.path.join(datasetimg_dir, imgs[i]) # 指定目标原图像路径
label_target_path = os.path.join(label_out_dir, imgs[i][0:-4] + '.txt')
label_src_path = os.path.join(datasetlabel_dir, imgs[i][0:-4] + '.txt')
shutil.copy(src_path, target_path) # 复制图片
shutil.copy(label_src_path, label_target_path) # 复制txt
print('train:{}, valid:{}, test:{}'.format(train_point, valid_point - train_point, img_count - valid_point))
#split_images_floder(r"C:\Users\24572\PycharmProjects\pythonProject\360click\images", r"C:\Users\24572\PycharmProjects\pythonProject\360click\labels")
def make_yolo_yaml(yaml_name, image_dir, class_names):
"""生成yolo格式的yaml文件"""
if not os.path.exists(yaml_name):
os.makedirs(yaml_name)
nums = len(class_names)
# 生成数据集配置.yaml
with open(yaml_name, "w", encoding='utf-8') as f:
f.write(f"train: {image_dir}/train/images\n")
f.write(f"val: {image_dir}/valid/images\n")
f.write(f"test: {image_dir}/test/images\n")
f.write("\n")
f.write(f"nc: {nums}\n")
f.write(f"names: {class_names}\n")
make_yolo_yaml(r"C:\Users\24572\PycharmProjects\pythonProject\360click\font.yaml", r'C:\Users\24572\PycharmProjects\pythonProject\360click\dataset', ["labels"])
def create_valid_test_folder(image_dir, labels_dir, img_suffix='.jpg'):
"""
:param image_dir: D:\Yzmtrain\yzmdata\dyclick_524\images_all
:param labels_dir: D:\Yzmtrain\yzmdata\dyclick_524\labels_all
:param img_suffix:
:return:
"""
_folder = os.path.dirname(image_dir)
for dirs in ['train/images', 'train/labels', 'valid/images', 'valid/labels', 'test/images', 'test/labels']:
try:
os.makedirs(os.path.join(image_dir, dirs))
except FileExistsError:
print(f'{dirs} 目录已存在')
image_file_list = os.listdir(image_dir)
total_num = len(image_file_list)
train_num = int((total_num-10)/7)
valid_num = total_num - 10 - train_num
for img_name in image_file_list[:10]:
json_name = img_name.replace(img_suffix, ".json")
img_src = os.path.join(image_dir, img_name)
dst_images = os.path.join(f"{_folder}/test/images", img_name)
os.rename(img_src, dst_images)
labels_src = os.path.join(labels_dir, json_name)
dst_labels = os.path.join(f"{_folder}/test/labels", json_name)
os.rename(labels_src, dst_labels)
def rename_file():
aa_all = {}
json_folder = "D:\Yzmtrain\yzmdata\hanzi_1\hanzi\hanzi\js11"
file_list = os.listdir(json_folder)
json_file_list = [x for x in file_list if ".json" in x] # 获取所有json文件的路径
for p in json_file_list:
json_file = os.path.join(json_folder, p)
image_file = json_file.replace("\js11", '').replace("json", "jpg")
with open(json_file, encoding='utf-8') as f:
data = json.loads(f.read())
print(json_file, image_file)
print(p, data['shapes'][0]['label'])
aa_all[p] = f"{data['shapes'][0]['label']}_{p[2:]}"
os.rename(json_file, f"D:\Yzmtrain\yzmdata\hanzi_1\hanzi\hanzi\js11\\{data['shapes'][0]['label']}_{p[2:]}")
os.rename(image_file, f"D:\Yzmtrain\yzmdata\hanzi_1\hanzi\hanzi\\{data['shapes'][0]['label']}_{p[2:]}".replace("json", "jpg"))
print(aa_all)
# rename_file()
# _folder = "D:\Yzmtrain\yzmdata\chinese"
# file_list = os.listdir(_folder)
# json_file_list = [x for x in file_list if ".json" in x] # 获取所有json文件的路径
# for p in json_file_list:
# json_file = os.path.join(_folder, p)
# os.rename(json_file, f"D:\Yzmtrain\yzmdata\chinese_json\\{p}")