Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bok Choy Disease Detection API

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.

📋 Mô tả

Đâ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

🛠️ Công nghệ sử dụng

  • 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

📁 Cấu trúc thư mục

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

🚀 Hướng dẫn cài đặt và chạy

Bước 1: Kiểm tra Python

Đảm bảo bạn đã cài đặt Python 3.8+ trên máy:

python --version

Bước 2: Tạo virtual environment (optional)

cd e:\Python_Programming\bokchoy-api
python -m venv venv
venv\Scripts\activate

Bước 3: Cài đặt dependencies

pip install -r requirements.txt

Bước 4: Chuẩn bị model file

Đả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.

Bước 5: Chạy API server

cd app
uvicorn main:app --host 0.0.0.0 --port 8080 --reload

Hoặc chạy từ thư mục gốc:

uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload

Server sẽ chạy tại: http://localhost:8080

Bước 6: Kiểm tra API

Mở trình duyệt và truy cập:

  • API Documentation: http://localhost:8080/docs
  • Health check: http://localhost:8080/health

📖 Sử dụng API

Endpoint chính: /predict

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."
}

Ví dụ sử dụng với Python

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)

Ví dụ sử dụng với curl

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"

🧪 Test API

Sử dụng script có sẵn để test:

# Sửa đường dẫn ảnh trong request.py
python request.py

⚙️ Cấu hình

Google Gemini API Key

API 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')

Model Configuration

Mô hình YOLO được load từ ./models/best.pt. Để thay đổi model:

  1. Thay thế file best.pt trong thư mục models/
  2. Hoặc sửa đường dẫn trong main.py:
    MODEL_PATH = "./models/your_new_model.pt"

About

API detect health for plants (bok-choy)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages