This commit is contained in:
2025-12-21 12:06:28 +01:00
parent c0374b3890
commit 94b83fb018
32 changed files with 281 additions and 212 deletions

View File

View File

@@ -0,0 +1,26 @@
from pathlib import Path
label_dir = Path("./datasets/your_dataset/val/labels")
def valid_line(line):
parts = line.split()
if len(parts) != 5:
return False
cls, x, y, w, h = map(float, parts)
return 0 <= cls and 0 < w <= 1 and 0 < h <= 1
empty = []
for p in label_dir.glob("*.txt"):
if p.stat().st_size == 0:
empty.append(p.name)
print(f"Empty label files: {len(empty)}")
invalid = []
for p in label_dir.glob("*.txt"):
lines = p.read_text().strip().splitlines()
if not any(valid_line(l) for l in lines):
invalid.append(p.name)
print(f"Effectively empty labels: {len(invalid)}")

View File

@@ -0,0 +1,36 @@
import os
# --------------------------
# Configuration
# --------------------------
labels_dir = "datasets/visiope/test/labels"
# --------------------------
# Process each label file
# --------------------------
for filename in os.listdir(labels_dir):
if not filename.endswith(".txt"):
continue
txt_path = os.path.join(labels_dir, filename)
new_lines = []
with open(txt_path, "r") as f:
lines = f.readlines()
for line in lines:
parts = line.strip().split()
if len(parts) < 5:
continue # skip invalid lines
cls = int(parts[0])
cls -= 1 # subtract 1 from class index
cls = max(cls, 0) # ensure no negative indices
new_lines.append(" ".join([str(cls)] + parts[1:]))
# Overwrite file with updated indices
with open(txt_path, "w") as f:
f.write("\n".join(new_lines))
print(f"Updated {filename}")
print("All label files have been adjusted!")

View File

@@ -0,0 +1,33 @@
import os
labels_dir = "../datasets/corners/Outer Chess Corners.v1i.yolov11/valid/labels"
label_to_be_removed = 1
for filename in os.listdir(labels_dir):
if not filename.endswith(".txt"):
continue
txt_path = os.path.join(labels_dir, filename)
new_lines = []
with open(txt_path, "r") as f:
lines = f.readlines()
for line in lines:
parts = line.strip().split()
if len(parts) < 5:
continue
cls = int(parts[0])
if cls == label_to_be_removed:
print(f"{parts} found in {filename}")
continue
new_lines.append(" ".join([str(cls)] + parts[1:]))
# Overwrite file with updated indices
with open(txt_path, "w") as f:
f.write("\n".join(new_lines))
print("All label files have been adjusted!")

View File

@@ -0,0 +1,16 @@
import os
# Paths to the folders
folder_to_check = "./datasets/roboflow/train/labels"
folder_with_files = "./datasets/unified/train/labels"
files_to_check = set(os.listdir(folder_to_check))
for filename in os.listdir(folder_with_files):
file_path = os.path.join(folder_with_files, filename)
if filename in files_to_check and os.path.isfile(file_path):
try:
os.remove(file_path)
print(f"Deleted: {file_path}")
except Exception as e:
print(f"Error deleting {file_path}: {e}")

View File

@@ -0,0 +1,71 @@
import os
import shutil
def copy_images(src, dest):
image_extensions = [".jpg", ".jpeg", ".png", ".bmp", ".gif"]
for filename in os.listdir(src):
if any(filename.lower().endswith(ext) for ext in image_extensions):
src_path = os.path.join(src, filename)
dst_path = os.path.join(dest, filename)
shutil.copy2(src_path, dst_path)
def remap_labels(src, dest):
count = 0
for filename in os.listdir(src):
if filename.endswith(".txt"):
input_path = os.path.join(src, filename)
output_path = os.path.join(dest, filename)
with open(input_path, "r") as f:
lines = f.readlines()
new_lines = []
for line in lines:
parts = line.strip().split()
old_idx = int(parts[0])
new_idx = index_map[old_idx]
new_lines.append(" ".join([str(new_idx)] + parts[1:]))
with open(output_path, "w") as f:
f.write("\n".join(new_lines))
if count%100 == 0:
print(count)
count += 1
print(f"All labels remapped and saved to '{dest}'")
if __name__ == "__main__":
src_dir = "../datasets/pieces/visualizan/"
dest_dir = "../datasets/pieces/unified/"
reference_classes = [
'w_pawn', 'w_knight', 'w_bishop', 'w_rook', 'w_queen', 'w_king',
'b_pawn', 'b_knight', 'b_bishop', 'b_rook', 'b_queen', 'b_king'
]
current_classes = ['b_bishop', 'b_king', 'b_knight', 'b_pawn', 'b_queen', 'b_rook',
'w_bishop', 'w_king', 'w_knight', 'w_pawn', 'w_queen', 'w_rook']
index_map = {current_classes.index(cls): reference_classes.index(cls) for cls in current_classes}
sub_elements = os.listdir(src_dir)
for sub in sub_elements:
src_full_path = os.path.normpath(os.path.join(src_dir, sub))
dest_full_path = os.path.normpath(os.path.join(dest_dir, sub))
if not os.path.isdir(src_full_path): continue
src_image_folder = os.path.normpath(os.path.join(src_full_path, "images"))
src_labels_folder = os.path.normpath(os.path.join(src_full_path, "labels"))
dst_image_folder = os.path.normpath(os.path.join(dest_full_path, "images"))
dst_labels_folder = os.path.normpath(os.path.join(dest_full_path, "labels"))
copy_images(src_image_folder, dst_image_folder)
remap_labels(src_labels_folder, dst_labels_folder)

View File

@@ -0,0 +1,47 @@
import os
if __name__ == "__main__":
trg_dir = "../datasets/pieces/unified/train/labels"
src_dir = "../datasets/pieces/khalid/train/labels"
trg_labels = [
'w_pawn','w_knight','w_bishop','w_rook','w_queen','w_king',
'b_pawn','b_knight','b_bishop','b_rook','b_queen','b_king'
]
src_labels = [
'b_bishop', 'b_king', 'b_knight', 'b_queen', 'b_rook', 'b_pawn',
'w_bishop', 'w_king', 'w_knight', 'w_queen', 'w_rook', 'w_pawn'
]
trg_files = os.listdir(trg_dir)
src_files = os.listdir(src_dir)
for src_file in src_files:
trg_file = os.path.abspath(os.path.join(trg_dir, src_file))
src_file = os.path.abspath(os.path.join(src_dir, src_file))
trg_lines = []
src_lines = []
with open(src_file, "r") as f:
src_lines = f.readlines().copy()
with open(trg_file, "r") as f:
trg_lines = f.readlines().copy()
for i in range(0, len(trg_lines)):
trg_line = trg_lines[i]
src_line = src_lines[i]
trg_label_index = int(trg_line.strip().split(" ")[0])
src_label_index = int(src_line.strip().split(" ")[0])
trg_label_value = trg_labels[trg_label_index]
src_label_value = src_labels[src_label_index]
if trg_label_value != src_label_value :
print(f"Error detected in {trg_file} at line {i}.\n"
f"==> Index {trg_label_index} points to {trg_label_value} instead of {src_label_index}:{src_label_value}")
print("Detection terminated.")