Instructions to use sdhaos/Aminatron with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use sdhaos/Aminatron with Keras:
# !pip install -U keras tensorflow huggingface_hub # Keras needs TensorFlow installed to read "hf://" paths, so the tensorflow backend is selected here; # "jax" and "torch" also work for computation once TensorFlow is installed. import os os.environ["KERAS_BACKEND"] = "tensorflow" import keras model = keras.saving.load_model("hf://sdhaos/Aminatron") - Notebooks
- Google Colab
- Kaggle
Download aminatron_model.py from sdhaos/Aminatron: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/sdhaos/Aminatron/resolve/main/aminatron_model.py
- Command line
-
hf download hf://sdhaos/Aminatron/aminatron_model.py
-
curl -L -o aminatron_model.py https://huggingface.co/sdhaos/Aminatron/resolve/main/aminatron_model.py
2.92 kB
| from __future__ import annotations | |
| from collections import Counter | |
| from functools import lru_cache | |
| from pathlib import Path | |
| from typing import Any | |
| from PIL import Image | |
| PROJECT_DIR = Path(__file__).resolve().parent | |
| LOCAL_MODEL_PATH = PROJECT_DIR / "model" / "aminatron.pt" | |
| def resolve_model_path(model_path: str | None = None) -> str: | |
| """Return the Aminatron model path or fail with a clear setup message.""" | |
| if model_path: | |
| return str(Path(model_path).expanduser().resolve()) if Path(model_path).exists() else model_path | |
| if LOCAL_MODEL_PATH.exists(): | |
| return str(LOCAL_MODEL_PATH) | |
| raise FileNotFoundError( | |
| f"Aminatron model not found: {LOCAL_MODEL_PATH}. " | |
| "Create it with: python train.py --save-as-aminatron" | |
| ) | |
| def load_model(model_path: str | None = None) -> YOLO: | |
| """Load and cache the detector model.""" | |
| from ultralytics import YOLO | |
| return YOLO(resolve_model_path(model_path)) | |
| def detect_image( | |
| image: Image.Image, | |
| model_path: str | None = None, | |
| confidence: float = 0.25, | |
| iou: float = 0.45, | |
| image_size: int = 640, | |
| max_detections: int = 100, | |
| ) -> Any: | |
| """Detect multiple objects on a single image.""" | |
| model = load_model(model_path) | |
| rgb_image = image.convert("RGB") | |
| results = model.predict( | |
| rgb_image, | |
| conf=confidence, | |
| iou=iou, | |
| imgsz=image_size, | |
| max_det=max_detections, | |
| verbose=False, | |
| ) | |
| return results[0] | |
| def format_detections(result: Any) -> list[dict[str, object]]: | |
| """Convert YOLO boxes to readable dictionaries.""" | |
| detections = [] | |
| names = result.names | |
| for box in result.boxes: | |
| class_id = int(box.cls[0].item()) | |
| confidence = float(box.conf[0].item()) | |
| x1, y1, x2, y2 = [float(value) for value in box.xyxy[0].tolist()] | |
| detections.append( | |
| { | |
| "class": names[class_id], | |
| "confidence": round(confidence, 4), | |
| "box": [round(x1, 2), round(y1, 2), round(x2, 2), round(y2, 2)], | |
| } | |
| ) | |
| detections.sort(key=lambda item: item["confidence"], reverse=True) | |
| return detections | |
| def summarize_detections(detections: list[dict[str, object]]) -> dict[str, int]: | |
| """Count detected objects by class.""" | |
| return dict(Counter(str(item["class"]) for item in detections)) | |
| def render_result_image(result: Any) -> Image.Image: | |
| """Return a PIL image with bounding boxes drawn by YOLO.""" | |
| # Ultralytics returns a BGR numpy image from plot(), PIL expects RGB. | |
| annotated_bgr = result.plot() | |
| return Image.fromarray(annotated_bgr[..., ::-1]) | |
| def save_result_image(result: Any, output_path: Path) -> Path: | |
| """Save an annotated prediction image.""" | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| render_result_image(result).save(output_path) | |
| return output_path | |