|
| 1 | +from typing import TypedDict, Literal |
| 2 | +import json |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from platformdirs import user_data_dir |
| 6 | + |
| 7 | +FeatureType = Literal["object_detection", "face_detection", "face_embedding"] |
| 8 | +TierType = Literal["nano", "small", "medium", "required"] |
| 9 | + |
| 10 | + |
| 11 | +class ModelSpec(TypedDict): |
| 12 | + filename: str |
| 13 | + url: str |
| 14 | + sha256: str |
| 15 | + size_mb: float |
| 16 | + feature: FeatureType |
| 17 | + tier: TierType |
| 18 | + |
| 19 | + |
| 20 | +MODEL_REGISTRY: dict[str, ModelSpec] = { |
| 21 | + "yolo_nano": ModelSpec( |
| 22 | + filename="YOLOv11_Nano.onnx", |
| 23 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/YOLOv11_Nano.onnx", |
| 24 | + sha256="64e0a360a854cd1cebf697e38967adb73f24a2c41a86379f8a0bfae1b0c6af0b", |
| 25 | + size_mb=10.2, |
| 26 | + feature="object_detection", |
| 27 | + tier="nano", |
| 28 | + ), |
| 29 | + "yolo_nano_face": ModelSpec( |
| 30 | + filename="YOLOv11_Nano_Face.onnx", |
| 31 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/YOLOv11_Nano_Face.onnx", |
| 32 | + sha256="1d09cb0f31d46700a3e80838623aeafa125f2cd0d1c9a12f0b0853bf64b0a83d", |
| 33 | + size_mb=10.1, |
| 34 | + feature="face_detection", |
| 35 | + tier="nano", |
| 36 | + ), |
| 37 | + "yolo_small": ModelSpec( |
| 38 | + filename="YOLOv11_Small.onnx", |
| 39 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/YOLOv11_Small.onnx", |
| 40 | + sha256="8bfa953dbe93bef33b09be00da01cb4727f25416cb5d2fdb2a9f1083283b8aaa", |
| 41 | + size_mb=36.2, |
| 42 | + feature="object_detection", |
| 43 | + tier="small", |
| 44 | + ), |
| 45 | + "yolo_small_face": ModelSpec( |
| 46 | + filename="YOLOv11_Small_Face.onnx", |
| 47 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/YOLOv11_Small_Face.onnx", |
| 48 | + sha256="213333bbecd049c8e1d8bc63b4799df08d2ec52c8f8a6737b37b75ba839d7c03", |
| 49 | + size_mb=36.1, |
| 50 | + feature="face_detection", |
| 51 | + tier="small", |
| 52 | + ), |
| 53 | + "yolo_medium": ModelSpec( |
| 54 | + filename="YOLOv11_Medium.onnx", |
| 55 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/YOLOv11_Medium.onnx", |
| 56 | + sha256="f53a0bf6a141788f329ab92b438045f0ebac73ff10285b9ef0d06551cdbd01ea", |
| 57 | + size_mb=76.9, |
| 58 | + feature="object_detection", |
| 59 | + tier="medium", |
| 60 | + ), |
| 61 | + "yolo_medium_face": ModelSpec( |
| 62 | + filename="YOLOv11_Medium_Face.onnx", |
| 63 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/YOLOv11_Medium_Face.onnx", |
| 64 | + sha256="fd3ab085d776ee1ce59fd47def82c2abf49bbfde3a10a1b5b660b76cb41a6912", |
| 65 | + size_mb=76.6, |
| 66 | + feature="face_detection", |
| 67 | + tier="medium", |
| 68 | + ), |
| 69 | + "facenet": ModelSpec( |
| 70 | + filename="FaceNet_128D.onnx", |
| 71 | + url="https://github.com/AOSSIE-Org/PictoPy/releases/download/models-v1.0/FaceNet_128D.onnx", |
| 72 | + sha256="c37946ea8cce94141777dcbcccb8a61786c9a7c4f0c9f40471bd27029fa664ed", |
| 73 | + size_mb=87.0, |
| 74 | + feature="face_embedding", |
| 75 | + tier="required", |
| 76 | + ), |
| 77 | +} |
| 78 | + |
| 79 | +TIER_MODELS: dict[str, list[str]] = { |
| 80 | + "nano": ["yolo_nano", "yolo_nano_face"], |
| 81 | + "small": ["yolo_small", "yolo_small_face"], |
| 82 | + "medium": ["yolo_medium", "yolo_medium_face"], |
| 83 | + "required": ["facenet"], # Required model; not user-selectable |
| 84 | +} |
| 85 | + |
| 86 | +USER_DATA_MODELS = os.path.join(user_data_dir("PictoPy"), "models") |
| 87 | +LOCAL_ONNX_EXPORTS = os.path.join(os.path.dirname(__file__), "ONNX_Exports") |
| 88 | + |
| 89 | + |
| 90 | +def ensure_model_exports_directory() -> None: |
| 91 | + """Create the active model exports directory if it does not exist.""" |
| 92 | + if getattr(sys, 'frozen', False): |
| 93 | + os.makedirs(USER_DATA_MODELS, exist_ok=True) |
| 94 | + else: |
| 95 | + os.makedirs(LOCAL_ONNX_EXPORTS, exist_ok=True) |
| 96 | + |
| 97 | + |
| 98 | +def get_model_path(key: str) -> str: |
| 99 | + filename = MODEL_REGISTRY[key]["filename"] |
| 100 | + ensure_model_exports_directory() |
| 101 | + |
| 102 | + # In production (compiled by PyInstaller), use the platform-appropriate user data directory. |
| 103 | + if getattr(sys, 'frozen', False): |
| 104 | + return os.path.normpath(os.path.join(USER_DATA_MODELS, filename)) |
| 105 | + |
| 106 | + # In development, strictly use the local repo folder |
| 107 | + return os.path.normpath(os.path.join(LOCAL_ONNX_EXPORTS, filename)) |
| 108 | + |
0 commit comments