Spaces:
Running
Running
inf all
Browse files
comic_panel_extractor/create_dataset.py
CHANGED
|
@@ -3,7 +3,8 @@ import shutil
|
|
| 3 |
import random
|
| 4 |
from pathlib import Path
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
-
from tqdm import tqdm
|
|
|
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
SOURCE_PATHS = os.getenv('SOURCE_PATH')
|
|
@@ -14,17 +15,12 @@ if not SOURCE_PATHS:
|
|
| 14 |
# Split by comma and strip whitespace
|
| 15 |
source_paths = [Path(p.strip()) for p in SOURCE_PATHS.split(',')]
|
| 16 |
|
| 17 |
-
images_dir = Path('images')
|
| 18 |
-
dataset_dir = Path('dataset')
|
| 19 |
|
| 20 |
image_exts = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp'}
|
| 21 |
label_exts = {'.txt'}
|
| 22 |
|
| 23 |
-
# Clear images_dir
|
| 24 |
-
if images_dir.exists():
|
| 25 |
-
shutil.rmtree(images_dir)
|
| 26 |
-
images_dir.mkdir(parents=True)
|
| 27 |
-
|
| 28 |
# Copy images from all source paths with tqdm progress
|
| 29 |
for source_path in source_paths:
|
| 30 |
if not source_path.exists():
|
|
@@ -76,7 +72,7 @@ splits = {
|
|
| 76 |
'test': all_images[val_end:]
|
| 77 |
}
|
| 78 |
|
| 79 |
-
label_src_dir = Path('image_labels')
|
| 80 |
|
| 81 |
# Move/copy images and labels to their split folders with tqdm
|
| 82 |
for split, files in splits.items():
|
|
|
|
| 3 |
import random
|
| 4 |
from pathlib import Path
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from tqdm import tqdm
|
| 7 |
+
from .config import Config
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
SOURCE_PATHS = os.getenv('SOURCE_PATH')
|
|
|
|
| 15 |
# Split by comma and strip whitespace
|
| 16 |
source_paths = [Path(p.strip()) for p in SOURCE_PATHS.split(',')]
|
| 17 |
|
| 18 |
+
images_dir = Path(f'{Config.current_path}/images')
|
| 19 |
+
dataset_dir = Path(f'{Config.current_path}/dataset')
|
| 20 |
|
| 21 |
image_exts = {'.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp'}
|
| 22 |
label_exts = {'.txt'}
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Copy images from all source paths with tqdm progress
|
| 25 |
for source_path in source_paths:
|
| 26 |
if not source_path.exists():
|
|
|
|
| 72 |
'test': all_images[val_end:]
|
| 73 |
}
|
| 74 |
|
| 75 |
+
label_src_dir = Path(f'{Config.current_path}/image_labels')
|
| 76 |
|
| 77 |
# Move/copy images and labels to their split folders with tqdm
|
| 78 |
for split, files in splits.items():
|
comic_panel_extractor/inference.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# inference.py
|
| 2 |
-
from yolo_manager import YOLOManager
|
| 3 |
-
from utils import get_abs_path, get_image_paths
|
| 4 |
import os
|
| 5 |
from .config import Config
|
| 6 |
|
|
@@ -50,5 +50,24 @@ def main():
|
|
| 50 |
|
| 51 |
run_inference(weights_path, images_dirs, './temp_dir')
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
|
|
|
|
| 1 |
# inference.py
|
| 2 |
+
from .yolo_manager import YOLOManager
|
| 3 |
+
from .utils import get_abs_path, get_image_paths
|
| 4 |
import os
|
| 5 |
from .config import Config
|
| 6 |
|
|
|
|
| 50 |
|
| 51 |
run_inference(weights_path, images_dirs, './temp_dir')
|
| 52 |
|
| 53 |
+
def annotate_all_image():
|
| 54 |
+
with YOLOManager() as yolo_manager:
|
| 55 |
+
weights_path = Config.yolo_trained_model_path
|
| 56 |
+
yolo_manager.load_model(weights_path)
|
| 57 |
+
IMAGE_ROOT = os.path.join(Config.current_path, "dataset/images")
|
| 58 |
+
IMAGE_LABEL_ROOT = os.path.join(Config.current_path, "image_labels")
|
| 59 |
+
for root, _, files in os.walk(IMAGE_ROOT):
|
| 60 |
+
for file in sorted(files):
|
| 61 |
+
if file.lower().endswith((".jpg", ".jpeg", ".png")):
|
| 62 |
+
name, ext = os.path.splitext(file)
|
| 63 |
+
save_txt_path = os.path.join(IMAGE_LABEL_ROOT, f'{name}.txt') # YOLO label txt
|
| 64 |
+
if not os.path.exists(save_txt_path):
|
| 65 |
+
image_path = os.path.join(root, file)
|
| 66 |
+
yolo_manager.annotate_images(
|
| 67 |
+
image_paths=[image_path],
|
| 68 |
+
output_dir=IMAGE_LABEL_ROOT,
|
| 69 |
+
save_image=False
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
annotate_all_image()
|