Spaces:
Running
Running
re arrange ui
Browse files
comic_panel_extractor/annorator_server.py
CHANGED
|
@@ -10,6 +10,8 @@ import shutil
|
|
| 10 |
from .config import Config
|
| 11 |
from typing import List, Optional, Union, Dict, Any
|
| 12 |
from . import utils
|
|
|
|
|
|
|
| 13 |
|
| 14 |
app = APIRouter()
|
| 15 |
|
|
@@ -73,17 +75,19 @@ def load_yolo_annotations(image_path: str, label_path: str, detect: bool = False
|
|
| 73 |
annotations = []
|
| 74 |
|
| 75 |
# Auto-detect if needed
|
|
|
|
| 76 |
if detect and not os.path.exists(label_path):
|
| 77 |
from .yolo_manager import YOLOManager
|
| 78 |
with YOLOManager() as yolo_manager:
|
| 79 |
weights_path = Config.yolo_trained_model_path
|
| 80 |
yolo_manager.load_model(weights_path)
|
| 81 |
-
|
| 82 |
image_paths=[image_path],
|
| 83 |
output_dir=IMAGE_LABEL_ROOT,
|
| 84 |
save_image=False,
|
| 85 |
label_path=label_path
|
| 86 |
)
|
|
|
|
| 87 |
|
| 88 |
if os.path.exists(label_path):
|
| 89 |
with open(label_path, "r") as f:
|
|
@@ -134,13 +138,32 @@ def load_yolo_annotations(image_path: str, label_path: str, detect: bool = False
|
|
| 134 |
"fill": "rgba(0, 255, 0, 0.2)",
|
| 135 |
"saved": True
|
| 136 |
})
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
return annotations, (w, h)
|
| 139 |
except Exception as e:
|
| 140 |
-
raise HTTPException(status_code=500, detail=f"Error loading annotations: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def save_yolo_annotations(annotations: List[Box], original_size: tuple, label_path: str):
|
| 143 |
"""Save annotations in YOLO format (both bbox and segmentation)"""
|
|
|
|
| 144 |
os.makedirs(os.path.dirname(label_path), exist_ok=True)
|
| 145 |
w, h = original_size
|
| 146 |
|
|
@@ -169,7 +192,7 @@ def save_yolo_annotations(annotations: List[Box], original_size: tuple, label_pa
|
|
| 169 |
shutil.copy2(label_path, f"{IMAGE_LABEL_ROOT}/{os.path.basename(label_path)}")
|
| 170 |
return True
|
| 171 |
except Exception as e:
|
| 172 |
-
raise HTTPException(status_code=500, detail=f"Error saving annotations: {str(e)}")
|
| 173 |
|
| 174 |
def parse_yolo_line(line: str, image_width: int, image_height: int) -> Dict[str, Any]:
|
| 175 |
"""Parse a single YOLO format line and return annotation dict"""
|
|
@@ -273,7 +296,7 @@ async def get_annotations(image_name: str):
|
|
| 273 |
annotations, (width, height) = load_yolo_annotations(image_path, label_path)
|
| 274 |
|
| 275 |
return {
|
| 276 |
-
"annotations":
|
| 277 |
"original_width": width,
|
| 278 |
"original_height": height
|
| 279 |
}
|
|
@@ -288,7 +311,7 @@ async def get_detected_annotations(image_name: str):
|
|
| 288 |
|
| 289 |
annotations, (width, height) = load_yolo_annotations(image_path, label_path, True)
|
| 290 |
return {
|
| 291 |
-
"annotations":
|
| 292 |
"original_width": width,
|
| 293 |
"original_height": height
|
| 294 |
}
|
|
|
|
| 10 |
from .config import Config
|
| 11 |
from typing import List, Optional, Union, Dict, Any
|
| 12 |
from . import utils
|
| 13 |
+
import copy
|
| 14 |
+
import traceback
|
| 15 |
|
| 16 |
app = APIRouter()
|
| 17 |
|
|
|
|
| 75 |
annotations = []
|
| 76 |
|
| 77 |
# Auto-detect if needed
|
| 78 |
+
normalise = False
|
| 79 |
if detect and not os.path.exists(label_path):
|
| 80 |
from .yolo_manager import YOLOManager
|
| 81 |
with YOLOManager() as yolo_manager:
|
| 82 |
weights_path = Config.yolo_trained_model_path
|
| 83 |
yolo_manager.load_model(weights_path)
|
| 84 |
+
yolo_manager.annotate_images(
|
| 85 |
image_paths=[image_path],
|
| 86 |
output_dir=IMAGE_LABEL_ROOT,
|
| 87 |
save_image=False,
|
| 88 |
label_path=label_path
|
| 89 |
)
|
| 90 |
+
normalise = True
|
| 91 |
|
| 92 |
if os.path.exists(label_path):
|
| 93 |
with open(label_path, "r") as f:
|
|
|
|
| 138 |
"fill": "rgba(0, 255, 0, 0.2)",
|
| 139 |
"saved": True
|
| 140 |
})
|
| 141 |
+
if normalise:
|
| 142 |
+
annotations = utils.normalize_segmentation(annotations)
|
| 143 |
+
save_yolo_annotations(
|
| 144 |
+
copy.deepcopy(annotations),
|
| 145 |
+
(w, h),
|
| 146 |
+
label_path
|
| 147 |
+
)
|
| 148 |
return annotations, (w, h)
|
| 149 |
except Exception as e:
|
| 150 |
+
raise HTTPException(status_code=500, detail=f"Error loading annotations: {str(e)} {traceback.format_exc()}")
|
| 151 |
+
|
| 152 |
+
def normalize_annotations(annotations: List[Union[Box, dict]]) -> List[Box]:
|
| 153 |
+
"""Convert all annotations to Box objects."""
|
| 154 |
+
normalized = []
|
| 155 |
+
for ann in annotations:
|
| 156 |
+
if isinstance(ann, Box):
|
| 157 |
+
normalized.append(ann)
|
| 158 |
+
elif isinstance(ann, dict):
|
| 159 |
+
normalized.append(Box(**ann))
|
| 160 |
+
else:
|
| 161 |
+
raise TypeError(f"Unsupported annotation type: {type(ann)}")
|
| 162 |
+
return normalized
|
| 163 |
|
| 164 |
def save_yolo_annotations(annotations: List[Box], original_size: tuple, label_path: str):
|
| 165 |
"""Save annotations in YOLO format (both bbox and segmentation)"""
|
| 166 |
+
annotations = normalize_annotations(annotations)
|
| 167 |
os.makedirs(os.path.dirname(label_path), exist_ok=True)
|
| 168 |
w, h = original_size
|
| 169 |
|
|
|
|
| 192 |
shutil.copy2(label_path, f"{IMAGE_LABEL_ROOT}/{os.path.basename(label_path)}")
|
| 193 |
return True
|
| 194 |
except Exception as e:
|
| 195 |
+
raise HTTPException(status_code=500, detail=f"Error saving annotations: {str(e)} {traceback.format_exc()}")
|
| 196 |
|
| 197 |
def parse_yolo_line(line: str, image_width: int, image_height: int) -> Dict[str, Any]:
|
| 198 |
"""Parse a single YOLO format line and return annotation dict"""
|
|
|
|
| 296 |
annotations, (width, height) = load_yolo_annotations(image_path, label_path)
|
| 297 |
|
| 298 |
return {
|
| 299 |
+
"annotations": annotations,
|
| 300 |
"original_width": width,
|
| 301 |
"original_height": height
|
| 302 |
}
|
|
|
|
| 311 |
|
| 312 |
annotations, (width, height) = load_yolo_annotations(image_path, label_path, True)
|
| 313 |
return {
|
| 314 |
+
"annotations": annotations,
|
| 315 |
"original_width": width,
|
| 316 |
"original_height": height
|
| 317 |
}
|
comic_panel_extractor/llm_panel_extractor.py
CHANGED
|
@@ -17,7 +17,7 @@ class LLMPanelExtractor:
|
|
| 17 |
|
| 18 |
# Check if YOLO model exists; if not, download it to the specified path
|
| 19 |
yolo_base_model_path = f'{self.config.yolo_base_model_path}_best.pt'
|
| 20 |
-
|
| 21 |
if not os.path.exists(yolo_base_model_path):
|
| 22 |
url = "https://huggingface.co/mosesb/best-comic-panel-detection/resolve/main/best.pt"
|
| 23 |
print(f"Downloading YOLO model to {yolo_base_model_path}...")
|
|
|
|
| 17 |
|
| 18 |
# Check if YOLO model exists; if not, download it to the specified path
|
| 19 |
yolo_base_model_path = f'{self.config.yolo_base_model_path}_best.pt'
|
| 20 |
+
yolo_base_model_path = f'{self.config.yolo_trained_model_path}'
|
| 21 |
if not os.path.exists(yolo_base_model_path):
|
| 22 |
url = "https://huggingface.co/mosesb/best-comic-panel-detection/resolve/main/best.pt"
|
| 23 |
print(f"Downloading YOLO model to {yolo_base_model_path}...")
|
comic_panel_extractor/server.py
CHANGED
|
@@ -51,4 +51,4 @@ def main():
|
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
main
|
|
|
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
main()
|