ultralytics / ultralytics

NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
27.56k stars 5.49k forks source link

Error after running make and ./Yolov8CPPInference in C++ #14406

Open Builder-Byte opened 1 month ago

Builder-Byte commented 1 month ago

Search before asking

YOLOv8 Component

Other

Bug

error after executing make https://github.com/ultralytics/ultralytics/tree/main/examples/YOLOv8-CPP-Inference voldy@Divyansh:~/ultralytics/examples/YOLOv8-CPP-Inference/build$ ./Yolov8CPPInference

terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.5.4) ./modules/dnn/src/onnx/onnx_importer.cpp:739: error: (-2:Unspecified error) in function 'handleNode'

Node [Add]:(/model.22/Add_output_0) parse error: OpenCV(4.5.4) ./modules/dnn/src/onnx/onnx_importer.cpp:1067: error: (-215:Assertion failed) blob_0.size == blob_1.size in function 'parseBias'

Aborted

Environment

Ultralytics YOLOv8.2.54 🚀 Python-3.10.12 torch-2.3.1+cu121 CPU (Intel Core(TM) Ultra 5 125H) Setup complete ✅ (18 CPUs, 7.6 GB RAM, 64.7/1006.9 GB disk)

OS Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.35 Environment Linux Python 3.10.12 Install pip RAM 7.57 GB CPU Intel Core(TM) Ultra 5 125H CUDA None

numpy ✅ 1.23.5<2.0.0,>=1.23.0 matplotlib ✅ 3.9.1>=3.3.0 opencv-python ✅ 4.10.0.84>=4.6.0 pillow ✅ 10.4.0>=7.1.2 pyyaml ✅ 6.0.1>=5.3.1 requests ✅ 2.32.3>=2.23.0 scipy ✅ 1.14.0>=1.4.1 torch ✅ 2.3.1>=1.8.0 torchvision ✅ 0.18.1>=0.9.0 tqdm ✅ 4.66.4>=4.64.0 psutil ✅ 6.0.0 py-cpuinfo ✅ 9.0.0 pandas ✅ 2.2.2>=1.1.4 seaborn ✅ 0.13.2>=0.11.0 ultralytics-thop ✅ 2.0.0>=2.0.0

Minimal Reproducible Example

include

include

include

include <opencv2/opencv.hpp>

include "inference.h"

using namespace std; using namespace cv;

int main(int argc, char **argv) { std::string projectBasePath = "/home/voldy/ultralytics"; // Set your ultralytics base path bool runOnGPU = false;

// Inference with yolov8 (ONNX)
Inference inf(projectBasePath + "/yolov8s.onnx", cv::Size(640, 480), "classes.txt", runOnGPU);

std::vector<std::string> imageNames;
imageNames.push_back(projectBasePath + "/ultralytics/assets/dog.jpg");
// imageNames.push_back(projectBasePath + "/ultralytics/assets/zidane.jpg");

for (int i = 0; i < imageNames.size(); ++i)
{
    cv::Mat frame = cv::imread(imageNames[i]);
    if (frame.empty())
    {
        std::cerr << "Error: Could not read image " << imageNames[i] << std::endl;
        continue;
    }

    // Inference starts here...
    std::vector<Detection> output = inf.runInference(frame);

    int detections = output.size();
    std::cout << "Number of detections:" << detections << std::endl;

    for (int j = 0; j < detections; ++j)
    {
        Detection detection = output[j];

        cv::Rect box = detection.box;
        cv::Scalar color = detection.color;

        // Detection box
        cv::rectangle(frame, box, color, 2);

        // Detection box text
        std::string classString = detection.className + ' ' + std::to_string(detection.confidence).substr(0, 4);
        cv::Size textSize = cv::getTextSize(classString, cv::FONT_HERSHEY_DUPLEX, 1, 2, 0);
        cv::Rect textBox(box.x, box.y - 40, textSize.width + 10, textSize.height + 20);

        cv::rectangle(frame, textBox, color, cv::FILLED);
        cv::putText(frame, classString, cv::Point(box.x + 5, box.y - 10), cv::FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 0, 0), 2, 0);
    }
    // Inference ends here...

    // Resize for display
    float scale = 0.8;
    cv::resize(frame, frame, cv::Size(frame.cols * scale, frame.rows * scale));

    // Display the frame with detections
    cv::imshow("Inference", frame);
    cv::waitKey(-1);
}

return 0;

}

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 1 month ago

👋 Hello @Builder-Byte, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Y-T-G commented 1 month ago

Probably need to update your OpenCV version

glenn-jocher commented 1 month ago

Thank you for your suggestion! Updating OpenCV can indeed resolve compatibility issues. To update OpenCV, you can use the following command:

pip install --upgrade opencv-python

Additionally, ensure that your ONNX model is exported correctly. You can use the following Python script to export your YOLOv8 model to ONNX format:

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8s.pt")

# Export the model to ONNX format
model.export(format="onnx", opset=12, simplify=True)

If the issue persists, please provide a minimal reproducible example to help us diagnose the problem more effectively. You can refer to our Minimum Reproducible Example guide for more details.

Feel free to reach out if you have any further questions! 😊

github-actions[bot] commented 1 week ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