Version: 1.1.0
API để phát hiện bệnh trên rau cải bắp xanh (bok choy) sử dụng mô hình YOLO và Google Gemini AI.
Đây là một RESTful API được xây dựng bằng FastAPI để:
- Phát hiện bệnh trên lá rau cải bắp xanh thông qua ảnh
- Sử dụng mô hình YOLO đã được huấn luyện để detect các vùng bị bệnh
- Tích hợp Google Gemini AI để phân tích và đưa ra lời khuyên bằng tiếng Việt
- Trả về kết quả dưới dạng JSON với thông tin chi tiết về các vùng bệnh
- FastAPI: Framework để xây dựng API
- Ultralytics YOLO: Mô hình detect object
- Google Gemini AI: AI để phân tích và đưa ra lời khuyên
- PyTorch: Framework deep learning
- OpenCV & Pillow: Xử lý ảnh
- Pydantic: Validation data
bokchoy-api/
├── app/
│ ├── __init__.py
│ ├── main.py # File chính chứa API endpoints
│ ├── inference.py # Class xử lý YOLO model
│ └── schemas.py # Pydantic models cho request/response
├── models/
│ └── best.pt # YOLO model đã được train
├── requirements.txt # Dependencies
├── request.py # Script test API
└── README.md # File này
Đảm bảo bạn đã cài đặt Python 3.8+ trên máy:
python --versioncd e:\Python_Programming\bokchoy-api
python -m venv venv
venv\Scripts\activatepip install -r requirements.txtĐảm bảo file best.pt có trong thư mục models/. Đây là mô hình YOLO đã được huấn luyện để phát hiện bệnh trên rau cải.
cd app
uvicorn main:app --host 0.0.0.0 --port 8080 --reloadHoặc chạy từ thư mục gốc:
uvicorn app.main:app --host 0.0.0.0 --port 8080 --reloadServer sẽ chạy tại: http://localhost:8080
Mở trình duyệt và truy cập:
- API Documentation:
http://localhost:8080/docs - Health check:
http://localhost:8080/health
Method: POST
URL: http://localhost:8080/predict
Parameters:
file: File ảnh (jpg, png, jpeg)conf_threshold: Ngưỡng confidence (mặc định: 0.25)iou_threshold: Ngưỡng IoU (mặc định: 0.5)imgsz: Kích thước ảnh input (mặc định: 640)min_area: Diện tích tối thiểu của bounding box (mặc định: 0.0)
Response:
{
"is_diseased": true,
"max_confidence": 0.85,
"num_detections": 2,
"image_width": 640,
"image_height": 480,
"threshold_conf": 0.25,
"boxes": [
{
"cls": "diseased",
"conf": 0.85,
"x1": 100,
"y1": 150,
"x2": 200,
"y2": 250
}
],
"annotated_image_base64": null,
"prediction_text": "Lá cải có dấu hiệu bị nhiễm bệnh đốm lá với các vùng vàng úa và đốm nâu."
}import requests
url = "http://127.0.0.1:8000/predict"
params = {"conf_threshold": 0.25}
with open("path/to/your/image.jpg", "rb") as f:
files = {"file": f}
response = requests.post(url, params=params, files=files)
data = response.json()
print(data)curl -X POST "http://localhost:8000/predict?conf_threshold=0.25" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@path/to/your/image.jpg"Sử dụng script có sẵn để test:
# Sửa đường dẫn ảnh trong request.py
python request.pyAPI sử dụng Google Gemini để phân tích ảnh. API key hiện tại được cấu hình trong file main.py:
api_key = 'YOUR_GEMINI_API_KEY'Lưu ý bảo mật: Trong production, nên sử dụng environment variables:
api_key = os.getenv('GEMINI_API_KEY')Mô hình YOLO được load từ ./models/best.pt. Để thay đổi model:
- Thay thế file
best.pttrong thư mụcmodels/ - Hoặc sửa đường dẫn trong
main.py:MODEL_PATH = "./models/your_new_model.pt"