-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathimage_inference.cpp
103 lines (89 loc) · 4.43 KB
/
image_inference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file image_inference.cpp
* @brief Object detection in a static image using YOLO models (v5, v7, v8, v10).
*
* This file implements an object detection application that utilizes YOLO
* (You Only Look Once) models, specifically versions 5, 7, 8, and 10.
* The application loads a specified image, processes it to detect objects,
* and displays the results with bounding boxes around detected objects.
*
* The application supports the following functionality:
* - Loading a specified image from disk.
* - Initializing the YOLO detector with the desired model and labels.
* - Detecting objects within the image.
* - Drawing bounding boxes around detected objects and displaying the result.
*
* Configuration parameters can be adjusted to suit specific requirements:
* - `isGPU`: Set to true to enable GPU processing for improved performance;
* set to false for CPU processing.
* - `labelsPath`: Path to the class labels file (e.g., COCO dataset).
* - `imagePath`: Path to the image file to be processed (e.g., dogs.jpg).
* - `modelPath`: Path to the desired YOLO model file (e.g., ONNX format).
*
* The application can be extended to use different YOLO versions by modifying
* the model path and the corresponding detector class.
*
* Usage Instructions:
* 1. Compile the application with the necessary OpenCV and YOLO dependencies.
* 2. Ensure that the specified image and model files are present in the
* provided paths.
* 3. Run the executable to initiate the object detection process.
*
* @note The code includes commented-out sections to demonstrate how to switch
* between different YOLO models and image inputs.
*
* Author: Abdalrahman M. Amer, www.linkedin.com/in/abdalrahman-m-amer
* Date: 29.09.2024
*/
// Include necessary headers
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <string>
// #include "YOLO5.hpp" // Uncomment for YOLOv5
// #include "YOLO7.hpp" // Uncomment for YOLOv7
// #include "YOLO8.hpp" // Uncomment for YOLOv8
// #include "YOLO10.hpp" // Uncomment for YOLOv10
#include "YOLO11.hpp" // Uncomment for YOLOv11
int main()
{
// Paths to the model, labels, and test image
const std::string labelsPath = "../models/coco.names";
const std::string imagePath = "../data/dogs.jpg"; // Primary image path
// Uncomment the desired image path for testing
// const std::string imagePath = "../data/happy_dogs.jpg"; // Alternate image
// const std::string imagePath = "../data/desk.jpg"; // Another alternate image
// Model paths for different YOLO versions
// Uncomment the desired model path for testing
// const std::string modelPath = "../models/yolo5-n6.onnx"; // YOLOv5
// const std::string modelPath = "../models/yolo7-tiny.onnx"; // YOLOv7
// const std::string modelPath = "../models/yolo8n.onnx"; // YOLOv8
// const std::string modelPath = "../quantized_models/yolo10n_uint8.onnx"; // Quantized YOLOv10
const std::string modelPath = "../models/yolo11n.onnx"; // YOLOv11
// Initialize the YOLO detector with the chosen model and labels
bool isGPU = true; // Set to false for CPU processing
// YOLO7Detector detector(modelPath, labelsPath, isGPU);
// YOLO5Detector detector(modelPath, labelsPath, isGPU); // Uncomment for YOLOv5
// YOLO8Detector detector(modelPath, labelsPath, isGPU); // Uncomment for YOLOv8
// YOLO10Detector detector(modelPath, labelsPath, isGPU); // Uncomment for YOLOv10
YOLO11Detector detector(modelPath, labelsPath, isGPU); // Uncomment for YOLOv11
// Load an image
cv::Mat image = cv::imread(imagePath);
if (image.empty())
{
std::cerr << "Error: Could not open or find the image!\n";
return -1;
}
// Detect objects in the image and measure execution time
auto start = std::chrono::high_resolution_clock::now();
std::vector<Detection> results = detector.detect(image);
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start);
std::cout << "Detection completed in: " << duration.count() << " ms" << std::endl;
// Draw bounding boxes on the image
detector.drawBoundingBox(image, results); // simple bbox drawing
// detector.drawBoundingBoxMask(image, results); // Uncomment for mask drawing
// Display the image
cv::imshow("Detections", image);
cv::waitKey(0); // Wait for a key press to close the window
return 0;
}