当前位置:   article > 正文

opencv C++ dnn模块调用yolov5以及Intel RealSense D435深度相机联合使用进行目标检测

opencv C++ dnn模块调用yolov5以及Intel RealSense D435深度相机联合使用进行目标检测

一、代码

  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/dnn/dnn.hpp>
  3. #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
  4. using namespace cv;
  5. using namespace dnn;
  6. using namespace std;
  7. using namespace rs2;
  8. // 类名数组,这里需要替换为实际YOLO模型所检测的对象的类名
  9. const char* classNames[] = {"object1", "object2", "object3", "object4"};
  10. int main(int argc, char** argv)
  11. {
  12. // 模型权重和配置文件路径,这些文件包含了训练好的YOLO模型参数和网络配置
  13. String model = "yolov5.onnx"; // 替换为实际模型文件路径
  14. // 加载预训练的模型和配置到DNN网络中
  15. Net net = readNetFromONNX(model);
  16. // 设置推理引擎后端为OpenCV,目标设备为CPU
  17. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  18. net.setPreferableTarget(DNN_TARGET_CPU);
  19. // Declare depth colorizer for pretty visualization of depth data
  20. colorizer color_map;
  21. // Declare RealSense pipeline, encapsulating the actual device and sensors
  22. pipeline p;
  23. // Start streaming with default recommended configuration
  24. p.start();
  25. // 循环直到用户按下键盘上的任意键
  26. while (waitKey(1) < 0) {
  27. // Wait for the next set of frames from the camera
  28. frameset frames = p.wait_for_frames();
  29. // Get a frame from the RGB camera
  30. frame color = frames.get_color_frame();
  31. // Create OpenCV matrix of size (color_height, color_width)
  32. Mat frame(Size(640, 480), CV_8UC3, (void*)color.get_data(), Mat::AUTO_STEP);
  33. Mat blob; // 用于存储处理后的图像,以适应网络输入
  34. // 将帧图像转换为网络输入所需格式
  35. blobFromImage(frame, blob, 1/255.0, cv::Size(416, 416), Scalar(0,0,0), true, false);
  36. // 将blob设置为网络的输入
  37. net.setInput(blob);
  38. // 运行前向传递以获取网络的输出层
  39. vector<Mat> outs;
  40. net.forward(outs, net.getUnconnectedOutLayersNames());
  41. // 遍历网络输出的每一层结果
  42. for (size_t i = 0; i < outs.size(); ++i) {
  43. for (int j = 0; j < outs[i].rows; ++j) {
  44. Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
  45. Point classIdPoint;
  46. double confidence;
  47. minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
  48. if (confidence > 0.5) {
  49. int centerX = (int)(outs[i].at<float>(j, 0) * frame.cols);
  50. int centerY = (int)(outs[i].at<float>(j, 1) * frame.rows);
  51. int width = (int)(outs[i].at<float>(j, 2) * frame.cols);
  52. int height = (int)(outs[i].at<float>(j, 3) * frame.rows);
  53. int left = centerX - width / 2;
  54. int top = centerY - height / 2;
  55. rectangle(frame, Rect(left, top, width, height), Scalar(0, 255, 0), 2);
  56. int classIdx = static_cast<int>(classIdPoint.x);
  57. string classLabel = string(classNames[classIdx]);
  58. string label = classLabel + ":" + format("%.2f", confidence);
  59. int baseLine;
  60. Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
  61. top = max(top, labelSize.height);
  62. rectangle(frame, Point(left, top - labelSize.height), Point(left + labelSize.width, top + baseLine), Scalar::all(255), FILLED);
  63. putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
  64. }
  65. }
  66. }
  67. // 展示处理后的帧
  68. imshow("YoloV8", frame);
  69. }
  70. return 0;
  71. }

注意:由于手头上没有该摄像头,本人只是查询资料,以及文档之后写的代码,并没有实操

二、安装包

需要安装opencv、librealsense2库

链接:Intel.RealSense.SDK.zip资源-CSDN文库

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号