This commit is contained in:
琉璃月光
2025-10-21 14:11:52 +08:00
parent 349449f2b7
commit df7c0730f5
363 changed files with 5386 additions and 578 deletions

View File

@ -0,0 +1,83 @@
import os
import shutil
import random
def split_train_val_test(train_dir, val_dir, test_dir, val_ratio=0.1, test_ratio=0.1):
"""
将 train 目录中的数据按比例划分为 train/val/test 三部分。
val 和 test 各自获得不同的一部分数据。
"""
# 检查目录
if not os.path.exists(train_dir):
raise FileNotFoundError(f"训练目录不存在: {train_dir}")
os.makedirs(val_dir, exist_ok=True)
os.makedirs(test_dir, exist_ok=True)
# 支持的图片格式
img_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp'}
# 获取所有图片文件
all_files = os.listdir(train_dir)
image_files = [f for f in all_files if os.path.splitext(f.lower())[1] in img_extensions]
# 配对图片和标签
pairs = []
for img in image_files:
name_no_ext = os.path.splitext(img)[0]
txt = name_no_ext + '.txt'
if txt in all_files:
pairs.append((img, txt))
else:
print(f"⚠️ 忽略 {img}:缺少对应标签文件")
if len(pairs) == 0:
print("❌ 未找到有效数据对")
return
total = len(pairs)
num_test = int(total * test_ratio)
num_val = int(total * val_ratio)
print(f"✅ 共找到 {total} 组有效数据")
print(f"✅ 将移动val={num_val}, test={num_test}")
# 打乱并抽样
random.shuffle(pairs)
test_sample = pairs[:num_test]
val_sample = pairs[num_test:num_test + num_val]
# 剩下的留在 train
# 移动到 test
for img, txt in test_sample:
try:
shutil.move(os.path.join(train_dir, img), os.path.join(test_dir, img))
shutil.move(os.path.join(train_dir, txt), os.path.join(test_dir, txt))
except Exception as e:
print(f"❌ 移动到 test 失败: {img}, {e}")
# 移动到 val
for img, txt in val_sample:
try:
shutil.move(os.path.join(train_dir, img), os.path.join(val_dir, img))
shutil.move(os.path.join(train_dir, txt), os.path.join(val_dir, txt))
except Exception as e:
print(f"❌ 移动到 val 失败: {img}, {e}")
print(f"\n✅ 划分完成!")
print(f" train 保留: {total - num_val - num_test}")
print(f" val : {num_val}")
print(f" test : {num_test}")
print(f" 所有 val 和 test 数据已从 train 移出。")
# ========================
# 使用示例
# ========================
if __name__ == "__main__":
train_dir = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb3/train"
val_dir = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb3/val"
test_dir = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb3/test"
split_train_val_test(train_dir, val_dir, test_dir, val_ratio=0.1, test_ratio=0.1)
# ========================

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 368 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 377 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 403 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 380 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 309 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 393 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 387 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 386 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 381 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 381 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 392 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 413 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 359 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 362 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 376 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 396 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 382 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 366 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 376 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 356 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 396 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 441 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 346 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 392 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 369 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 421 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

View File

@ -0,0 +1,49 @@
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150642365.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153132629.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144341882.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014154328204.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153839977.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014145006232.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150210497.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144500393.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153257985.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150246105.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153420611.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153918385.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144421259.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153506692.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144222222.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150720686.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014145128164.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144538968.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014151150768.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014145321687.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144303840.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_202510141514352.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_2025101415103290.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_2025101415152183.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150444617.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014145045330.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144814974.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153338300.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150918102.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014151311222.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153215762.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_2025101415111060.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014151922555.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144144937.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014154037360.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014151232368.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144619387.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014154121106.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150601333.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150325231.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014151353357.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014154203390.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014145244471.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153957167.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014150406560.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_2025101415052359.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153632704.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014144929890.jpg
/home/hx/yolo/yolo11_obb/image_lianghua/192.168.0.234_01_20251014153800800.jpg

View File

@ -0,0 +1,53 @@
import os
import shutil
def move_images_to_labels(image_dir, label_dir):
"""
将标签对应的图片从图片文件夹移动到标签文件夹。
:param image_dir: 存放图片的文件夹路径
:param label_dir: 存放标签的文件夹路径,同时也是目标文件夹
"""
# 支持的图片扩展名集合
IMG_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', '.webp'}
# 遍历标签文件夹中的所有文件
for label_filename in os.listdir(label_dir):
if not os.path.isfile(os.path.join(label_dir, label_filename)):
continue # 跳过非文件项(如目录)
# 获取文件的基础名称(不带扩展名)
base_name, _ = os.path.splitext(label_filename)
# 在图片文件夹中查找对应的所有可能扩展名的图片文件
found_image = False
for ext in IMG_EXTENSIONS:
image_filename = base_name + ext
image_path = os.path.join(image_dir, image_filename)
if os.path.exists(image_path):
# 构造目标路径
target_path = os.path.join(label_dir, image_filename)
# 移动图片文件到标签文件夹
print(f"移动 {image_path}{target_path}")
shutil.move(image_path, target_path)
found_image = True
break
if not found_image:
print(f"未找到与标签 {label_filename} 对应的图片")
if __name__ == "__main__":
IMAGE_SOURCE_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/images" # 替换为你的图片文件夹路径
LABEL_TARGET_DIR = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/obb4/labels" # 替换为你的标签文件夹路径
if not os.path.isdir(IMAGE_SOURCE_DIR):
print(f"图片文件夹不存在: {IMAGE_SOURCE_DIR}")
elif not os.path.isdir(LABEL_TARGET_DIR):
print(f"标签文件夹不存在: {LABEL_TARGET_DIR}")
else:
move_images_to_labels(IMAGE_SOURCE_DIR, LABEL_TARGET_DIR)

View File

@ -116,8 +116,7 @@ if __name__ == "__main__":
CVAT_XML_PATH = "annotations.xml" # 你的 annotations.xml 文件
OUTPUT_DIR = "yolo_obb_dataset1" # 输出目录
CLASS_MAPPING = {
"clamp": 0, # 请根据你的实际类别修改
# "other_class": 1,
"clamp": 0,
}
# 执行转换

View File

@ -2,10 +2,10 @@ import os
# ================== 配置参数 ==================
# 图片所在的文件夹路径
image_folder = '/home/hx/yolo/yolo11_obb/yolo_obb_dataset' # 修改为你的图片文件夹路径
image_folder = '/home/hx/yolo/yolo11_obb/image_lianghua' # 修改为你的图片文件夹路径
# 输出的txt文件路径
output_txt = '/home/hx/yolo/yolo11_obb/yolo_obb_dataset/image_list.txt' # 修改为你想保存的路径
output_txt = '/home/hx/yolo/yolo11_obb/image_lianghua/image_list.txt' # 修改为你想保存的路径
# 支持的图片格式
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.webp', '.tiff', '.gif'}