zhaoyinghan hace 1 mes
padre
commit
9616289f94

+ 102 - 0
d_data_spliter.py

@@ -0,0 +1,102 @@
+"""
+该脚本将源目录下同名的 .json 和 .diff 文件配对,
+并按如下结构将其整理至目标目录:
+
+输出结构如下:
+
+output_dir/
+├── images/
+│   ├── train/
+│   └── val/
+└── labels/
+    ├── train/
+    └── val/
+
+其中:
+- images/train 和 val 存放的是 `.diff` 文件
+- labels/train 和 val 存放的是 `.json` 文件
+"""
+
+import os
+import shutil
+import random
+from pathlib import Path
+
+def organize_data(
+    src_dir,
+    dest_dir,
+    image_extensions=['.tiff'],
+    label_extensions=['.json'],
+    val_ratio=0.2
+):
+    src_dir = Path(src_dir)
+    dest_dir = Path(dest_dir)
+
+    image_dir = dest_dir / 'images'
+    label_dir = dest_dir / 'labels'
+
+    # 创建文件夹结构
+    for split in ['train', 'val']:
+        (image_dir / split).mkdir(parents=True, exist_ok=True)
+        (label_dir / split).mkdir(parents=True, exist_ok=True)
+
+    # 获取所有文件
+    files = list(src_dir.glob('*'))
+    name_to_files = {}
+
+    # 分组:同名文件归为一组
+    for f in files:
+        stem = f.stem
+        name_to_files.setdefault(stem, []).append(f)
+
+    # 筛选出同时包含 label 和 image 的样本
+    paired_samples = []
+    for name, file_group in name_to_files.items():
+        image_file = next((f for f in file_group if f.suffix in image_extensions), None)
+        label_file = next((f for f in file_group if f.suffix in label_extensions), None)
+        if image_file and label_file:
+            paired_samples.append((image_file, label_file))
+        else:
+            print(f"⚠️ Skipping unpaired files for: {name}")
+
+    # 打乱并划分
+    random.shuffle(paired_samples)
+    split_idx = int(len(paired_samples) * (1 - val_ratio))
+    train_samples = paired_samples[:split_idx]
+    val_samples = paired_samples[split_idx:]
+
+    # 拷贝函数
+    def copy_samples(samples, split):
+        for img, lbl in samples:
+            shutil.copy(img, image_dir / split / img.name)
+            shutil.copy(lbl, label_dir / split / lbl.name)
+
+    # 执行拷贝
+    copy_samples(train_samples, 'train')
+    copy_samples(val_samples, 'val')
+
+    print(f"\n✅ Done! Processed {len(paired_samples)} pairs.")
+    print(f"Train: {len(train_samples)}, Val: {len(val_samples)}")
+
+if __name__ == "__main__":
+    # 输入输出目录(可修改)
+    source_dir = r"/home/zhaoyinghan/py_ws/data/circle/huayan"
+
+    parent_dir = os.path.dirname(source_dir)
+    output_dir = os.path.join(parent_dir, "a_dataset")
+
+    # 后缀名列表,方便以后扩展其他格式
+    image_exts = ['.tiff','.jpg','.png']
+    label_exts = ['.json']
+
+    organize_data(source_dir, output_dir, image_exts, label_exts)
+
+
+
+
+
+
+
+
+
+

+ 116 - 0
models/line_detect/cs_readv.py

