Untitled

 avatar
unknown
plain_text
a year ago
1.3 kB
4
Indexable
int main() {
    Yolov8Onnx yolov8;

    string modelPath = "/path/to/model.onnx";
    bool isCuda = true; // or false for CPU inference
    int cudaID = 0; // optional
    bool warmUp = true; // optional

    if (!yolov8.ReadModel(modelPath, isCuda, cudaID, warmUp)) {
        std::cerr << "Failed to load model." << std::endl;
        return -1;
    }

    string imgPath = "/path/to/image.jpg";
    cv::Mat img = cv::imread(imgPath);
    if (img.empty()) {
        std::cerr << "Failed to load image." << std::endl;
        return -1;
    }

    std::vector<OutputParams> output;
    if (!yolov8.OnnxDetect(img, output)) {
        std::cerr << "Failed to detect objects." << std::endl;
        return -1;
    }

    for (const auto& obj : output) {
        cv::Rect roi = obj.box;
        cv::Mat croppedImg = img(roi);
        // Save the cropped image to a file
        string outputFolder = "cropped";
        string outputFile = "image_" + std::to_string(obj.id) + ".jpg";
        string outputPath = outputFolder + "/" + outputFile;
        if (cv::imwrite(outputPath, croppedImg)) {
            std::cout << "Image saved: " << outputPath << std::endl;
        } else {
            std::cout << "Error saving image: " << outputPath << std::endl;
        }
    }

    return 0;
}
Editor is loading...
Leave a Comment