赞
踩
- import cv2
- import numpy as np
- import os
- import glob
-
- # 数据增强函数
- def augment_data(img):
- rows,cols,_ = img.shape
-
- # 水平翻转图像
- if np.random.random() > 0.5:
- img = cv2.flip(img, 1)
- img_name = os.path.splitext(save_path)[0] + "_flip.png"
- cv2.imwrite(img_name, img)
- print("Saved augmented image:", img_name)
-
- # 随机缩放图像
- scale = np.random.uniform(0.9, 1.1)
- M = cv2.getRotationMatrix2D((cols/2, rows/2), 0, scale)
- img_transformed = cv2.warpAffine(img, M, (cols, rows))
- img_name = os.path.splitext(save_path)[0] + "_transform.png"
- cv2.imwrite(img_name, img_transformed)
- print("Saved augmented image:", img_name)
-
- # 随机旋转图像
- angle = np.random.randint(-10, 10)
- M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1)
- img_rotated = cv2.warpAffine(img, M, (cols, rows))
- img_name = os.path.splitext(save_path)[0] + "_rotated.png"
- cv2.imwrite(img_name, img_rotated)
- print("Saved augmented image:", img_name)
-
- # 添加高斯噪音
- mean = 0
- std = np.random.uniform(5, 15)
- noise = np.zeros(img.shape, np.float32)
- cv2.randn(noise, mean, std)
- noise = np.uint8(noise)
- img_noisy = cv2.add(img, noise)
- img_name = os.path.splitext(save_path)[0] + "_noisy.png"
- cv2.imwrite(img_name, img_noisy)
- print("Saved augmented image:", img_name)
-
- # 随机调整对比度和亮度
- alpha = np.random.uniform(0.8, 1.2)
- beta = np.random.randint(-10, 10)
- img_contrast = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)
- img_name = os.path.splitext(save_path)[0] + "_contrast.png"
- cv2.imwrite(img_name, img_contrast)
- print("Saved augmented image:", img_name)
-
- return img
-
-
- # 读取 data 文件夹中的所有图片,并进行数据增强
- data_dir = "data"
- save_dir = "result"
- if not os.path.exists(save_dir):
- os.makedirs(save_dir)
-
- # 使用 glob 库来遍历 data 文件夹中所有图像
- for img_path in glob.glob(os.path.join(data_dir, "*.png")):
-
- img = cv2.imread(img_path)
-
- # 获取保存增强后的图片文件名
- img_name = os.path.basename(img_path)
- save_path = os.path.join(save_dir, img_name)
-
- # 数据增强
- augment_data(img)
-
- # 保存原始图片
- cv2.imwrite(save_path, img)
- print("Saved original image:", save_path)
XML格式数据集转TXT(YOLO)_xml转txt-CSDN博客
yolov8-制作数据集,数据集格式转换(yolo格式-voc格式)附完整代码_yolov8数据集格式-CSDN博客
yolo图像检测数据集格式转换:xml 与 txt格式相互转换_yolo .csv文件转.xml文件-CSDN博客
-
- import os
- import xml.etree.ElementTree as ET
-
- # 定义类别列表
- classes = ["crack", "porosity", "normal"] # 根据您的数据集进行修改
-
- # 输入和输出文件夹路径
- xml_folder = "F:/train/Annotations"
- txt_folder = "F:/train/labels"
- os.makedirs(txt_folder, exist_ok=True)
-
- # 解析XML文件并转换为YOLO格式
- for xml_file in os.listdir(xml_folder):
- if xml_file.endswith(".xml"):
- # 解析XML
- tree = ET.parse(os.path.join(xml_folder, xml_file))
- root = tree.getroot()
-
- txt_file_path = os.path.join(txt_folder, os.path.splitext(xml_file)[0] + ".txt")
- with open(txt_file_path, "w") as txt_file:
- for obj in root.findall("object"):
- # 提取类别、边界框信息
- class_name = obj.find("name").text
- if class_name not in classes:
- continue
- class_id = classes.index(class_name)
-
- bbox = obj.find("bndbox")
- xmin = int(bbox.find("xmin").text)
- ymin = int(bbox.find("ymin").text)
- xmax = int(bbox.find("xmax").text)
- ymax = int(bbox.find("ymax").text)
-
- # 计算边界框中心点和宽高
- width = xmax - xmin
- height = ymax - ymin
- x_center = (xmin + xmax) / 2
- y_center = (ymin + ymax) / 2
-
- # 归一化坐标
- img_width = int(root.find("size").find("width").text)
- img_height = int(root.find("size").find("height").text)
- x_center /= img_width
- y_center /= img_height
- width /= img_width
- height /= img_height
-
- # 写入TXT文件
- txt_file.write(f"{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}\n")
-
- print("转换完成!")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。