/
Nemo_499
/
DecodingX-rayImages
Обзор
Документация
Войти
/
Nemo_499
/
DecodingX-rayImages
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Convert.py
200 строк
6 KB
Денис
Добавлены новых конфигов
04 май 2026, 22:02
04 май 2026, 22:02
9825d24
Код
Авторство
О чём код?
from pathlib import Path def polygon_to_bbox(coords): xs = coords[0::2] ys = coords[1::2] return min(xs), min(ys), max(xs), max(ys) def polygon_to_bbox2(coords, h = 640, w = 640): x_c, y_c, x_w, y_h = coords[0], coords[1], coords[2], coords[3] x1 = x_c * w - x_w * w // 2 x2 = x_c * w + x_w * w // 2 y1 = y_c * h - y_h * h // 2 y2 = y_c * h + y_h * h // 2 x1_norm = x1 / w y1_norm = y1 / h x2_norm = x2 / w y2_norm = y2 / h return x1_norm, y1_norm, x2_norm, y2_norm def overwrite_polygon_labels(labels_dir): labels_dir = Path(labels_dir) txt_files = list(labels_dir.glob("*.txt")) print(f"Найдено файлов: {len(txt_files)}") for file in txt_files: new_lines = [] with open(file, "r") as f: for line_num, line in enumerate(f, 1): line = line.strip() if not line: continue parts = list(map(float, line.split())) if len(parts) < 7: print(f"[SKIP] {file.name}:{line_num} — некорректная строка") continue class_id = int(parts[0]) + 1 coords = parts[1:] try: x_min, y_min, x_max, y_max = polygon_to_bbox(coords) except Exception as e: print(f"[ERROR] {file.name}:{line_num} — {e}") continue new_lines.append( f"{class_id} {x_min:.6f} {y_min:.6f} {x_max:.6f} {y_max:.6f}" ) #ПЕРЕЗАПИСЬ ФАЙЛА with open(file, "w") as f: f.write("\n".join(new_lines)) print("Все файлы успешно перезаписаны") def overwrite_polygon_labels2(labels_dir): labels_dir = Path(labels_dir) txt_files = list(labels_dir.glob("*.txt")) print(f"Найдено файлов: {len(txt_files)}") for file in txt_files: new_lines = [] with open(file, "r") as f: for line_num, line in enumerate(f, 1): line = line.strip() if not line: continue parts = list(map(float, line.split())) class_id = int(parts[0]) + 1 coords = parts[1:] try: x_min, y_min, x_max, y_max = polygon_to_bbox2(coords) except Exception as e: print(f"[ERROR] {file.name}:{line_num} — {e}") continue new_lines.append( f"{class_id} {x_min:.6f} {y_min:.6f} {x_max:.6f} {y_max:.6f}" ) #ПЕРЕЗАПИСЬ ФАЙЛА with open(file, "w") as f: f.write("\n".join(new_lines)) print("Все файлы успешно перезаписаны") def overwrite_polygon_labels3(labels_dir): labels_dir = Path(labels_dir) txt_files = list(labels_dir.glob("*.txt")) print(f"Найдено файлов: {len(txt_files)}") for file in txt_files: new_lines = [] with open(file, "r") as f: for line_num, line in enumerate(f, 1): line = line.strip() if not line: continue parts = list(map(float, line.split())) class_id = int(parts[0]) coords = parts[1:] try: x_min, y_min, x_max, y_max = polygon_to_bbox(coords) xc_norm = (x_min + x_max) / 2 yc_norm = (y_min + y_max) / 2 h_norm = y_max - y_min w_norm = x_max - x_min except Exception as e: print(f"[ERROR] {file.name}:{line_num} — {e}") continue new_lines.append( f"{class_id} {xc_norm:.6f} {yc_norm:.6f} {w_norm:.6f} {h_norm:.6f}" ) #ПЕРЕЗАПИСЬ ФАЙЛА with open(file, "w") as f: f.write("\n".join(new_lines)) print("Все файлы успешно перезаписаны") def overwrite_polygon_labels4(labels_dir, labels_dir2): labels_dir = Path(labels_dir) txt_files = list(labels_dir.glob("*.txt")) print(f"Найдено файлов: {len(txt_files)}") for file in txt_files: class_id_mas = [] new_lines = [] filename = file.name with open(file, "r") as f: for line_num, line in enumerate(f, 1): line = line.strip() if not line: continue parts = list(map(float, line.split())) class_id = int(parts[0]) class_id_mas.append(class_id) target_file = Path(labels_dir2 + "/" + filename) if target_file.exists(): with open(target_file, "r") as f: for line_num, line in enumerate(f): parts = list(map(int, line.split())) new_lines.append( f"{parts[0]} {parts[1]} {parts[2]} {parts[3]} {class_id_mas[line_num]}" ) #ПЕРЕЗАПИСЬ ФАЙЛА with open(target_file, "w") as f: f.write("\n".join(new_lines)) else: continue print("Все файлы успешно перезаписаны") #overwrite_polygon_labels("D:/Dataset/train/labels") #overwrite_polygon_labels("D:/Dataset/val/labels") #overwrite_polygon_labels2("D:/The Welding Defect Dataset - v2/train/labels") #overwrite_polygon_labels2("D:/The Welding Defect Dataset - v2/val/labels") #overwrite_polygon_labels3("D:/data_kaggle/train/labels") #overwrite_polygon_labels3("D:/data_kaggle/val/labels") #overwrite_polygon_labels4("D:/data_kaggle/train/labels","D:/DecodingX-rayImages2/DecodingX-rayImages/Dataset/Train") #overwrite_polygon_labels4("D:/data_kaggle/val/labels","D:/DecodingX-rayImages2/DecodingX-rayImages/Dataset/Test") #overwrite_polygon_labels3("D:/Dataset/train_data2/train/labels") #overwrite_polygon_labels3("D:/Dataset/train_data2/val/labels") overwrite_polygon_labels4("D:/Dataset/train_data2/train/labels","D:/DecodingX-rayImages2/DecodingX-rayImages2/Dataset/Train") overwrite_polygon_labels4("D:/Dataset/train_data2/val/labels","D:/DecodingX-rayImages2/DecodingX-rayImages2/Dataset/Test")