| | from fastapi import FastAPI, File, UploadFile |
| | import numpy as np |
| | from PIL import Image |
| | import io |
| | import cv2 |
| | from datasets import load_dataset |
| |
|
| | app = FastAPI() |
| |
|
| | |
| | dataset = load_dataset("susnato/plant_disease_detection_processed") |
| |
|
| | |
| | |
| | train_data = dataset["train"] |
| |
|
| | @app.post("/detect_disease/") |
| | async def detect_disease(file: UploadFile = File(...)): |
| | |
| | image_bytes = await file.read() |
| | image = Image.open(io.BytesIO(image_bytes)) |
| | img_np = np.array(image) |
| |
|
| | |
| | gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY) |
| | edges = cv2.Canny(gray, 100, 200) |
| |
|
| | |
| | |
| | disease_detected = "Enfermedad detectada" if np.mean(edges) > 50 else "Saludable" |
| |
|
| | |
| | example_image = train_data[0]['image'] |
| | example_label = train_data[0]['label'] |
| |
|
| | return { |
| | "diagnosis": disease_detected, |
| | "example_image": example_image, |
| | "example_label": example_label |
| | } |