01 labelme json 转 txt(w_convert_labelme_to_yolo.py)
#WT 将labelme json标签格式转换为YOLO txt格式
# 导入所需模块
import cv2 # OpenCV用于图像处理
import os # 操作系统路径管理
import json # JSON文件解析
import glob # 文件通配符搜索
import numpy as np # 数值计算
def convert_json_label_to_yolov_seg_label():
# 设置JSON文件路径(本地标注数据目录)
json_path = r"data" # 本地json路径
# 获取所有JSON文件列表
json_files = glob.glob(json_path + "/*.json")
print(json_files)
# 创建输出目录(YOLO格式标签存储路径)
output_folder = "txt2" # txt存放路径
if not os.path.exists(output_folder):
os.makedirs(output_folder) # 递归创建目录
# 遍历处理每个JSON文件
for json_file in json_files:
print(json_file)
# 读取并解析JSON文件
with open(json_file, 'r') as f:
json_info = json.load(f)
# 加载对应图像获取尺寸
img = cv2.imread(os.path.join(json_path, json_info["imagePath"]))
height, width, _ = img.shape
np_w_h = np.array([[width, height]], np.int32) # 转换为numpy数组便于计算
# 构建输出文件路径(保持与JSON同名,后缀改为.txt)
txt_file = os.path.join(output_folder, os.path.basename(json_file).replace(".json", ".txt"))
# 写入YOLO格式标签
with open(txt_file, "w") as f:
for point_json in json_info["shapes"]:
txt_content = ""
# 转换坐标点为numpy数组
np_points = np.array(point_json["points"], np.int32)
# 归一化坐标(除以图像宽高)
norm_points = np_points / np_w_h
# 转换为列表并格式化输出
norm_points_list = norm_points.tolist()
# 拼接类别标签和坐标点(YOLO格式要求:类别+归一化坐标)
txt_content += "0 " + " ".join([" ".join([str(cell[0]), str(cell[1])]) for cell in norm_points_list]) + "\n"
f.write(txt_content)
if __name__ == "__main__":
# 执行转换函数
convert_json_label_to_yolov_seg_label()
02 检查图片和标签文件是否对应python脚本(w_check_img_to_label.py)
#WT:检查图片和标签是否匹配2025-04-06
import os
def get_file_names(directory):
file_names = os.listdir(directory)
return file_names
labels_path = 'train/labels'
labels_names_list = get_file_names(labels_path)
images_path = 'train/images'
images_names_list = get_file_names(images_path)
for i in range(len(images_names_list)):
filename = images_names_list[i][0:-3] + 'txt'
if filename in labels_names_list:
continue
else:
print(f"【{filename}】标签不存在!")
continue
print("检查结束")
03 从labeme标注的json文件提取标签label类别class脚本(w_extract_json_label.py)
#WT:从labeme标注的json文件提取标签label类别class
#WT 将labelme json标签格式转换为YOLO txt格式
# 导入所需模块
import os # 操作系统路径管理
import json # JSON文件解析
import glob # 文件通配符搜索
def extract_labelme_json_label():
# 设置JSON文件路径(本地标注数据目录)
json_path = r"data" # 本地json路径
# 获取所有JSON文件列表
json_files = glob.glob(json_path + "/*.json")
#print(json_files)
label_class_list = []
# 遍历处理每个JSON文件
for json_file in json_files:
print(json_file)
# 读取并解析JSON文件
with open(json_file, 'r') as f:
json_info = json.load(f)
json_shapes = json_info["shapes"]
#print(json_shapes,type(json_shapes))
for i in range(len(json_shapes)):
#print(json_shapes[i]["label"])
if json_shapes[i]["label"] in label_class_list:
continue
else:
label_class_list.append(json_shapes[i]["label"])
print("完成!")
print(label_class_list)
if __name__ == "__main__":
# 执行函数
extract_labelme_json_label()
04 重置图片格式大小【脚本存在一些问题】(w_resize_train_images.py)
#重置图片格式大小
import os
import cv2
import numpy as np
# 定义letterbox函数,用于对图像进行缩放和填充,使其适应指定尺寸
def letterbox(img, new_shape=(640, 640), color=(255, 255, 255), auto=False, scale_fill=False, scale_up=False, stride=32):
shape = img.shape[:2] # 获取图像的原始高度和宽度
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape) # 如果new_shape是整数,则将其转换为方形尺寸
# 计算缩放比例r,确保图像不会拉伸变形
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scale_up:
r = min(r, 1.0) # 如果scale_up为False,则缩放比例不大于1,避免放大
ratio = r, r # 记录宽高的缩放比例
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) # 根据比例计算缩放后的尺寸
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # 计算需要填充的宽度和高度
if auto:
dw, dh = np.mod(dw, stride), np.mod(dh, stride) # 如果auto为True,将填充尺寸调整为步长的倍数
elif scale_fill:
dw, dh = 0.0, 0.0 # 如果scale_fill为True,则不进行填充,直接拉伸到目标尺寸
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # 重新计算缩放比例
dw /= 2 # 左右填充的宽度
dh /= 2 # 上下填充的高度
if shape[::-1] != new_unpad: # 如果原始尺寸和缩放后的尺寸不同,进行图像缩放
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) # 线性插值进行图像缩放
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) # 上下填充像素数
left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) # 左右填充像素数
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # 使用指定颜色填充
return img, ratio, (dw, dh) # 返回处理后的图像、缩放比例以及填充的宽高
# 保存经过letterbox处理的图像
def save_letterboxed_image(image_bytes, output_filename, new_shape=(1280, 1280)):
im0 = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), cv2.IMREAD_COLOR) # 从字节数据解码出图像
if im0 is None:
raise ValueError("Failed to decode image from image_bytes") # 如果图像解码失败,抛出异常
img, _, _ = letterbox(im0, new_shape=new_shape) # 对图像进行letterbox处理
# 保存处理后的图像
cv2.imwrite(output_filename, img) # 将处理后的图像写入文件
print(f"Saved letterboxed image to {output_filename}") # 输出保存成功的信息
# 处理文件夹中的所有图像
def process_images_in_folder(folder_path, output_folder, new_shape=(1280, 1280)):
if not os.path.exists(output_folder):
os.makedirs(output_folder) # 如果输出文件夹不存在,则创建
# 遍历文件夹中的所有图像文件
for filename in os.listdir(folder_path):
if filename.endswith(('.jpg', '.png', '.jpeg')): # 仅处理特定格式的图像文件
image_path = os.path.join(folder_path, filename) # 获取图像的完整路径
with open(image_path, 'rb') as f:
image_bytes = f.read() # 读取图像文件的字节数据
output_filename = os.path.join(output_folder, filename) # 构建输出文件的完整路径
save_letterboxed_image(image_bytes, output_filename, new_shape=new_shape) # 保存处理后的图像
# 使用示例
if __name__ == "__main__":
folder_path = r'train/images' # 替换为你的图片文件夹路径
output_folder = 'resizeimg' # 替换为保存处理后图片的文件夹路径
process_images_in_folder(folder_path, output_folder, new_shape=(640, 640)) # 处理文件夹中的图像
05 class.txt
desquamate
hollow
cracking
06 data.yaml
train: ../train/images
val: ../valid/images
test: ../test/images
nc: 3
names: ['desquamate', 'hollow', 'cracking']
代码参考:
Labelme的安装与使用教程_labelme安装及使用教程-CSDN博客
YOLO11 图像缩放 | 图像填充 | 自适应不同尺寸的图片_yolo训练 图片不同大小-CSDN博客