Spaces:
Running
Running
deploy added
Browse files
comic_panel_extractor/annorator_server.py
CHANGED
|
@@ -434,6 +434,14 @@ async def reset_model():
|
|
| 434 |
|
| 435 |
return {'message': 'Model Reseted', 'status': 'success'}
|
| 436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
@app.get("/api/annotate/train")
|
| 438 |
async def upload_image():
|
| 439 |
os.environ['PYTHONUNBUFFERED'] = "1"
|
|
|
|
| 434 |
|
| 435 |
return {'message': 'Model Reseted', 'status': 'success'}
|
| 436 |
|
| 437 |
+
@app.post("/api/annotate/deploy")
|
| 438 |
+
async def deploy_model(app_name: str):
|
| 439 |
+
from yolo_manager import YOLOManager
|
| 440 |
+
with YOLOManager() as yolo_manager:
|
| 441 |
+
yolo_manager.deploy()
|
| 442 |
+
|
| 443 |
+
return {'message': 'Model Deployed', 'status': 'success'}
|
| 444 |
+
|
| 445 |
@app.get("/api/annotate/train")
|
| 446 |
async def upload_image():
|
| 447 |
os.environ['PYTHONUNBUFFERED'] = "1"
|
comic_panel_extractor/static/annotator.html
CHANGED
|
@@ -1185,8 +1185,8 @@
|
|
| 1185 |
document.getElementById('deployBtn').addEventListener('click', async () => {
|
| 1186 |
const appName = document.getElementById('appName').value;
|
| 1187 |
if (!appName) {
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
}
|
| 1191 |
|
| 1192 |
try {
|
|
|
|
| 1185 |
document.getElementById('deployBtn').addEventListener('click', async () => {
|
| 1186 |
const appName = document.getElementById('appName').value;
|
| 1187 |
if (!appName) {
|
| 1188 |
+
appName = "Comic Panel Extractor"
|
| 1189 |
+
this.showAlert('Using App Name: "Comic Panel Extractor"');
|
| 1190 |
}
|
| 1191 |
|
| 1192 |
try {
|
comic_panel_extractor/train.py
CHANGED
|
@@ -177,18 +177,12 @@ def main():
|
|
| 177 |
print(f"π― Training model: {config.YOLO_MODEL_NAME}")
|
| 178 |
|
| 179 |
# Train model
|
| 180 |
-
|
| 181 |
-
data_yaml_path=data_yaml_path
|
| 182 |
-
run_name=config.YOLO_MODEL_NAME
|
| 183 |
)
|
| 184 |
|
| 185 |
# Validate model
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
# Backup best weights
|
| 189 |
-
weights_path = yolo_manager.get_best_weights_path()
|
| 190 |
-
backup_path = config.yolo_trained_model_path
|
| 191 |
-
backup_file(weights_path, backup_path)
|
| 192 |
|
| 193 |
print("π Training completed successfully!")
|
| 194 |
|
|
|
|
| 177 |
print(f"π― Training model: {config.YOLO_MODEL_NAME}")
|
| 178 |
|
| 179 |
# Train model
|
| 180 |
+
yolo_manager.train(
|
| 181 |
+
data_yaml_path=data_yaml_path
|
|
|
|
| 182 |
)
|
| 183 |
|
| 184 |
# Validate model
|
| 185 |
+
yolo_manager.validate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
print("π Training completed successfully!")
|
| 188 |
|
comic_panel_extractor/yolo_manager.py
CHANGED
|
@@ -42,14 +42,6 @@ def get_image_paths(directories: Union[str, List[str]]) -> List[str]:
|
|
| 42 |
|
| 43 |
return list(set(all_images)) # Remove duplicates
|
| 44 |
|
| 45 |
-
def backup_file(source_path: str, backup_path: str) -> str:
|
| 46 |
-
"""Backup a file to specified location."""
|
| 47 |
-
backup_path = get_abs_path(backup_path)
|
| 48 |
-
os.makedirs(os.path.dirname(backup_path), exist_ok=True)
|
| 49 |
-
shutil.copy(source_path, backup_path)
|
| 50 |
-
print(f"β
File backed up to: {backup_path}")
|
| 51 |
-
return backup_path
|
| 52 |
-
|
| 53 |
# yolo_manager.py
|
| 54 |
import os
|
| 55 |
import cv2
|
|
@@ -62,8 +54,8 @@ load_dotenv()
|
|
| 62 |
class YOLOManager:
|
| 63 |
"""Manages YOLO model training and inference operations."""
|
| 64 |
|
| 65 |
-
def __init__(self
|
| 66 |
-
self.model_name =
|
| 67 |
self.model = None
|
| 68 |
|
| 69 |
def load_model(self, weights_path: Optional[str] = None) -> YOLO:
|
|
@@ -78,7 +70,6 @@ class YOLOManager:
|
|
| 78 |
|
| 79 |
def train(self,
|
| 80 |
data_yaml_path: str,
|
| 81 |
-
run_name: Optional[str] = None,
|
| 82 |
device: int = 0,
|
| 83 |
resume: bool = config.RESUME_TRAIN,
|
| 84 |
**kwargs) -> YOLO:
|
|
@@ -87,13 +78,11 @@ class YOLOManager:
|
|
| 87 |
|
| 88 |
Args:
|
| 89 |
data_yaml_path: Path to dataset YAML file
|
| 90 |
-
run_name: Name for the training run
|
| 91 |
device: Device to use for training
|
| 92 |
resume: Whether to resume from checkpoint if available
|
| 93 |
**kwargs: Additional training parameters
|
| 94 |
"""
|
| 95 |
-
|
| 96 |
-
checkpoint_path = f"{config.current_path}/runs/detect/{run_name}/weights/last.pt"
|
| 97 |
|
| 98 |
# Check for existing checkpoint
|
| 99 |
if resume and os.path.isfile(checkpoint_path):
|
|
@@ -110,7 +99,7 @@ class YOLOManager:
|
|
| 110 |
'imgsz': config.DEFAULT_IMAGE_SIZE,
|
| 111 |
'epochs': config.EPOCH,
|
| 112 |
'batch': config.BATCH,
|
| 113 |
-
'name':
|
| 114 |
'device': device,
|
| 115 |
'cache': True,
|
| 116 |
'project': f'{config.current_path}/runs/detect',
|
|
@@ -136,11 +125,23 @@ class YOLOManager:
|
|
| 136 |
metrics = self.model.val()
|
| 137 |
print("π Validation Metrics:", metrics)
|
| 138 |
return metrics
|
| 139 |
-
|
| 140 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
"""Get path to best trained weights."""
|
| 142 |
-
|
| 143 |
-
weights_path = os.path.join(config.current_path, 'runs', 'detect', run_name, 'weights', 'best.pt')
|
| 144 |
|
| 145 |
if not os.path.isfile(weights_path):
|
| 146 |
raise FileNotFoundError(f"β Trained weights not found at: {weights_path}")
|
|
|
|
| 42 |
|
| 43 |
return list(set(all_images)) # Remove duplicates
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# yolo_manager.py
|
| 46 |
import os
|
| 47 |
import cv2
|
|
|
|
| 54 |
class YOLOManager:
|
| 55 |
"""Manages YOLO model training and inference operations."""
|
| 56 |
|
| 57 |
+
def __init__(self):
|
| 58 |
+
self.model_name = config.YOLO_MODEL_NAME
|
| 59 |
self.model = None
|
| 60 |
|
| 61 |
def load_model(self, weights_path: Optional[str] = None) -> YOLO:
|
|
|
|
| 70 |
|
| 71 |
def train(self,
|
| 72 |
data_yaml_path: str,
|
|
|
|
| 73 |
device: int = 0,
|
| 74 |
resume: bool = config.RESUME_TRAIN,
|
| 75 |
**kwargs) -> YOLO:
|
|
|
|
| 78 |
|
| 79 |
Args:
|
| 80 |
data_yaml_path: Path to dataset YAML file
|
|
|
|
| 81 |
device: Device to use for training
|
| 82 |
resume: Whether to resume from checkpoint if available
|
| 83 |
**kwargs: Additional training parameters
|
| 84 |
"""
|
| 85 |
+
checkpoint_path = f"{config.current_path}/runs/detect/{self.model_name}/weights/last.pt"
|
|
|
|
| 86 |
|
| 87 |
# Check for existing checkpoint
|
| 88 |
if resume and os.path.isfile(checkpoint_path):
|
|
|
|
| 99 |
'imgsz': config.DEFAULT_IMAGE_SIZE,
|
| 100 |
'epochs': config.EPOCH,
|
| 101 |
'batch': config.BATCH,
|
| 102 |
+
'name': self.model_name,
|
| 103 |
'device': device,
|
| 104 |
'cache': True,
|
| 105 |
'project': f'{config.current_path}/runs/detect',
|
|
|
|
| 125 |
metrics = self.model.val()
|
| 126 |
print("π Validation Metrics:", metrics)
|
| 127 |
return metrics
|
| 128 |
+
|
| 129 |
+
def deploy(self):
|
| 130 |
+
weights_path = self.get_best_weights_path()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
from pathlib import Path
|
| 134 |
+
deploy_path = config.yolo_trained_model_path
|
| 135 |
+
file_path = Path(deploy_path)
|
| 136 |
+
|
| 137 |
+
if file_path.exists():
|
| 138 |
+
file_path.unlink()
|
| 139 |
+
|
| 140 |
+
shutil.copy(weights_path, deploy_path)
|
| 141 |
+
|
| 142 |
+
def get_best_weights_path(self) -> str:
|
| 143 |
"""Get path to best trained weights."""
|
| 144 |
+
weights_path = os.path.join(config.current_path, 'runs', 'detect', self.model_name, 'weights', 'best.pt')
|
|
|
|
| 145 |
|
| 146 |
if not os.path.isfile(weights_path):
|
| 147 |
raise FileNotFoundError(f"β Trained weights not found at: {weights_path}")
|