This project provides a Python script for real-time apple detection and classification using the YOLOv8 model. It supports both dual-camera live tracking and static image analysis, classifying apples as green, red, or rotten, and provides color analysis for further reference.
- Dual Camera Support: Simultaneous detection and classification from two camera feeds.
- YOLOv8 Integration: Utilizes Ultralytics YOLOv8 for robust object detection.
- Apple Type Classification: Distinguishes between green, red, and rotten apples.
- Color Area Analysis: Calculates the percentage of green, red, and rotten areas within detected apples.
- Real-Time Visualization: Annotates frames with detection results, confidence scores, and color breakdowns.
- Image Mode: Supports detection and analysis on static images.
- Python 3.7+
- Ultralytics YOLOv8
- OpenCV (
cv2
) - NumPy
Install dependencies with:
pip install ultralytics opencv-python numpy
By default, the script runs in camera mode. It will open two camera feeds, perform detection, and display annotated results. There are three script with diffrent purposes such as by model which directly classify them by pretrained model. Secondly, classify by color which calculate green brown and yellow areas to classify. Thirdly, single detection for single apple (it uses algorithm to give a final result according to both feed)
python yoloV8_result_by_model.py
or
python yoloV8_result_by_color_area.py
or
python yolov8_singleDetection.py
- Camera IDs:
camera1_id=0
(default)camera2_id=1
(default)
- Model Path:
- Update the
model_name
parameter in the script to point to your trained YOLOv8 weights (e.g.,train3/weights/best.pt
).
- Update the
To run detection on a single image, set use_cam = False
in the __main__
section of the script and specify the image_path
.
if __name__ == "__main__":
use_cam = False
tracker = DualCameraYOLOv8SingleObjectTracker(
camera1_id=0,
camera2_id=1,
model_name="path/to/your/best.pt",
conf_threshold=0.5,
image_path="path/to/your/image.png"
)
tracker.detect_on_image()
yoloV8_result_by_model.py
— Main script for detection and trackingREADME.md
— Project documentationtrain3/weights/best.pt
— (You must provide your own trained YOLOv8 model weights)- Example images (optional)
- Ensure your camera devices are connected and accessible by OpenCV.
- Update all file paths in the script to match your local setup.
- The script prints detailed results to the console and displays annotated images in OpenCV windows.
- Annotated video/image windows showing detected apples, their type (green, red, rotten), confidence scores, and color area breakdowns.
- Console output with detailed classification and color analysis for each detected apple.
This project is for research and educational purposes. Please cite the original YOLOv8 authors if you use this code in your work.
- Ultralytics YOLOv8
- OpenCV
- NumPy