Mayo commited on
feat: simplify manga109 to YOLO conversion by removing book selection and updating class mapping
Browse files- scripts/manga109_yolo.py +8 -15
scripts/manga109_yolo.py
CHANGED
|
@@ -33,13 +33,13 @@ def process_annotation(ann, class_id, img_width, img_height, out_file):
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
-
def manga109_to_yolo(manga109_root_dir, output_dir
|
| 37 |
"""Convert Manga109 annotations to YOLO format with 80/20 train/val split."""
|
| 38 |
# Initialize parser
|
| 39 |
parser = manga109api.Parser(root_dir=manga109_root_dir)
|
| 40 |
|
| 41 |
# Define class mapping
|
| 42 |
-
class_map = {"
|
| 43 |
|
| 44 |
# Create directory structure
|
| 45 |
os.makedirs(os.path.join(output_dir, "images", "train"), exist_ok=True)
|
|
@@ -49,14 +49,10 @@ def manga109_to_yolo(manga109_root_dir, output_dir, books=None):
|
|
| 49 |
|
| 50 |
# Write class names file
|
| 51 |
with open(os.path.join(output_dir, "classes.txt"), "w") as f:
|
| 52 |
-
for class_name in ["
|
| 53 |
f.write(f"{class_name}\n")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
if books:
|
| 57 |
-
book_list = books
|
| 58 |
-
else:
|
| 59 |
-
book_list = parser.books
|
| 60 |
|
| 61 |
# Shuffle books to ensure random distribution
|
| 62 |
random.shuffle(book_list)
|
|
@@ -76,14 +72,14 @@ def manga109_to_yolo(manga109_root_dir, output_dir, books=None):
|
|
| 76 |
process_books(parser, val_books, output_dir, class_map, "val")
|
| 77 |
|
| 78 |
# Create YAML configuration file for YOLO
|
| 79 |
-
yaml_path = os.path.join(output_dir, "
|
| 80 |
with open(yaml_path, "w") as f:
|
| 81 |
f.write(f"path: {os.path.abspath(output_dir)}\n")
|
| 82 |
f.write("train: images/train\n")
|
| 83 |
f.write("val: images/val\n\n")
|
| 84 |
|
| 85 |
f.write("names:\n")
|
| 86 |
-
for i, name in enumerate(["
|
| 87 |
f.write(f" {i}: {name}\n")
|
| 88 |
|
| 89 |
|
|
@@ -120,7 +116,7 @@ def process_books(parser, book_list, output_dir, class_map, split_type):
|
|
| 120 |
|
| 121 |
with open(label_path, "w") as f:
|
| 122 |
# Process each annotation type
|
| 123 |
-
for ann_type in ["
|
| 124 |
if ann_type in page:
|
| 125 |
# Handle both single annotation and list of annotations
|
| 126 |
annotations = page[ann_type]
|
|
@@ -145,9 +141,6 @@ def main():
|
|
| 145 |
required=True,
|
| 146 |
help="Output directory for YOLO-formatted dataset",
|
| 147 |
)
|
| 148 |
-
parser.add_argument(
|
| 149 |
-
"--books", nargs="*", help="Specific books to convert (default: all books)"
|
| 150 |
-
)
|
| 151 |
parser.add_argument(
|
| 152 |
"--seed", type=int, default=42, help="Random seed for dataset splitting"
|
| 153 |
)
|
|
@@ -157,7 +150,7 @@ def main():
|
|
| 157 |
# Set random seed for reproducibility
|
| 158 |
random.seed(args.seed)
|
| 159 |
|
| 160 |
-
manga109_to_yolo(args.manga109_dir, args.output_dir
|
| 161 |
|
| 162 |
print(f"Conversion complete! Output saved to {args.output_dir}")
|
| 163 |
print(f"Dataset split: 80% training, 20% validation")
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
|
| 36 |
+
def manga109_to_yolo(manga109_root_dir, output_dir):
|
| 37 |
"""Convert Manga109 annotations to YOLO format with 80/20 train/val split."""
|
| 38 |
# Initialize parser
|
| 39 |
parser = manga109api.Parser(root_dir=manga109_root_dir)
|
| 40 |
|
| 41 |
# Define class mapping
|
| 42 |
+
class_map = {"frame": 0, "text": 1}
|
| 43 |
|
| 44 |
# Create directory structure
|
| 45 |
os.makedirs(os.path.join(output_dir, "images", "train"), exist_ok=True)
|
|
|
|
| 49 |
|
| 50 |
# Write class names file
|
| 51 |
with open(os.path.join(output_dir, "classes.txt"), "w") as f:
|
| 52 |
+
for class_name in ["frame", "text"]:
|
| 53 |
f.write(f"{class_name}\n")
|
| 54 |
|
| 55 |
+
book_list = parser.books
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Shuffle books to ensure random distribution
|
| 58 |
random.shuffle(book_list)
|
|
|
|
| 72 |
process_books(parser, val_books, output_dir, class_map, "val")
|
| 73 |
|
| 74 |
# Create YAML configuration file for YOLO
|
| 75 |
+
yaml_path = os.path.join(output_dir, "data.yaml")
|
| 76 |
with open(yaml_path, "w") as f:
|
| 77 |
f.write(f"path: {os.path.abspath(output_dir)}\n")
|
| 78 |
f.write("train: images/train\n")
|
| 79 |
f.write("val: images/val\n\n")
|
| 80 |
|
| 81 |
f.write("names:\n")
|
| 82 |
+
for i, name in enumerate(["frame", "text"]):
|
| 83 |
f.write(f" {i}: {name}\n")
|
| 84 |
|
| 85 |
|
|
|
|
| 116 |
|
| 117 |
with open(label_path, "w") as f:
|
| 118 |
# Process each annotation type
|
| 119 |
+
for ann_type in ["frame", "text"]:
|
| 120 |
if ann_type in page:
|
| 121 |
# Handle both single annotation and list of annotations
|
| 122 |
annotations = page[ann_type]
|
|
|
|
| 141 |
required=True,
|
| 142 |
help="Output directory for YOLO-formatted dataset",
|
| 143 |
)
|
|
|
|
|
|
|
|
|
|
| 144 |
parser.add_argument(
|
| 145 |
"--seed", type=int, default=42, help="Random seed for dataset splitting"
|
| 146 |
)
|
|
|
|
| 150 |
# Set random seed for reproducibility
|
| 151 |
random.seed(args.seed)
|
| 152 |
|
| 153 |
+
manga109_to_yolo(args.manga109_dir, args.output_dir)
|
| 154 |
|
| 155 |
print(f"Conversion complete! Output saved to {args.output_dir}")
|
| 156 |
print(f"Dataset split: 80% training, 20% validation")
|