Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(example)
add_subdirectory(yolov8-cls-inference)
13 changes: 13 additions & 0 deletions app/yolov8-cls-inference/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.5)

project(yolov8-cls-inference)

find_package(OpenCV REQUIRED)

set(PROJECT_SOURCE_FILES
main.cpp
)

add_executable(${CMAKE_PROJECT_NAME} ${PROJECT_SOURCE_FILES})
target_link_libraries(${CMAKE_PROJECT_NAME} ${OpenCV_LIBS})
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS})
29 changes: 29 additions & 0 deletions app/yolov8-cls-inference/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

## YOLOv8-cls inference

1. OpenCV installation

[Get started with OpenCV](https://opencv.org/get-started/)

2. Ultralytics installation

Create and activate python venv
```
python -m venv <venv dir>
source <venv dir>/bin/activate
```
Install ultralytics using pip
```
pip install ultralytics
```

3. Export YOLOv8-cls model
```
python export.py
```

4. Run the project
```
make -C build
build/yolov8-cls-inference <model path> <class list path> <image path>
```
13 changes: 13 additions & 0 deletions app/yolov8-cls-inference/export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

from ultralytics import YOLO

# load the YOLOv8-cls model
model = YOLO("yolov8n-cls.pt")

# export the classification list
with open('classification_list.txt', 'w') as file:
for name in model.names.values():
file.write(name + '\n')

# export the model to ONNX format
model.export(format="onnx", imgsz=224)
68 changes: 68 additions & 0 deletions app/yolov8-cls-inference/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

#include <fstream>
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/types.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <string>
#include <vector>

int main(int argc, char** argv) {
if (argc < 4) {
std::cerr << "[fatal error] Not all required parameters are specified\n";
return 1;
}

// read an input image
cv::Mat raw_image = cv::imread(argv[3]);
if (raw_image.empty()) {
std::cerr << "[fatal error] Failed to read the input image\n";
return 1;
}

// read the classification list
std::ifstream file(argv[2]);

if (!file.is_open()) {
std::cerr << "[fatal error] Failed to open the classification list file\n";
return 1;
}

std::string line;
std::vector<std::string> class_list;
while (std::getline(file, line)) {
class_list.push_back(line);
}

// read the network model
cv::dnn::Net net = cv::dnn::readNetFromONNX(argv[1]);
if (net.empty()) {
std::cerr << "[fatal error] Failed to read the network model\n";
return 1;
}

// prepare the image for inference
cv::Mat input_image;
cv::resize(raw_image, input_image, cv::Size(224, 224), cv::INTER_LINEAR);
cv::Mat blob = cv::dnn::blobFromImage(input_image, 1.0 / 255.0,
cv::Size(224, 224), cv::Scalar());

// inference
net.setInput(blob);
cv::Mat output = net.forward("output0");

// extract a result from the output
double max_class_score = 0.0;
cv::Point max_loc;
cv::minMaxLoc(output, nullptr, &max_class_score, nullptr, &max_loc);

// print the results
std::cout << "class ID: " << max_loc.x << '\n';
std::cout << "class name: " << class_list[max_loc.x] << '\n';
std::cout << "confidence: " << max_class_score << '\n';

return 0;
}
Loading