C++/Yolov8人体特征识别 广场室内 人数统计

简介: 这篇博客针对<<C++/Yolov8人体特征识别 广场室内 人数统计>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。

程序示例精选

C++/Yolov8人体特征识别 广场室内 人数统计

如需安装运行环境或远程调试,可点击右边主头像昵称进入个人主页查看博主联系方式,由专业技术人员远程协助!


前言

这篇博客针对<<C++/Yolov8人体特征识别 广场室内 人数统计>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


文章目录

一、所需工具软件

二、使用步骤

       1. 引入库

       2. 识别图像特征

       3. 参数设置

       4. 运行结果

三、在线协助

一、所需工具软件

1. VS2019, C++

2. Yolov8, OpenCV

二、使用步骤

1.引入库

代码如下(示例):

#include <iostream>
#include<opencv2/opencv.hpp>
#include<math.h>
#include "yolov8_onnx.h"
#include<time.h>
using namespace std;
using namespace cv;
using namespace dnn;

image.gif

2.识别图像特征

代码如下(示例):

bool CheckParams(int netHeight, int netWidth, const int* netStride, int strideSize) {
  if (netHeight % netStride[strideSize - 1] != 0 || netWidth % netStride[strideSize - 1] != 0)
  {
    cout << "Error:_netHeight and _netWidth must be multiple of max stride " << netStride[strideSize - 1] << "!" << endl;
    return false;
  }
  return true;
}
void LetterBox(const cv::Mat& image, cv::Mat& outImage, cv::Vec4d& params, const cv::Size& newShape,
  bool autoShape, bool scaleFill, bool scaleUp, int stride, const cv::Scalar& color)
{
  if (false) {
    int maxLen = MAX(image.rows, image.cols);
    outImage = Mat::zeros(Size(maxLen, maxLen), CV_8UC3);
    image.copyTo(outImage(Rect(0, 0, image.cols, image.rows)));
    params[0] = 1;
    params[1] = 1;
    params[3] = 0;
    params[2] = 0;
  }
  cv::Size shape = image.size();
  float r = std::min((float)newShape.height / (float)shape.height,
    (float)newShape.width / (float)shape.width);
  if (!scaleUp)
    r = std::min(r, 1.0f);
  float ratio[2]{ r, r };
  int new_un_pad[2] = { (int)std::round((float)shape.width * r),(int)std::round((float)shape.height * r) };
  auto dw = (float)(newShape.width - new_un_pad[0]);
  auto dh = (float)(newShape.height - new_un_pad[1]);
  if (autoShape)
  {
    dw = (float)((int)dw % stride);
    dh = (float)((int)dh % stride);
  }
  else if (scaleFill)
  {
    dw = 0.0f;
    dh = 0.0f;
    new_un_pad[0] = newShape.width;
    new_un_pad[1] = newShape.height;
    ratio[0] = (float)newShape.width / (float)shape.width;
    ratio[1] = (float)newShape.height / (float)shape.height;
  }
  dw /= 2.0f;
  dh /= 2.0f;
  if (shape.width != new_un_pad[0] && shape.height != new_un_pad[1])
  {
    cv::resize(image, outImage, cv::Size(new_un_pad[0], new_un_pad[1]));
  }
  else {
    outImage = image.clone();
  }
  int top = int(std::round(dh - 0.1f));
  int bottom = int(std::round(dh + 0.1f));
  int left = int(std::round(dw - 0.1f));
  int right = int(std::round(dw + 0.1f));
  params[0] = ratio[0];
  params[1] = ratio[1];
  params[2] = left;
  params[3] = top;
  cv::copyMakeBorder(outImage, outImage, top, bottom, left, right, cv::BORDER_CONSTANT, color);
}
void GetMask(const cv::Mat& maskProposals, const cv::Mat& maskProtos, std::vector<OutputSeg>& output, const MaskParams& maskParams) {
  //cout << maskProtos.size << endl;
  int seg_channels = maskParams.segChannels;
  int net_width = maskParams.netWidth;
  int seg_width = maskParams.segWidth;
  int net_height = maskParams.netHeight;
  int seg_height = maskParams.segHeight;
  float mask_threshold = maskParams.maskThreshold;
  Vec4f params = maskParams.params;
  Size src_img_shape = maskParams.srcImgShape;
  Mat protos = maskProtos.reshape(0, { seg_channels,seg_width * seg_height });
  Mat matmul_res = (maskProposals * protos).t();
  Mat masks = matmul_res.reshape(output.size(), { seg_width,seg_height });
  vector<Mat> maskChannels;
  split(masks, maskChannels);
  for (int i = 0; i < output.size(); ++i) {
    Mat dest, mask;
    //sigmoid
    cv::exp(-maskChannels[i], dest);
    dest = 1.0 / (1.0 + dest);
    Rect roi(int(params[2] / net_width * seg_width), int(params[3] / net_height * seg_height), int(seg_width - params[2] / 2), int(seg_height - params[3] / 2));
    dest = dest(roi);
    resize(dest, mask, src_img_shape, INTER_NEAREST);
    //crop
    Rect temp_rect = output[i].box;
    mask = mask(temp_rect) > mask_threshold;
    output[i].boxMask = mask;
  }
}
void GetMask2(const Mat& maskProposals, const Mat& mask_protos, OutputSeg& output, const MaskParams& maskParams) {
  int seg_channels = maskParams.segChannels;
  int net_width = maskParams.netWidth;
  int seg_width = maskParams.segWidth;
  int net_height = maskParams.netHeight;
  int seg_height = maskParams.segHeight;
  float mask_threshold = maskParams.maskThreshold;
  Vec4f params = maskParams.params;
  Size src_img_shape = maskParams.srcImgShape;
  Rect temp_rect = output.box;
  //crop from mask_protos
  int rang_x = floor((temp_rect.x * params[0] + params[2]) / net_width * seg_width);
  int rang_y = floor((temp_rect.y * params[1] + params[3]) / net_height * seg_height);
  int rang_w = ceil(((temp_rect.x + temp_rect.width) * params[0] + params[2]) / net_width * seg_width) - rang_x;
  int rang_h = ceil(((temp_rect.y + temp_rect.height) * params[1] + params[3]) / net_height * seg_height) - rang_y;
  //如果下面的 mask_protos(roi_rangs).clone()位置报错,说明你的output.box数据不对,或者矩形框就1个像素的,开启下面的注释部分防止报错。
  rang_w = MAX(rang_w, 1);
  rang_h = MAX(rang_h, 1);
  if (rang_x + rang_w > seg_width) {
    if (seg_width - rang_x > 0)
      rang_w = seg_width - rang_x;
    else
      rang_x -= 1;
  }
  if (rang_y + rang_h > seg_height) {
    if (seg_height - rang_y > 0)
      rang_h = seg_height - rang_y;
    else
      rang_y -= 1;
  }
  vector<Range> roi_rangs;
  roi_rangs.push_back(Range(0, 1));
  roi_rangs.push_back(Range::all());
  roi_rangs.push_back(Range(rang_y, rang_h + rang_y));
  roi_rangs.push_back(Range(rang_x, rang_w + rang_x));
  //crop
  Mat temp_mask_protos = mask_protos(roi_rangs).clone();
  Mat protos = temp_mask_protos.reshape(0, { seg_channels,rang_w * rang_h });
  Mat matmul_res = (maskProposals * protos).t();
  Mat masks_feature = matmul_res.reshape(1, { rang_h,rang_w });
  Mat dest, mask;
  //sigmoid
  cv::exp(-masks_feature, dest);
  dest = 1.0 / (1.0 + dest);
  int left = floor((net_width / seg_width * rang_x - params[2]) / params[0]);
  int top = floor((net_height / seg_height * rang_y - params[3]) / params[1]);
  int width = ceil(net_width / seg_width * rang_w / params[0]);
  int height = ceil(net_height / seg_height * rang_h / params[1]);
  resize(dest, mask, Size(width, height), INTER_NEAREST);
  mask = mask(temp_rect - Point(left, top)) > mask_threshold;
  output.boxMask = mask;
}
void DrawPred(Mat& img, vector<OutputSeg> result, std::vector<std::string> classNames, vector<Scalar> color) {
  Mat mask = img.clone();
  for (int i = 0; i < result.size(); i++) {
    int left, top;
    left = result[i].box.x;
    top = result[i].box.y;
    int color_num = i;
    rectangle(img, result[i].box, color[result[i].id], 2, 8);
    if(result[i].boxMask.rows&& result[i].boxMask.cols>0)
      mask(result[i].box).setTo(color[result[i].id], result[i].boxMask);
    //string label = classNames[result[i].id] + ":" + to_string(result[i].confidence);
    std::ostringstream oss;
    oss << round(10 / result[i].confidence * 10) / 10.0;
    std::cout << oss.str() << std::endl;
    //string label = classNames[result[i].id] + ":" + oss.str();
    string label = classNames[result[i].id];
    int baseLine;
    Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.8, 1, &baseLine);
    top = max(top, labelSize.height);
    //rectangle(frame, Point(left, top - int(1.5 * labelSize.height)), Point(left + int(1.5 * labelSize.width), top + baseLine), Scalar(0, 255, 0), FILLED);
    putText(img, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.6, color[result[i].id], 2);
  }
  addWeighted(img, 0.5, mask, 0.5, 0, img); //add mask to src
  imshow("1", img);
  //imwrite("out.bmp", img);
  waitKey();
  //destroyAllWindows();
}

