您的位置:首页 > 文旅 > 美景 > mac安装opencv并在vscode中配置c++环境调试推理YOLOv8网络模型

mac安装opencv并在vscode中配置c++环境调试推理YOLOv8网络模型

2025/1/9 23:45:27 来源:https://blog.csdn.net/heromps/article/details/139984720  浏览:    关键词:mac安装opencv并在vscode中配置c++环境调试推理YOLOv8网络模型

OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它支持多平台(包括 Windows、Linux、macOS)和多种编程语言(如 C++、Python、Java),使其成为研究、开发和部署计算机视觉应用的重要工具之一。

步骤 1: 创建项目目录和必要的文件

  1. 创建项目目录并进入:

    mkdir yolov8_project
    cd yolov8_project
    mkdir build
    
  2. 创建 CMakeLists.txt 文件并添加内容:

    touch CMakeLists.txt
    

    内容如下:

    cmake_minimum_required(VERSION 3.10)
    project(yolov8)set(CMAKE_CXX_STANDARD 14)
    # Set OpenCV_DIR to the directory where OpenCVConfig.cmake is located
    set(OpenCV_DIR /usr/local/opt/opencv/lib/cmake/opencv4)# Find OpenCV package
    find_package(OpenCV REQUIRED)# Include OpenCV directories
    include_directories(${OpenCV_INCLUDE_DIRS})# Link OpenCV libraries
    add_executable(yolov8 yolov8.cpp)
    target_link_libraries(yolov8 ${OpenCV_LIBS})# Print OpenCV include directories and libraries
    message(STATUS "OpenCV include directories: ${OpenCV_INCLUDE_DIRS}")
    message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
    
  3. 创建 yolov8.cpp 文件并添加您的代码:

    touch yolov8.cpp
    

    内容如下:

    #include <iostream>
    #include <opencv2/opencv.hpp>
    #include <opencv2/dnn/all_layers.hpp>
    #include <fstream>using namespace std;
    using namespace cv;struct DCSP_RESULT {int classId;float confidence;cv::Rect box;
    };void process(cv::Mat& blob, cv::dnn::Net& net, std::vector<cv::Mat>& outputs) {net.setInput(blob);net.forward(outputs, net.getUnconnectedOutLayersNames());
    }void pre_process(cv::Mat& image, cv::Mat& blob) {cv::dnn::blobFromImage(image, blob, 1. / 255., cv::Size(640, 640), cv::Scalar(0, 0, 0), true, false);
    }void saveTensor(const std::string& filename, const cv::Mat& tensor) {std::ofstream file(filename, std::ios::binary);if (file.is_open()) {file.write(reinterpret_cast<const char*>(tensor.data), tensor.total() * tensor.elemSize());file.close();}
    }void post_process(cv::Mat& image, const std::vector<cv::Mat>& outs, float confThreshold,</

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com