@@ -0,0 +1,116 @@
+import os
+import csv
+import json
+import math
+import ast
+import shutil
+
+def ellipse_to_box_points(cx, cy, rx, ry, theta):
+    """
+    ½«ÍÖÔ²²ÎÊýת»»Îª¾ØÐΣ¨ÉÏÏÂ×óÓÒ4µã£©
+    ½üËÆ·½·¨£ºÓÃÐýת¾ØÐεÄ4¸ö¶¥µã±íʾÍÖÔ²±ß½ç
+    """
+    # Ëĸöµã£¨×óÓÒÖе㣬ÉÏÏÂÖе㣩
+    points = [
+        [cx - rx, cy],  # ×ó
+        [cx + rx, cy],  # ÓÒ
+        [cx, cy - ry],  # ÉÏ
+        [cx, cy + ry],  # ÏÂ
+    ]
+
+    # ÐýתÕâЩµã
+    cos_t = math.cos(theta)
+    sin_t = math.sin(theta)
+    rotated_points = []
+    for x, y in points:
+        x_new = (x - cx) * cos_t - (y - cy) * sin_t + cx
+        y_new = (x - cx) * sin_t + (y - cy) * cos_t + cy
+        rotated_points.append([x_new, y_new])
+
+    return rotated_points
+
+
+def csv_to_labelme_json(csv_path, img_folder, output_folder, image_exts=(".png", ".tiff")):
+    os.makedirs(output_folder, exist_ok=True)
+
+    with open(csv_path, newline='', encoding='utf-8') as csvfile:
+        reader = csv.DictReader(csvfile)
+        for row in reader:
+            filename = row["filename"]
+            region_shape_attributes = row["region_shape_attributes"]
+
+            # ת³É×ֵ䣨CSV ÀïÊÇ×Ö·û´®£©
+            try:
+                attr = json.loads(region_shape_attributes)
+            except json.JSONDecodeError:
+                attr = ast.literal_eval(region_shape_attributes)
+
+            if attr.get("name") != "ellipse":
+                continue
+
+            cx, cy = float(attr["cx"]), float(attr["cy"])
+            rx, ry = float(attr["rx"]), float(attr["ry"])
+            theta = float(attr["theta"])
+
+            # ÍÖÔ²Ëĸöµã
+            points = ellipse_to_box_points(cx, cy, rx, ry, theta)
+
+            # »ñÈ¡xmin, ymin, xmax, ymax
+            xs = [p[0] for p in points]
+            ys = [p[1] for p in points]
+            xmin, ymin, xmax, ymax = int(min(xs)), int(min(ys)), int(max(xs)), int(max(ys))
+
+            # Éú³É Labelme ¸ñʽ
+            data = {
+                "version": "5.0.1",
+                "flags": {},
+                "shapes": [
+                    {
+                        "label": "circle",
+                        "points": points,
+                        "shape_type": "polygon",
+                        "flags": {},
+                        "xmin": xmin,
+                        "ymin": ymin,
+                        "xmax": xmax,
+                        "ymax": ymax,
+                    }
+                ],
+                "imagePath": filename.replace(".png", ".jpg").replace(".tiff", ".jpg"),
+                "imageHeight": 2000,  # Èç¹ûÄãÖªµÀʵ¼ÊͼÏñ³ß´ç¿ÉÒÔ¸Ä
+                "imageWidth": 2000
+            }
+
+            # дÈë JSON Îļþ
+            json_filename = os.path.splitext(filename)[0] + ".json"
+            json_path = os.path.join(output_folder, json_filename)
+            with open(json_path, "w", encoding="utf-8") as f:
+                json.dump(data, f, indent=4, ensure_ascii=False)
+
+            # ¿½±´Í¼Æ¬Îļþ
+            src_img_path = None
+            for ext in image_exts:
+                test_path = os.path.join(img_folder, filename.replace(".png", ext).replace(".tiff", ext))
+                if os.path.exists(test_path):
+                    src_img_path = test_path
+                    break
+
+            if src_img_path:
+                dst_img_path = os.path.join(output_folder, os.path.basename(src_img_path))
+                shutil.copy(src_img_path, dst_img_path)
+            else:
+                print(f"?? ÕÒ²»µ½Í¼Æ¬: {filename}")
+
+    print(f"? ת»»Íê³É£¬Êä³ö·¾¶£º{output_folder}")
+
+
+if __name__ == "__main__":
+    # ÊäÈëÎļþ¼Ð
+    csv_folder = "/data/share/zyh/数据集汇总/circle/huayan_circle/csv"
+    img_folder = "/data/share/zyh/数据集汇总/circle/huayan_circle/彩色图"
+    output_folder = "/home/zhaoyinghan/py_ws/data/circle/huayan"
+
+    for csv_file in os.listdir(csv_folder):
+        if csv_file.endswith(".csv"):
+            csv_path = os.path.join(csv_folder, csv_file)
+            csv_to_labelme_json(csv_path, img_folder, output_folder)

+ 2 - 2
models/line_detect/line_detect.py

@@ -72,8 +72,8 @@ class LineDetect(BaseDetectionNet):
             box_score_thresh=0.05,
             box_nms_thresh=0.5,
             box_detections_per_img=200,
-            box_fg_iou_thresh=0.5,
-            box_bg_iou_thresh=0.5,
+            box_fg_iou_thresh=0.7,
+            box_bg_iou_thresh=0.3,
             box_batch_size_per_image=512,
             box_positive_fraction=0.25,
             bbox_reg_weights=None,

+ 1 - 1
models/line_detect/train.yaml

@@ -7,7 +7,7 @@ io:
 #  datadir: /data/share/rlq/datasets/250718caisegangban
 
 #  datadir: /data/share/rlq/datasets/singepoint_Dataset0709_2
-  datadir: /data/share/rlq/datasets/guanban_circle
+  datadir: /home/zhaoyinghan/py_ws/data/circle/a_dataset
 #  datadir: \\192.168.50.222/share/rlq/datasets/singepoint_Dataset0709_2
 #  datadir: \\192.168.50.222/share/rlq/datasets/250718caisegangban
   data_type: rgb