image.gif

3.参数定义

代码如下(示例):

int main() {
  //string img_path = "./data/image/aa.png";
  string img_path = "./data/image/11.jpeg";
  string detect_model_path = "yolov8n.onnx";
  Mat img = imread(img_path);
  Yolov8Onnx task_detect_onnx;
  yolov8_onnx(task_detect_onnx,img,detect_model_path);  //onnxruntime detect
  return 0;
}

image.gif

4. 运行结果如下

image.gif



三、在线协助:

如需安装运行环境或远程调试, 可点击右边 主头像 昵称 进入个人主页查看博主联系方式 ,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Qt, C++, Python入门指导
3)界面美化
4)软件制作


博主推荐文章:python人脸识别统计人数qt窗体-CSDN博客

博主推荐文章:Python Yolov5火焰烟雾识别源码分享-CSDN博客

                        Python OpenCV识别行人入口进出人数统计_python识别人数-CSDN博客

个人博客主页:alicema1111的博客_CSDN博客-Python,C++,网页领域博主

博主所有文章点这里:alicema1111的博客_CSDN博客-Python,C++,网页领域博主


相关文章
|
3月前
|
算法 测试技术 C++
【动态规划】【图论】【C++算法】1575统计所有可行路径
【动态规划】【图论】【C++算法】1575统计所有可行路径
|
3月前
|
人工智能 算法 测试技术
【动态规划】【二分查找】C++算法 466 统计重复个数
【动态规划】【二分查找】C++算法 466 统计重复个数
|
3月前
|
算法 测试技术 C#
C++二分查找:统计点对的数目
C++二分查找:统计点对的数目
|
3月前
|
存储 资源调度 算法
Opencv(C++)系列学习---SIFT、SURF、ORB算子特征检测
Opencv(C++)系列学习---SIFT、SURF、ORB算子特征检测
175 0
|
3月前
|
安全 程序员 编译器
【C/C++ 泛型编程 进阶篇 Type traits 】C++类型特征探究:编译时类型判断的艺术
【C/C++ 泛型编程 进阶篇 Type traits 】C++类型特征探究:编译时类型判断的艺术
325 1
|
26天前
|
NoSQL Redis C++
c++开发redis module问题之在复杂的Redis模块中,特别是使用第三方库或C++开发时,接管内存统计有哪些困难
c++开发redis module问题之在复杂的Redis模块中,特别是使用第三方库或C++开发时,接管内存统计有哪些困难
|
2月前
|
数据安全/隐私保护 C++
C++ 中的类是一种用户定义的数据类型,用于表示具有相似特征和行为的对象的模板。
C++ 中的类是一种用户定义的数据类型,用于表示具有相似特征和行为的对象的模板。
|
2月前
|
程序员 数据安全/隐私保护 C++
C++面向对象的四大特征
C++面向对象的四大特征
18 0
|
3月前
|
C++
41.用c++编写程序:从键盘上任意输20个1-99之间的整数,分别统计其个位数0-9的数字各有多少
41.用c++编写程序:从键盘上任意输20个1-99之间的整数,分别统计其个位数0-9的数字各有多少
34 0
|
3月前
|
算法 测试技术 C#
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II
【字典树】【KMP】【C++算法】3045统计前后缀下标对 II