|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import math |
| 4 | +import threading |
4 | 5 | from pathlib import Path |
5 | 6 | from typing import Optional, Tuple |
6 | 7 |
|
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | Rect = Tuple[int, int, int, int] |
| 14 | +_CV2_IMPORT_LOCK = threading.Lock() |
13 | 15 |
|
14 | 16 |
|
15 | 17 | def merge_face_rects(frame_size: Tuple[int, int], rects: list[Rect]) -> list[Rect]: |
@@ -348,17 +350,12 @@ def _ultralight_input_size_from_name(model_name: str) -> Tuple[int, int]: |
348 | 350 |
|
349 | 351 |
|
350 | 352 | def create_ultralight_net(*, model_path: Path): |
351 | | - import cv2 |
| 353 | + with _CV2_IMPORT_LOCK: |
| 354 | + import cv2 |
352 | 355 |
|
353 | 356 | if not hasattr(cv2, "dnn"): |
354 | 357 | raise RuntimeError("OpenCV dnn is not available in this build") |
355 | | - try: |
356 | | - blob = model_path.read_bytes() |
357 | | - if not blob: |
358 | | - raise RuntimeError("ONNX file is empty") |
359 | | - return cv2.dnn.readNetFromONNX(blob) |
360 | | - except Exception: |
361 | | - return cv2.dnn.readNetFromONNX(str(model_path)) |
| 358 | + return cv2.dnn.readNetFromONNX(str(model_path)) |
362 | 359 |
|
363 | 360 |
|
364 | 361 | def _generate_ultralight_priors(input_size: Tuple[int, int]): |
@@ -631,17 +628,12 @@ def ensure_scrfd_model_path( |
631 | 628 |
|
632 | 629 |
|
633 | 630 | def create_scrfd_net(*, model_path: Path): |
634 | | - import cv2 |
| 631 | + with _CV2_IMPORT_LOCK: |
| 632 | + import cv2 |
635 | 633 |
|
636 | 634 | if not hasattr(cv2, "dnn"): |
637 | 635 | raise RuntimeError("OpenCV dnn is not available in this build") |
638 | | - try: |
639 | | - blob = model_path.read_bytes() |
640 | | - if not blob: |
641 | | - raise RuntimeError("ONNX file is empty") |
642 | | - return cv2.dnn.readNetFromONNX(blob) |
643 | | - except Exception: |
644 | | - return cv2.dnn.readNetFromONNX(str(model_path)) |
| 636 | + return cv2.dnn.readNetFromONNX(str(model_path)) |
645 | 637 |
|
646 | 638 |
|
647 | 639 | def detect_faces_scrfd( |
|
0 commit comments