Spaces:
Build error
Build error
msIntui commited on
Commit ·
b76ce91
1
Parent(s): c2516d1
Refactor line detection with better fallback
Browse files- detectors.py +11 -16
- line_detectors.py +51 -0
detectors.py
CHANGED
|
@@ -60,6 +60,9 @@ from detection_schema import (
|
|
| 60 |
from skimage.morphology import skeletonize
|
| 61 |
from skimage.measure import label
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
class Detector(ABC):
|
| 64 |
"""Base class for all detectors"""
|
| 65 |
|
|
@@ -133,24 +136,16 @@ class LineDetector(Detector):
|
|
| 133 |
|
| 134 |
def __init__(self, config, model_path=None, model_config=None, device='cpu', debug_handler=None):
|
| 135 |
super().__init__(config, debug_handler)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
def detect(self, image: np.ndarray) -> Dict:
|
| 142 |
-
"""Detect lines using DeepLSD or fallback to basic methods"""
|
| 143 |
-
if self.use_deeplsd:
|
| 144 |
-
# Use DeepLSD
|
| 145 |
-
return self._detect_with_deeplsd(image)
|
| 146 |
else:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
|
| 150 |
-
def
|
| 151 |
-
|
| 152 |
-
# Basic implementation
|
| 153 |
-
return {'detections': []}
|
| 154 |
|
| 155 |
|
| 156 |
class PointDetector(Detector):
|
|
|
|
| 60 |
from skimage.morphology import skeletonize
|
| 61 |
from skimage.measure import label
|
| 62 |
|
| 63 |
+
# At the top with other imports
|
| 64 |
+
from line_detectors import OpenCVLineDetector, DeepLSDDetector, DEEPLSD_AVAILABLE
|
| 65 |
+
|
| 66 |
class Detector(ABC):
|
| 67 |
"""Base class for all detectors"""
|
| 68 |
|
|
|
|
| 136 |
|
| 137 |
def __init__(self, config, model_path=None, model_config=None, device='cpu', debug_handler=None):
|
| 138 |
super().__init__(config, debug_handler)
|
| 139 |
+
# Try to use DeepLSD if available, otherwise fall back to OpenCV
|
| 140 |
+
if DEEPLSD_AVAILABLE and model_path:
|
| 141 |
+
self.detector = DeepLSDDetector(model_path)
|
| 142 |
+
logger.info("Using DeepLSD for line detection")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
else:
|
| 144 |
+
self.detector = OpenCVLineDetector()
|
| 145 |
+
logger.info("Using OpenCV for line detection")
|
| 146 |
|
| 147 |
+
def detect(self, image: np.ndarray) -> Dict:
|
| 148 |
+
return self.detector.detect(image)
|
|
|
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
class PointDetector(Detector):
|
line_detectors.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from typing import Dict, Optional
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
class BaseLineDetector:
|
| 9 |
+
"""Base class for line detection methods"""
|
| 10 |
+
def detect(self, image: np.ndarray) -> Dict:
|
| 11 |
+
raise NotImplementedError
|
| 12 |
+
|
| 13 |
+
class OpenCVLineDetector(BaseLineDetector):
|
| 14 |
+
"""Basic line detection using OpenCV"""
|
| 15 |
+
def detect(self, image: np.ndarray) -> Dict:
|
| 16 |
+
# Convert to grayscale if needed
|
| 17 |
+
if len(image.shape) == 3:
|
| 18 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 19 |
+
else:
|
| 20 |
+
gray = image
|
| 21 |
+
|
| 22 |
+
# Basic line detection using HoughLinesP
|
| 23 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
| 24 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)
|
| 25 |
+
|
| 26 |
+
detections = []
|
| 27 |
+
if lines is not None:
|
| 28 |
+
for line in lines:
|
| 29 |
+
x1, y1, x2, y2 = line[0]
|
| 30 |
+
detections.append({
|
| 31 |
+
'start': [float(x1), float(y1)],
|
| 32 |
+
'end': [float(x2), float(y2)]
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
return {'detections': detections}
|
| 36 |
+
|
| 37 |
+
# Try to import DeepLSD, but don't fail if not available
|
| 38 |
+
try:
|
| 39 |
+
from deeplsd.models.deeplsd_inference import DeepLSD
|
| 40 |
+
class DeepLSDDetector(BaseLineDetector):
|
| 41 |
+
def __init__(self, model_path: Optional[str] = None):
|
| 42 |
+
self.model = DeepLSD(model_path) if model_path else None
|
| 43 |
+
|
| 44 |
+
def detect(self, image: np.ndarray) -> Dict:
|
| 45 |
+
# DeepLSD implementation
|
| 46 |
+
return {'detections': []}
|
| 47 |
+
DEEPLSD_AVAILABLE = True
|
| 48 |
+
except ImportError as e:
|
| 49 |
+
logger.warning(f"DeepLSD not available: {e}")
|
| 50 |
+
DEEPLSD_AVAILABLE = False
|
| 51 |
+
DeepLSDDetector = None
|