Spaces:
Running
Running
refine
Browse files
comic_panel_extractor/annorator_server.py
CHANGED
|
@@ -75,7 +75,7 @@ def load_yolo_annotations(image_path: str, label_path: str, detect: bool = False
|
|
| 75 |
if detect and not os.path.exists(label_path):
|
| 76 |
from .yolo_manager import YOLOManager
|
| 77 |
with YOLOManager() as yolo_manager:
|
| 78 |
-
weights_path =
|
| 79 |
yolo_manager.load_model(weights_path)
|
| 80 |
_, label_path = yolo_manager.annotate_images(
|
| 81 |
image_paths=[image_path],
|
|
|
|
| 75 |
if detect and not os.path.exists(label_path):
|
| 76 |
from .yolo_manager import YOLOManager
|
| 77 |
with YOLOManager() as yolo_manager:
|
| 78 |
+
weights_path = Config.yolo_trained_model_path
|
| 79 |
yolo_manager.load_model(weights_path)
|
| 80 |
_, label_path = yolo_manager.annotate_images(
|
| 81 |
image_paths=[image_path],
|
comic_panel_extractor/config.py
CHANGED
|
@@ -10,8 +10,10 @@ class Config:
|
|
| 10 |
org_input_path: str = ""
|
| 11 |
input_path: str = ""
|
| 12 |
current_path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
black_overlay_input_path: str = ""
|
| 16 |
output_folder: str = "temp_dir"
|
| 17 |
distance_threshold: int = 70
|
|
|
|
| 10 |
org_input_path: str = ""
|
| 11 |
input_path: str = ""
|
| 12 |
current_path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
|
| 13 |
+
YOLO_BASE_MODEL_NAME = os.getenv('YOLO_BASE_MODEL_NAME', 'yolo11s-seg')
|
| 14 |
+
yolo_base_model_path: str = f'{current_path}/{YOLO_BASE_MODEL_NAME}.pt'
|
| 15 |
+
YOLO_MODEL_NAME = f'{os.getenv('YOLO_MODEL_NAME', 'comic_panel')}_{YOLO_BASE_MODEL_NAME}'
|
| 16 |
+
yolo_trained_model_path: str = f'{current_path}/{YOLO_MODEL_NAME}.pt'
|
| 17 |
black_overlay_input_path: str = ""
|
| 18 |
output_folder: str = "temp_dir"
|
| 19 |
distance_threshold: int = 70
|
comic_panel_extractor/inference.py
CHANGED
|
@@ -41,7 +41,7 @@ def run_inference(weights_path: str, images_dirs, output_dir: str = 'temp_dir')
|
|
| 41 |
|
| 42 |
def main():
|
| 43 |
"""Main inference function."""
|
| 44 |
-
weights_path =
|
| 45 |
images_dirs = [
|
| 46 |
'./dataset/images/train',
|
| 47 |
'./dataset/images/val',
|
|
|
|
| 41 |
|
| 42 |
def main():
|
| 43 |
"""Main inference function."""
|
| 44 |
+
weights_path = Config.yolo_trained_model_path
|
| 45 |
images_dirs = [
|
| 46 |
'./dataset/images/train',
|
| 47 |
'./dataset/images/val',
|
comic_panel_extractor/llm_panel_extractor.py
CHANGED
|
@@ -16,17 +16,16 @@ class LLMPanelExtractor:
|
|
| 16 |
self.config = config or Config()
|
| 17 |
|
| 18 |
# Check if YOLO model exists; if not, download it to the specified path
|
| 19 |
-
|
| 20 |
-
if not os.path.exists(yolo_model_path):
|
| 21 |
url = "https://huggingface.co/mosesb/best-comic-panel-detection/resolve/main/best.pt"
|
| 22 |
-
print(f"Downloading YOLO model to {
|
| 23 |
response = requests.get(url)
|
| 24 |
response.raise_for_status() # Raise an error if the download fails
|
| 25 |
-
with open(
|
| 26 |
f.write(response.content)
|
| 27 |
print("YOLO model downloaded successfully.")
|
| 28 |
|
| 29 |
-
self.yolo_model = YOLO(
|
| 30 |
os.makedirs(self.config.output_folder, exist_ok=True)
|
| 31 |
|
| 32 |
def extract_bounding_boxes(self, detection_result_boxes):
|
|
|
|
| 16 |
self.config = config or Config()
|
| 17 |
|
| 18 |
# Check if YOLO model exists; if not, download it to the specified path
|
| 19 |
+
if not os.path.exists(self.config.yolo_base_model_path):
|
|
|
|
| 20 |
url = "https://huggingface.co/mosesb/best-comic-panel-detection/resolve/main/best.pt"
|
| 21 |
+
print(f"Downloading YOLO model to {self.config.yolo_base_model_path}...")
|
| 22 |
response = requests.get(url)
|
| 23 |
response.raise_for_status() # Raise an error if the download fails
|
| 24 |
+
with open(self.config.yolo_base_model_path, "wb") as f:
|
| 25 |
f.write(response.content)
|
| 26 |
print("YOLO model downloaded successfully.")
|
| 27 |
|
| 28 |
+
self.yolo_model = YOLO(self.config.yolo_base_model_path)
|
| 29 |
os.makedirs(self.config.output_folder, exist_ok=True)
|
| 30 |
|
| 31 |
def extract_bounding_boxes(self, detection_result_boxes):
|
comic_panel_extractor/train.py
CHANGED
|
@@ -185,7 +185,7 @@ def main():
|
|
| 185 |
|
| 186 |
# Backup best weights
|
| 187 |
weights_path = yolo_manager.get_best_weights_path()
|
| 188 |
-
backup_path =
|
| 189 |
backup_file(weights_path, backup_path)
|
| 190 |
|
| 191 |
print("π Training completed successfully!")
|
|
|
|
| 185 |
|
| 186 |
# Backup best weights
|
| 187 |
weights_path = yolo_manager.get_best_weights_path()
|
| 188 |
+
backup_path = Config.yolo_trained_model_path
|
| 189 |
backup_file(weights_path, backup_path)
|
| 190 |
|
| 191 |
print("π Training completed successfully!")
|
comic_panel_extractor/yolo_manager.py
CHANGED
|
@@ -54,6 +54,8 @@ from ultralytics import YOLO
|
|
| 54 |
from typing import List, Optional, Dict, Any
|
| 55 |
from .utils import get_abs_path, clean_directory
|
| 56 |
from .config import Config
|
|
|
|
|
|
|
| 57 |
|
| 58 |
class YOLOManager:
|
| 59 |
"""Manages YOLO model training and inference operations."""
|
|
@@ -68,8 +70,8 @@ class YOLOManager:
|
|
| 68 |
print(f"π¦ Loading model from: {weights_path}")
|
| 69 |
self.model = YOLO(weights_path)
|
| 70 |
else:
|
| 71 |
-
print("β¨ Loading pretrained model '
|
| 72 |
-
self.model = YOLO(f"{Config.
|
| 73 |
return self.model
|
| 74 |
|
| 75 |
def train(self,
|
|
@@ -104,7 +106,7 @@ class YOLOManager:
|
|
| 104 |
train_params = {
|
| 105 |
'data': data_yaml_path,
|
| 106 |
'imgsz': Config.DEFAULT_IMAGE_SIZE,
|
| 107 |
-
'epochs':
|
| 108 |
'batch': 10,
|
| 109 |
'name': run_name,
|
| 110 |
'device': device,
|
|
|
|
| 54 |
from typing import List, Optional, Dict, Any
|
| 55 |
from .utils import get_abs_path, clean_directory
|
| 56 |
from .config import Config
|
| 57 |
+
from dotenv import load_dotenv
|
| 58 |
+
load_dotenv()
|
| 59 |
|
| 60 |
class YOLOManager:
|
| 61 |
"""Manages YOLO model training and inference operations."""
|
|
|
|
| 70 |
print(f"π¦ Loading model from: {weights_path}")
|
| 71 |
self.model = YOLO(weights_path)
|
| 72 |
else:
|
| 73 |
+
print(f"β¨ Loading pretrained model '{Config.yolo_base_model_path}'")
|
| 74 |
+
self.model = YOLO(f"{Config.yolo_base_model_path}")
|
| 75 |
return self.model
|
| 76 |
|
| 77 |
def train(self,
|
|
|
|
| 106 |
train_params = {
|
| 107 |
'data': data_yaml_path,
|
| 108 |
'imgsz': Config.DEFAULT_IMAGE_SIZE,
|
| 109 |
+
'epochs': Config.YOLO_BASE_MODEL_NAME,
|
| 110 |
'batch': 10,
|
| 111 |
'name': run_name,
|
| 112 |
'device': device,
|