增加部署代码

This commit is contained in:
琉璃月光
2025-09-05 14:29:33 +08:00
parent ad52ab9125
commit 471c718d42
951 changed files with 14072 additions and 264 deletions

12515
yolo11_obb/annotations.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,128 @@
# cvat_xml_to_yolo_obb.py
# 仅在有标注时生成 .txt 文件
import xml.etree.ElementTree as ET
import numpy as np
from pathlib import Path
def rotate_box(x_center, y_center, w, h, angle_deg):
"""
将旋转框转为 4 个角点坐标(未归一化)
"""
angle_rad = np.radians(angle_deg)
# 四个角点相对于中心的偏移
corners = np.array([
[-w/2, -h/2],
[ w/2, -h/2],
[ w/2, h/2],
[-w/2, h/2]
])
# 旋转矩阵(顺时针)
cos_a, sin_a = np.cos(angle_rad), np.sin(angle_rad)
rotation_matrix = np.array([[cos_a, -sin_a],
[sin_a, cos_a]])
# 旋转并平移
rotated_corners = np.dot(corners, rotation_matrix.T) + np.array([x_center, y_center])
return rotated_corners # 返回 (4, 2) 数组
def cvat_xml_to_yolo_obb(cvat_xml_path, output_dir, class_name_to_id=None):
"""
将 CVAT annotations.xml 转为 YOLO-OBB 格式
- 仅在有有效标注时创建 .txt 文件
"""
if class_name_to_id is None:
class_name_to_id = {"clamp": 0} # ✅ 请根据你的实际类别修改
tree = ET.parse(cvat_xml_path)
root = tree.getroot()
# 创建 labels 输出目录
labels_dir = Path(output_dir) / "labels"
labels_dir.mkdir(parents=True, exist_ok=True)
# 统计信息
processed_images = 0
saved_files = 0
skipped_classes = 0
for image_elem in root.findall("image"):
image_name = image_elem.get("name")
img_w = float(image_elem.get("width"))
img_h = float(image_elem.get("height"))
label_file = (labels_dir / Path(image_name).stem).with_suffix(".txt")
boxes = image_elem.findall("box")
valid_annotations = []
for box in boxes:
label = box.get("label")
if label not in class_name_to_id:
print(f"⚠️ 跳过未知类别: {label} (图片: {image_name})")
skipped_classes += 1
continue
class_id = class_name_to_id[label]
xtl = float(box.get("xtl"))
ytl = float(box.get("ytl"))
xbr = float(box.get("xbr"))
ybr = float(box.get("ybr"))
# 计算中心点和宽高
x_center = (xtl + xbr) / 2
y_center = (ytl + ybr) / 2
w = xbr - xtl
h = ybr - ytl
# 获取旋转角度
angle = float(box.get("rotation", 0.0))
# 计算 4 个角点
corners = rotate_box(x_center, y_center, w, h, angle)
# 归一化到 [0,1]
corners[:, 0] /= img_w
corners[:, 1] /= img_h
# 展平并生成行
points = corners.flatten()
line = str(class_id) + " " + " ".join(f"{coord:.6f}" for coord in points)
valid_annotations.append(line)
# ✅ 只有存在有效标注时才写入文件
if valid_annotations:
with open(label_file, 'w') as f:
f.write("\n".join(valid_annotations) + "\n")
saved_files += 1
print(f"✅ 已生成: {label_file}")
# else: # 无标注,不创建文件
# print(f"🟡 无标注,跳过: {image_name}")
processed_images += 1
print(f"\n🎉 转换完成!")
print(f"📊 处理图片: {processed_images}")
print(f"✅ 生成标签: {saved_files}")
if skipped_classes:
print(f"⚠️ 跳过类别: {skipped_classes} 个标注")
# ==================== 使用示例 ====================
if __name__ == "__main__":
# ✅ 请修改以下路径和类别
CVAT_XML_PATH = "annotations.xml" # 你的 annotations.xml 文件
OUTPUT_DIR = "yolo_obb_dataset" # 输出目录
CLASS_MAPPING = {
"clamp": 0, # 请根据你的实际类别修改
# "other_class": 1,
}
# 执行转换
cvat_xml_to_yolo_obb(
cvat_xml_path=CVAT_XML_PATH,
output_dir=OUTPUT_DIR,
class_name_to_id=CLASS_MAPPING
)

34
yolo11_obb/txt_1.py Normal file
View File

@ -0,0 +1,34 @@
import os
# ================== 配置参数 ==================
# 图片所在的文件夹路径
image_folder = '/home/hx/yolo/yolo11_obb/yolo_obb_dataset' # 修改为你的图片文件夹路径
# 输出的txt文件路径
output_txt = '/home/hx/yolo/yolo11_obb/yolo_obb_dataset/image_list.txt' # 修改为你想保存的路径
# 支持的图片格式
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.webp', '.tiff', '.gif'}
# 是否保存绝对路径True还是仅文件名False
save_full_path = True # 设为 False 只保存文件名
# =============================================
# 获取所有图片文件
image_files = []
for root, dirs, files in os.walk(image_folder):
for file in files:
ext = os.path.splitext(file.lower())[1]
if ext in image_extensions:
file_path = os.path.join(root, file)
if save_full_path:
image_files.append(os.path.abspath(file_path))
else:
image_files.append(file)
# 写入txt文件
with open(output_txt, 'w', encoding='utf-8') as f:
for img_path in image_files:
f.write(img_path + '\n')
print(f"✅ 已保存 {len(image_files)} 张图片的路径到:\n {output_txt}")

Binary file not shown.

After

Width:  |  Height:  |  Size: 444 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 468 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 470 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 599 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 611 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 564 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 600 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 622 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 627 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 572 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 611 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 482 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 509 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 516 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 527 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 552 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 541 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 598 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 514 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 519 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 521 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 513 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 498 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 473 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 505 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 507 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 501 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 493 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 497 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 484 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 496 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 500 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 485 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 518 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 529 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 543 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 583 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 545 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 593 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 627 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 577 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 625 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 508 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 515 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 524 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 530 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 536 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 466 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 503 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 514 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 525 KiB

View File

@ -0,0 +1,60 @@
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144339_274695.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144359_275250.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144400_275295.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144405_275430.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144337_274620.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144420_275865.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144349_274965.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144400_275280.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144421_275880.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144416_275730.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144344_274815.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144422_275910.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144409_275550.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144409_275535.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144423_275940.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144350_274995.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144419_275835.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144338_274665.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144404_275385.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144421_275895.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144340_274725.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144423_275955.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144358_275235.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144401_275325.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144352_275055.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144404_275400.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144351_275025.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144413_275655.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144345_274860.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144403_275370.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144422_275925.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144348_274950.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144349_274980.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144415_275715.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144406_275445.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144336_274590.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144402_275355.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144417_275760.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144407_275475.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144355_275145.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144420_275850.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144401_275310.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144405_275415.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144402_275340.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144412_275625.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144408_275520.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144336_274605.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144356_275175.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144406_275460.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144345_274845.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144343_274785.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144410_275580.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144359_275265.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144414_275685.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144354_275115.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144341_274755.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144353_275085.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144419_275820.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144412_275610.jpg
/home/hx/yolo/yolo11_obb/yolo_obb_dataset/frame_20250805_144346_274890.jpg