Object Detection
ultralytics
English
agriculture
weed-detection
yolo
yolov8
yolo26
precision-agriculture
research
Instructions to use Rmano-Rudeson/weedscan-kz with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ultralytics
How to use Rmano-Rudeson/weedscan-kz with ultralytics:
from huggingface_hub import hf_hub_download from ultralytics import YOLO # pick the weights file from this repo's "Files and versions" tab weights = hf_hub_download("Rmano-Rudeson/weedscan-kz", "<weights>.pt") model = YOLO(weights) source = 'http://images.cocodataset.org/val2017/000000039769.jpg' model.predict(source=source, save=True) - Notebooks
- Google Colab
- Kaggle
Download inference_server.py from Rmano-Rudeson/weedscan-kz: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/Rmano-Rudeson/weedscan-kz/resolve/main/inference_server.py
- Command line
-
hf download hf://Rmano-Rudeson/weedscan-kz/inference_server.py
-
curl -L -o inference_server.py https://huggingface.co/Rmano-Rudeson/weedscan-kz/resolve/main/inference_server.py
2.4 kB
| """Local FastAPI adapter for the recovered WeedScan KZ frontend.""" | |
| import base64 | |
| import binascii | |
| import io | |
| import os | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI, HTTPException, Request | |
| from PIL import Image, UnidentifiedImageError | |
| from pydantic import BaseModel, Field | |
| from predict import load_model | |
| async def lifespan(app): | |
| app.state.model = load_model(os.environ.get("WEEDSCAN_VARIANT", "yolov26n-10class")) | |
| yield | |
| app = FastAPI(title="WeedScan KZ", lifespan=lifespan) | |
| class InferRequest(BaseModel): | |
| imageBase64: str = Field(max_length=20_000_000) | |
| def classify(payload: InferRequest, request: Request): | |
| try: | |
| raw = base64.b64decode(payload.imageBase64, validate=True) | |
| image = Image.open(io.BytesIO(raw)) | |
| if image.width * image.height > 25_000_000: | |
| raise ValueError("Image exceeds 25 megapixels") | |
| image = image.convert("RGB") | |
| except (binascii.Error, UnidentifiedImageError, OSError, ValueError, Image.DecompressionBombError) as exc: | |
| raise HTTPException(status_code=400, detail="Invalid image or unsupported image size") from exc | |
| model = request.app.state.model | |
| result = model.predict(image, device="cpu", verbose=False)[0] | |
| width, height = image.size | |
| boxes, counts = [], {} | |
| weed_count, weed_box_area = 0, 0.0 | |
| for box in result.boxes: | |
| x1, y1, x2, y2 = box.xyxy[0].tolist() | |
| label = model.names[int(box.cls[0])] | |
| item = {"x": x1 / width, "y": y1 / height, | |
| "width": (x2 - x1) / width, "height": (y2 - y1) / height, | |
| "label": label, "confidence": round(float(box.conf[0]), 4)} | |
| boxes.append(item) | |
| counts[label] = counts.get(label, 0) + 1 | |
| if label.startswith("weed:"): | |
| weed_count += 1 | |
| weed_box_area += item["width"] * item["height"] | |
| total = len(boxes) | |
| species = [{"name": name, "count": count, "percentage": round(100 * count / total, 1)} | |
| for name, count in counts.items()] | |
| return {"weedCount": weed_count, "infestationRate": round(min(100 * weed_box_area, 100), 1), | |
| "species": species, "boundingBoxes": boxes, | |
| "summary": f"Detected {weed_count} weed instances among {total} plant detections. " | |
| "Coverage is an uncalibrated bounding-box area estimate."} | |