Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
1.3 kB
1
Indexable
Never
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;
}
Leave a Comment