Design and Implementation of a Crop Pest and Disease Detection System Using Object Detection and Large Language Models
This article explores the design and implementation of a crop pest and disease detection system that integrates object detection models with Large Language Models (LLMs). By combining visual recognition for identifying crop diseases and LLMs for generating professional treatment advice, the system forms a complete diagnostic closed-loop. It details the fine-grained disease classification system, a four-layer system architecture including visual detection, intelligent diagnosis, business services, and frontend, as well as practical deployment suggestions for scalable agricultural monitoring.
1. Background and Design Approach
Traditional pest and disease diagnosis relies on on-site agricultural technicians, leading to limited coverage, delayed responses, and high dependence on experience. This solution integrates two AI capabilities: first, a deep learning-based object detection model responsible for locating disease spots and pest areas from images and outputting disease categories; second, a Large Language Model (LLM) responsible for generating professional prevention and control advice and answering plant protection queries based on the detection results. These two chains complement each other—the visual model addresses “what it is,” while the LLM addresses “what to do,” forming a complete diagnostic closed-loop.
Regarding deployment, the system supports dual modes: offline visual inference and online LLM consultation. This approach caters to grassroots agricultural scenarios with poor network conditions as well as cloud scenarios requiring deep professional answers.
2. Detection Capability Design
The system establishes a fine-grained disease classification system for common cash crops, covering 37 categories, including:
- Fruit trees: Apple (Black rot, Scab, Healthy), Cherry (Powdery mildew), Grape (Black rot, Leaf blight), Citrus (Huanglongbing), Peach (Bacterial spot)
- Grain crops: Corn (Leaf spot, Common rust, Northern leaf blight), Rice (Brown spot, Blast), Potato (Early blight, Late blight)
- Cash and vegetable crops: Tomato (Bacterial spot, Early blight, Late blight, Leaf mold, Septoria leaf spot), Bell pepper, Cassava, Strawberry, etc.
When modeling, it is recommended to adopt a “healthy + diseased” balanced sampling strategy and use data augmentation techniques (random cropping, color perturbation, Mixup, etc.) to mitigate the shortage of disease spot samples. During the training phase, fine-tuning based on pre-trained weights can significantly reduce the requirement for labeled data volume.
3. System Architecture and Core Functions
The overall architecture is divided into four layers:
1. Visual Detection Layer
- Single image detection: Upload and inference, returning an annotated image with bounding boxes and confidence scores;
- Folder batch upload and batch inference: Suitable for centralized screening of sample libraries;
- Local video detection: Frame-by-frame decoding and inference, outputting an annotated video;
- Real-time camera detection: Pulls video streams for online monitoring, suitable for greenhouse inspections.
The inference service can be implemented using the Python ecosystem. Core example:
results = model.predict(image, conf=0.5)for box in results[0].boxes:label = model.names[int(box.cls)]confidence = float(box.conf)
2. Intelligent Diagnosis Layer
Detection results (disease category, confidence, region info) are assembled into a structured prompt and sent to the LLM to generate prevention and control advice. The content covers pathogenesis, chemical control plans, agricultural operation suggestions, and preventive measures. To control hallucination risks, it is recommended to limit the answer scope in the prompt and apply safety filtering to the generated content.
3. Business Service Layer
Uses mature backend frameworks (such as SpringBoot/Flask) combined with MySQL for data persistence, providing APIs for detection record management, user systems, and statistical aggregation. Detection statistics are rendered into visual charts via frontend chart libraries, supporting analysis by crop, disease type, and time dimension. Single or batch detections can be exported as PDF reports with one click, facilitating archiving and hierarchical reporting.
4. Permissions and Frontend Layer
The system implements hierarchical permissions: administrators can view all detection records and manage all users, while regular users can only access their own historical records. Data isolation meets multi-person collaboration scenarios. The frontend is built with Vue3 + TypeScript + Element Plus, supporting theme color switching, page layout adjustments, and component size customization to adapt to different users’ operational habits.
4. Implementation Suggestions
- Data closed-loop: Feed misdetected samples back to the training set to iteratively update model versions periodically;
- Performance optimization: Use half-precision or batch processing for batch inference; video stream detection can adopt frame skipping strategies to reduce computational overhead;
- Scalability: Adding new crop categories only requires expanding the category table and fine-tuning the model, without major changes to the business layer;
- O&M guarantee: It is recommended to integrate logging services and monitoring alerts to ensure the long-term stable operation of grassroots deployments.
This solution demonstrates the integration paradigm of “visual detection + LLM diagnosis,” which can also be migrated to similar scenarios such as forestry monitoring and warehouse quality inspection, offering good engineering promotion value.
