feat(backend): services PlantNet et YOLO pour identification de plantes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-22 12:17:23 +01:00
parent 6ca233d720
commit 2e67e9cb02
3 changed files with 86 additions and 3 deletions

View File

@@ -0,0 +1,35 @@
import os
from typing import List
import httpx
PLANTNET_KEY = os.environ.get("PLANTNET_API_KEY", "2b1088cHCJ4c7Cn2Vqq67xfve")
PLANTNET_URL = "https://my-api.plantnet.org/v2/identify/all"
async def identify(image_bytes: bytes, filename: str = "photo.jpg") -> List[dict]:
"""Appelle PlantNet et retourne les 3 meilleures identifications."""
try:
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.post(
PLANTNET_URL,
params={"api-key": PLANTNET_KEY, "nb-results": 3},
files={"images": (filename, image_bytes, "image/jpeg")},
data={"organs": ["auto"]},
)
resp.raise_for_status()
data = resp.json()
except Exception:
return []
results = []
for r in data.get("results", [])[:3]:
species = r.get("species", {})
common_names = species.get("commonNames", [])
results.append({
"species": species.get("scientificNameWithoutAuthor", ""),
"common_name": common_names[0] if common_names else "",
"confidence": round(r.get("score", 0.0), 3),
"image_url": (r.get("images", [{}]) or [{}])[0].get("url", {}).get("m", ""),
})
return results