Spaces:
Runtime error
Runtime error
Update yolo_model.py
Browse files- yolo_model.py +17 -2
yolo_model.py
CHANGED
|
@@ -12,7 +12,6 @@ _yolo_model: Optional[YOLO] = None # cached model
|
|
| 12 |
|
| 13 |
|
| 14 |
def get_yolo_model() -> YOLO:
|
| 15 |
-
"""Load YOLO once and reuse."""
|
| 16 |
global _yolo_model
|
| 17 |
if _yolo_model is None:
|
| 18 |
if not _WEIGHTS_PATH.exists():
|
|
@@ -22,7 +21,7 @@ def get_yolo_model() -> YOLO:
|
|
| 22 |
|
| 23 |
|
| 24 |
def run_yolo_on_page(image: Image.Image, conf_threshold: float = 0.3) -> Dict[str, Any]:
|
| 25 |
-
"""Run YOLO on full page and return annotated image + detections
|
| 26 |
image = image.convert("RGB")
|
| 27 |
img_rgb = np.array(image)
|
| 28 |
|
|
@@ -33,6 +32,7 @@ def run_yolo_on_page(image: Image.Image, conf_threshold: float = 0.3) -> Dict[st
|
|
| 33 |
annotated_image = Image.fromarray(annotated)
|
| 34 |
|
| 35 |
detections: List[Dict[str, Any]] = []
|
|
|
|
| 36 |
names = results.names
|
| 37 |
|
| 38 |
for box in results.boxes:
|
|
@@ -54,7 +54,22 @@ def run_yolo_on_page(image: Image.Image, conf_threshold: float = 0.3) -> Dict[st
|
|
| 54 |
}
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
return {
|
| 58 |
"annotated_image": annotated_image,
|
| 59 |
"detections": detections,
|
|
|
|
| 60 |
}
|
|
|
|
| 12 |
|
| 13 |
|
| 14 |
def get_yolo_model() -> YOLO:
|
|
|
|
| 15 |
global _yolo_model
|
| 16 |
if _yolo_model is None:
|
| 17 |
if not _WEIGHTS_PATH.exists():
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
def run_yolo_on_page(image: Image.Image, conf_threshold: float = 0.3) -> Dict[str, Any]:
|
| 24 |
+
"""Run YOLO on full page and return annotated image + detections + crops."""
|
| 25 |
image = image.convert("RGB")
|
| 26 |
img_rgb = np.array(image)
|
| 27 |
|
|
|
|
| 32 |
annotated_image = Image.fromarray(annotated)
|
| 33 |
|
| 34 |
detections: List[Dict[str, Any]] = []
|
| 35 |
+
crops: List[Dict[str, Any]] = []
|
| 36 |
names = results.names
|
| 37 |
|
| 38 |
for box in results.boxes:
|
|
|
|
| 54 |
}
|
| 55 |
)
|
| 56 |
|
| 57 |
+
if x2 > x1 and y2 > y1:
|
| 58 |
+
crop_np = img_rgb[y1:y2, x1:x2]
|
| 59 |
+
if crop_np.size > 0:
|
| 60 |
+
crop_img = Image.fromarray(crop_np)
|
| 61 |
+
crops.append(
|
| 62 |
+
{
|
| 63 |
+
"cls_id": cls_id,
|
| 64 |
+
"cls_name": cls_name,
|
| 65 |
+
"conf": conf,
|
| 66 |
+
"box": [x1, y1, x2, y2],
|
| 67 |
+
"crop_image": crop_img,
|
| 68 |
+
}
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
return {
|
| 72 |
"annotated_image": annotated_image,
|
| 73 |
"detections": detections,
|
| 74 |
+
"crops": crops,
|
| 75 |
}
|