This project is a computer vision system that detects and classifies hazelnuts in real time using a camera feed. It leverages Roboflow Inference and OpenCV, and it is organized into modular components so the detection, classification, and decision logic can be maintained independently.
The repository is built around a clean, layered architecture with clearly separated responsibilities:
- Stores API keys, model identifiers, environment flags, and operational thresholds.
- Supports both
desktopandraspberry_pimodes with device-specific settings.
HazelnutDetectordetects hazelnut locations in the input image.- Uses a Roboflow detection endpoint to return the highest-confidence bounding box.
- Outputs a normalized bounding box and a confidence score.
HazelnutClassifierclassifies the cropped hazelnut image.- Preprocesses the crop for the classification model and sends it to Roboflow.
- Returns top prediction, all class confidences, and raw model output.
DecisionEnginevalidates both detection and classification results.- Detection validation checks bounding box size, aspect ratio, and confidence.
- Classification validation checks prediction confidence and top-1/top-2 margin.
- Produces a final decision of either a valid hazelnut class or
invalid.
- Runs the main camera loop.
- Captures frames, calls the detector, crops the result, classifies the crop, and applies decision logic.
- Displays bounding boxes and live status text using OpenCV.
- Contains reusable image preprocessing utilities such as
prepare_for_type_model. - Keeps the model pipelines consistent by centralizing resize, padding, and normalization logic.
Below are sample outputs from the pipeline, using the images stored in result_images/.
Example UI overlay showing detected hazelnut type.
Another run showing the system's live classification feedback.
Final example illustrating the real-time bounding box and decision visualization.
Updated result showing the latest detection and classification output.
The detection and classification models were trained using Roboflow with a complete pipeline that includes dataset preparation, annotation, augmentation, and deployment.
- Dataset contains hazelnut images across lighting and orientation variations.
- Bounding boxes were annotated in Roboflow.
- Data augmentation included rotations, flips, brightness changes, and zoom.
- Detection model is trained for object localization.
- Classification model is trained on cropped hazelnut images.
- Models are deployed through Roboflow Inference.
- The Python pipeline sends images directly to the API for real-time inference.
- Python 3
- Roboflow Inference
- OpenCV
- Optional UI support via
ui_camera.pyandui_batch.py
- Clone the repository:
git clone <repository_url> cd detection_hazelnut
- Install dependencies:
pip install -r requirements.txt
- Run the application:
python main.py
Set MODE in config.py:
MODE = "desktop"for desktop usage.MODE = "raspberry_pi"for low-power hardware.
- Replace the placeholder Roboflow API key in
config.pywith your own key for inference to work. - Press
ESCorQto close the live display. result_images/contains example output screenshots and sample results.