This script serves as an object detector based on the latest version of YOLO, specifically YOLOv5. You can find the YOLO repository here and the official YOLO documentation here.
The default configuration is tailored for apple detection, yet it's easily adaptable to any subset of YOLO detection classes. The script outputs a JSON file containing the positions of detected objects. The information is organized by image and class, providing a structure like the following:
{
"IMG_6081.JPG": {
"path": "data/test/IMG_6081.JPG",
"position": {
"apples": [[x1, y1], [x2, y2], ...],
"car": [...]
}
},
// ...
}
Additionally, in the same folder, the script saves input images with bounding boxes drawn around detected objects. Each box is labeled with the corresponding class and confidence score.
Clone the repository and install the requirements.
# Clone the repository
git clone https://github.com/DavideSangiorgi/apple_detection.git
# or
git clone git@github.com:DavideSangiorgi/apple_detection.git
# Change directory
cd apple_detection
# Install dependencies
pip install -r requirements.txt
To run the object detection script, use the following command:
python src/object_detection.py [--config CONFIG_PATH]
--config
: Path to the configuration JSON file. (Default: 'configs/default.json')
Note: The --config
argument is optional, and if not provided,
the script will use the default configuration specified in 'configs/default.json'.
- Run object detection with the default configuration:
python src/object_detection.py
- Run object detection with a custom configuration:
python src/object_detection.py --config configs/custom.json
This section outlines the configurable parameters in the configs
folder. Adjust these parameters according to your requirements.
-
YOLO_model
: Specifies the YOLO model to use for detection. Options, from smallest/less performant to heavier/most performant: "yolov5n.pt", "yolov5s.pt", "yolov5m.pt", "yolov5l.pt", "yolov5x.pt", "yolov5n6.pt", "yolov5s6.pt", "yolov5m6.pt", "yolov5l6.pt", "yolov5x6.pt". Refer to the YOLO documentation for details. -
device
: Sets the device to run the model. Options: "cpu" or "cuda:n" (replace n with the index of the available CUDA device). Check CUDA device availability withpython3 -c "import torch; print(torch.cuda.is_available())"
and the number of available devices withpython3 -c "import torch; print(torch.cuda.device_count())"
.
-
confidence_threshold
: Float between 0 and 1, setting the confidence score threshold for filtering predictions. -
iou_threshold
: Float between 0 and 1, setting the IOU threshold for filtering predictions. -
augment
: Boolean determining whether to perform augmentation during prediction, potentially improving performance. -
classes
: List of classes to detect. If set tonull
, all YOLO classes will be detected. -
box_line_width
: Line width of bounding boxes drawn on output images. If set tonull
, the value is automatically determined. -
results_path
: Output path where images with bounding boxes and the JSON file with object positions are saved. If the folder exists, results will be overwritten.