#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::dnn;
int main() {
// Load model
Net net = readNet("model.onnx");
if(net.empty()) {
std::cerr << "Failed to load model\n";
return -1;
}
// Configure backend/target
net.setPreferableBackend(DNN_BACKEND_CUDA);
net.setPreferableTarget(DNN_TARGET_CUDA);
// Load and preprocess image
Mat img = imread("image.jpg");
Mat blob = blobFromImage(img, 1.0/255, Size(224, 224),
Scalar(), true, false);
// Set input
net.setInput(blob);
// Forward pass
Mat output = net.forward();
// Get performance info
std::vector<double> timings;
int64 t = net.getPerfProfile(timings);
std::cout << "Inference time: " << t / 1000.0 << " ms\n";
// Process output
Point classIdPoint;
minMaxLoc(output.reshape(1, 1), 0, 0, 0, &classIdPoint);
int classId = classIdPoint.x;
std::cout << "Predicted class: " << classId << std::endl;
return 0;
}