team-6/api/READMEPlantAPI.md
Tikhon Vodyanov 231b83afb5 api folder
2025-08-02 13:32:15 +02:00

4.6 KiB

🌱 Plant Health & Identification API

An intelligent backend service built for our hackathon project. This API uses a dual-model AI system to analyze an image of a plant, first identifying its species and then assessing its health.

🚀 The Problem

Have you ever wondered what kind of plant you have or why its leaves are suddenly turning yellow? Our project aims to provide a simple, accessible answer. This repository contains the backend API that powers our application, capable of providing a comprehensive plant analysis from a single image.

🧠 The AI Pipeline

This API uses a two-stage process to analyze an image:

Species Identification: We use a custom-trained TensorFlow/Keras model, built on a MobileNetV2 architecture. This model was fine-tuned on a dataset of six specific plant types to achieve high accuracy in identifying which plant is in the image.

Health Assessment: Once the plant's species is known (e.g., "tomato"), we use OpenAI's powerful, open-source CLIP model. We dynamically generate text prompts like "a photo of a healthy tomato plant" or "a photo of a sick tomato plant with yellow spots" and ask CLIP which description best matches the image. This zero-shot approach allows for a flexible and nuanced understanding of the plant's health.

Features

Identify 6 Plant Species: Accurately distinguishes between tomato, basil, mint, lettuce, rosemary, and strawberry.

Assess 4 Health States: Classifies plants as Healthy, Diseased, Dehydrated, or Dead.

Confidence Scores: Provides confidence levels for both the species identification and the health assessment.

Simple JSON API: Easy to integrate with any frontend or mobile application.

🛠️ Tech Stack

Backend Framework: Flask

AI / Machine Learning: TensorFlow (Keras), PyTorch, OpenAI CLIP

Image Processing: Pillow, OpenCV

Core Language: Python 3.10

🔌 API Documentation

This is the documentation for the main analysis endpoint.

Analyze a Plant Image

Endpoint: /analyze

Method: POST

Body: multipart/form-data

The request must contain a file field named image.

Successful Response (Status 200 OK)

The API will return a JSON object with the analysis.

Example Response:

Generated json { "plant_species": "tomato", "identification_confidence": "97.45%", "health_status": "Healthy", "health_confidence": "89.12%", "health_breakdown": { "Healthy": 0.8912, "Diseased": 0.0562, "Dehydrated": 0.0421, "Dead": 0.0105 } }

Field Descriptions:

Key Type Description plant_species String The identified species of the plant. identification_confidence String The model's confidence in the species identification. health_status String The most likely health status of the plant. health_confidence String The model's confidence in the health assessment. health_breakdown Object A dictionary of raw probability scores for each health state. Error Responses

If the request is missing an image, the API will return:

Status: 400 Bad Request

Body: {"error": "No image file provided"}

For any other server-side issues, the API will return:

Status: 500 Internal Server Error

Body: {"error": "An internal server error occurred."}

🖥️ How to Run Locally

To run this backend server on your own machine, follow these steps.

Clone the Repository:

Generated bash git clone cd /backend IGNORE_WHEN_COPYING_START content_copy download Use code with caution. Bash IGNORE_WHEN_COPYING_END

Create a Virtual Environment:

Generated bash python3 -m venv venv source venv/bin/activate IGNORE_WHEN_COPYING_START content_copy download Use code with caution. Bash IGNORE_WHEN_COPYING_END

Install Dependencies: This can take a while as it will download TensorFlow and PyTorch.

Generated bash pip install -r requirements.txt IGNORE_WHEN_COPYING_START content_copy download Use code with caution. Bash IGNORE_WHEN_COPYING_END

Place the Model: Make sure you have the trained Keras model (BestModel.keras) inside the models/ directory.

Run the Server:

Generated bash python app.py IGNORE_WHEN_COPYING_START content_copy download Use code with caution. Bash IGNORE_WHEN_COPYING_END

The API will now be running on your local machine at http://127.0.0.1:5000.

📁 Project Structure Generated code /backend |-- app.py # The main Flask server and API logic. |-- models/ # Folder for the trained Keras model. | |-- BestModel.keras |-- requirements.txt # Python dependencies. |-- .gitignore # Files to be ignored by Git (like the venv). IGNORE_WHEN_COPYING_START content_copy download Use code with caution. IGNORE_WHEN_COPYING_END 👥 Authors

[Your Name]

[Teammate's Name]

[Teammate's Name]