Baumer高防护相机如何通过YoloV8深度学习模型实现社交距离的检测识别(python)

在这里插入图片描述

《------往期经典推荐------》

AI应用软件开发实战专栏【链接】

序号项目名称项目名称
11.工业相机 + YOLOv8 实现人物检测识别:(C#代码,UI界面版)2.工业相机 + YOLOv8 实现PCB的缺陷检测:(C#代码,UI界面版)
23.工业相机 + YOLOv8 实现动物分类识别:(C#代码,UI界面版)4.工业相机 + YOLOv8 实现螺母螺丝的分类检测:(C#代码,UI界面版)
35.工业相机 + YOLOv8 实现人脸识别检测:(C#代码,UI界面版)6.工业相机 + YOLOv8 实现睡岗检测:(C#代码,UI界面版)
47.工业相机 + YOLOv8 实现打架检测检测:(C#代码,UI界面版)8.工业相机 + YOLOv8 实现水下鱼类识别:(C#代码,UI界面版)
59.工业相机 + YOLOv8 实现实现持械检测识别:(C#代码,UI界面版)10.工业相机 + YOLOv8实现工人安全装备(安全帽、手套、马甲等)检测识别:(C#代码,UI界面版)
611.工业相机 + YOLOv8 实现卫星图像识别:(C#代码,UI界面版)12.工业相机 + YOLOv8 实现火灾检测:(C#代码,UI界面版)
713.工业相机 + YOLOv8实现无人机检测识别:(C#代码,UI界面版)14.工业相机 + YOLOv8 实现沙滩小人检测识别:(C#代码,UI界面版)
815.工业相机 + YOLOv8 实现轮船检测识别:(C#代码,UI界面版)16.工业相机 + YOLOv8 实现PCB上二维码检测识别:(C#代码,UI界面版)
917.工业相机 + YOLOv8 实现标签条码一维码的检测:(C#代码,UI界面版)18.工业相机 + YOLOv8 实现不同水果的检测识别:(C#代码,UI界面版)
1019.工业相机 + YOLOv8 实现面部口罩的检测识别:(C#代码,UI界面版)20.工业相机 + YOLOv8 实现电池的检测识别:(C#代码,UI界面版)
1019.工业相机 + YOLOv8 实现面部口罩的检测识别:(C#代码,UI界面版)20.工业相机 + YOLOv8 实现电池的检测识别:(C#代码,UI界面版)
1121.工业相机 + YOLOv8 实现各种食物的类型检测识别:(C#代码,UI界面版)22.工业相机 + YOLOv8 实现裂缝的检测识别:(C#代码,UI界面版)
1223工业相机 + YOLOv8 实现汽车牌照的位置识别:(C#代码,UI界面版)24.工业相机 + YOLOv8 实现围栏羊驼的检测识别:(C#代码,UI界面版)
1325.工业相机 + YOLOv8 实现道路汽车的检测识别:(C#代码,UI界面版)26.工业相机 + YOLOv8 实现道路上头盔的检测识别:(C#代码,UI界面版)
1427.工业相机 + YOLOv8实现道路车辆事故的检测识别:(C#代码,UI界面版)28.工业相机 + YOLOv8 实现实时食物水果的检测识别:(C#代码,UI界面版)
1529.工业相机 + YOLOv8 实现各类垃圾的分类检测识别:(C#代码,UI界面版)30.工业相机 + YOLOv8 实现路口车辆速度的追踪识别:(C#代码,UI界面版)


工业相机使用YoloV8模型实现社交距离的检测识别

本项目集成了 YOLOv8 检测模型 与 C#图形界面工具,实现了包括图片、文件夹、视频与摄像头等多种输入方式的实现社交距离的检测识别。

Baumer工业相机堡盟相机是一种高性能、高质量的工业相机,可用于各种应用场景,如物体检测、计数和识别、运动分析和图像处理。

Baumer的万兆网相机拥有出色的图像处理性能,可以实时传输高分辨率图像。此外,该相机还具有快速数据传输、低功耗、易于集成以及高度可扩展性等特点。

Baumer工业相机由于其性能和质量的优越和稳定,常用于高速同步采集领域,通常使用各种图像算法来提高其捕获的图像的质量。

本文以Baumer工业相机作为案例进行演示,实现将工业相机的图像或者本地图像夹导入Yolo模型从而实现社交距离的检测识别等功能。

工业相机通过YoloV8模型实现社交距离的检测识别的技术背景

本文通过C#中实现一个简单的UI界面,用于将YoloV8模型实现社交距离的检测识别

用户可以通过该界面执行以下操作:

  1. 转换相机图像为Mat图像:通过YoloV8模型实现社交距离的检测识别

  2. 转换本地图像为mat图像:通过YoloV8模型实现社交距离的检测识别

通过这个UI界面,用户能够在实时应用机器视觉数据处理时快速有效地进行操作,无需深入了解图像数据的底层处理过程。这个简单的介绍旨在为开发人员提供一个明确的方向,以便开始构建此类应用程序,并且该程序主要用于演示目的。

在相机SDK中获取图像转换图像的代码分析

本文介绍使用Baumer工业相机,实现将图像转换为Bitmap图像,再转换Mat图像,导入到Yolo模型进行推理,输出实现社交距离的检测识别的结果。

工业相机图像转换Bitmap图像格式和Mat图像重要核心代码

//将相机内部图像内存数据转为bitmap数据
System.Drawing.Bitmap bitmap  = new System.Drawing.Bitmap((int)mBufferFilled.Width, (int)mBufferFilled.Height,(int)mBufferFilled.Width,System.Drawing.Imaging.PixelFormat.Format8bppIndexed, (IntPtr)((ulong)mBufferFilled.MemPtr + mBufferFilled.ImageOffset));
                                      
#region//Mono图像数据转换。彩色图像数据转换于此不同
System.Drawing.Imaging.ColorPalette palette = bitmap.Palette;
int nColors = 256;
for (int ix = 0; ix < nColors; ix++)
{
     uint Alpha = 0xFF;
     uint Intensity = (uint)(ix * 0xFF / (nColors - 1));
     palette.Entries[ix] = System.Drawing.Color.FromArgb((int)Alpha, (int)Intensity,(int)Intensity, (int)Intensity);
}
bitmap.Palette = palette;
#endregion

string strtime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string saveimagepath = pImgFileDir + "\\" + strtime + ".brw";
      
//使用Bitmap格式保存         
bitmap.Save(saveimagepath, System.Drawing.Imaging.ImageFormat.Bmp);  


//用bitmap转换为mat
OpenCvSharp.Mat Matgray1 = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);

本地文件图像转换Bitmap图像格式和Mat图像重要核心代码

C#环境下代码如下所示:

if (imagePaths.Count() == 0)
{
    LoadImagePaths("test_img");
}

string currentImagePath = imagePaths[currentImageIndex];

    // 显示到pictureBoxA
pictureBoxA.Image.Dispose(); // 释放上一张图片资源,避免内存泄漏
pictureBoxA.Image = new Bitmap(currentImagePath);
image_path = currentImagePath;

currentImageIndex = (currentImageIndex + 1) % imagePaths.Count;

OnNotifyShowRecieveMsg("检测中,请稍等……");
//textBox1.Text = "检测中,请稍等……";
//pictureBox2.Image = null;
Application.DoEvents();

image = new Mat(image_path);

float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);
int neww = (int)(image.Cols * ratio);
int newh = (int)(image.Rows * ratio);

Mat dstimg = new Mat();
Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));

Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);


Mat图像导入YoloV8模型重要核心代码

C#环境下代码如下所示:


// 定义 ONNX 模型的路径
string onnxModelPath = "Gesture-and-Fingertip_detection.onnx";

// 定义输入图像的形状
OpenCvSharp.Size inputShape = new OpenCvSharp.Size(640, 640);
// 从 ONNX 模型文件加载网络
if(net==null)
    net = CvDnn.ReadNetFromOnnx(onnxModelPath);

            
string[] modelClassify = {"Fingertip"};

if (imagePaths.Count() == 0)
{
    LoadImagePaths("test_img");
}

string currentImagePath = imagePaths[currentImageIndex];

    // 显示到pictureBoxA
pictureBoxA.Image.Dispose(); // 释放上一张图片资源,避免内存泄漏
pictureBoxA.Image = new Bitmap(currentImagePath);
image_path = currentImagePath;

if (pictureBoxA.Image == null)
{
    return;
}
currentImageIndex = (currentImageIndex + 1) % imagePaths.Count;

OnNotifyShowRecieveMsg("检测中,请稍等……");
            
Application.DoEvents();

image = new Mat(image_path);

dt1 = DateTime.Now;
// 调用识别图像的函数,并传入图像路径、阈值、网络、输入形状和分类类别列表
//result_image = Recognize(image, 0.35, net, inputShape, modelClassify);
result_image = RecognizeMat(image, 0.35, net, inputShape, modelClassify);
// 获取计算结束时间
dt2 = DateTime.Now;
// 显示输出的图像
pictureBoxA.Image = new Bitmap(result_image.ToMemoryStream());
         
// 显示推理耗时时间
OnNotifyShowRecieveMsg("推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms");



static Mat RecognizeMat(Mat imgInput, double threshold, Net net, OpenCvSharp.Size inputShape, string[] modelClassify)
{
    using (Mat img = imgInput)
    {

        int inpHeight = inputShape.Height; // 输入图像的高度
        int inpWidth = inputShape.Width; // 输入图像的宽度

        // 对图像进行预处理,调整尺寸
        Mat image = img;
        float ratio = Math.Min(1.0f * inpHeight / image.Rows, 1.0f * inpWidth / image.Cols);
        int neww = (int)(image.Cols * ratio);
        int newh = (int)(image.Rows * ratio);

        //// 将图像调整为模型需要的大小
        //Mat dstimg = new Mat();
        //Cv2.Resize(image, dstimg, new OpenCvSharp.Size(neww, newh));
        //Cv2.CopyMakeBorder(dstimg, dstimg, 0, inpHeight - newh, 0, inpWidth - neww, BorderTypes.Constant);
        //Mat BN_image = CvDnn.BlobFromImage(dstimg); // 将调整后的图像转换为Blob格式

        //// 配置图片输入数据 // 将 blob 设置为网络的输入
        //net.SetInput(BN_image);

        //// 从图像生成用于网络输入的 blob
        //Mat blob = CvDnn.BlobFromImage(img, 1 / 255.0, inputShape, new Scalar(0, 0, 0), false);
        ////Mat blob = CvDnn.BlobFromImage(img, 1.0 / 255.0, inputShape, new Scalar(0, 0, 0), true, false);
        // 将 blob 设置为网络的输入
        //net.SetInput(blob);

        //// 从图像生成用于网络输入的 blob
        Mat img0 = img;



        Mat blob0 = CvDnn.BlobFromImage(img0, 1 / 255.0, new OpenCvSharp.Size(inputShape.Width, inputShape.Height), swapRB: true, crop: false);
        net.SetInput(blob0);


        // 执行前向传播获取输出
        Mat output = net.Forward();
        // 此处可能需要根据 C# 中 OpenCV 的特性来处理转置操作
        output = ReshapeAndTranspose(output);


        // 获取图像的行数(高度)
        int height = img.Height;
        // 获取图像的列数(宽度)
        int width = img.Width;
        // 计算宽度的缩放因子
        double xFactor = (double)width / inputShape.Width;
        // 计算高度的缩放因子
        double yFactor = (double)height / inputShape.Height;

        // 初始化分类类别、得分和检测框的列表
        List<string> classifys = new List<string>();
        List<float> scores = new List<float>();
        List<Rect> boxes = new List<Rect>();

        List<Double> maxVales = new List<Double>();
        List<OpenCvSharp.Point> maxloces = new List<OpenCvSharp.Point>();

        // 遍历输出的行
        for (int i = 0; i < output.Rows; i++)
        {
            // 获取当前行的检测框数据
            using (Mat box = output.Row(i))
            {

                // 在框数据的特定范围中找到最小值、最大值及其位置
                OpenCvSharp.Point minloc, maxloc;
                double minVal, maxVal;
                // Mat classes_scores = box.ColRange(4, 5);//GetArray(i, 5, classes_scores);

                // double curmates0 = box.At<float>(0);
                double curmates1 = box.At<float>(4);
                int collength = box.Cols;
                int rowlength = box.Rows;


                Mat curmates = box.ColRange(4, box.Cols);
                //Cv2.MinMaxLoc(box.ColRange(4, box.Cols), out minVal, out maxVal, out minloc, out maxloc);
                Cv2.MinMaxLoc(box.ColRange(4, box.Cols), out minVal, out maxVal, out minloc, out maxloc);
                int classId = maxloc.Y;

                if (classId == 0)
                {
                    // 获取对应类别的得分                         
                    float score = (float)maxVal;

                    // 如果得分大于阈值
                    if (score > threshold)
                    {
                        // 将得分添加到得分列表
                        scores.Add(score);
                        // 将类别添加到类别列表
                        classifys.Add(modelClassify[classId]);

                        // 获取框的原始坐标
                        float x = box.At<float>(0, 0);
                        float y = box.At<float>(0, 1);
                        float w = box.At<float>(0, 2);
                        float h = box.At<float>(0, 3);


                        // 计算调整后的坐标
                        int xInt = (int)((x - 0.5 * w) * xFactor);
                        int yInt = (int)((y - 0.5 * h) * yFactor);
                        int wInt = (int)(w * xFactor);
                        int hInt = (int)(h * yFactor);
                        // 将调整后的框坐标添加到框列表
                        boxes.Add(new Rect(xInt, yInt, wInt, hInt));
                    }
                }

                   

            }
        }






        // 执行非极大值抑制操作
        int[] indices;
        CvDnn.NMSBoxes(boxes, scores, 0.25f, 0.45f, out indices);
        // 遍历非极大值抑制操作后的索引
        foreach (int i in indices)
        {
            // 获取对应的类别、得分和框
            string classify = classifys[i];
            float score = scores[i];
            Rect box = boxes[i];

            // 获取框的坐标和尺寸
            // 在图像上绘制矩形框
            Cv2.Rectangle(img, box, new Scalar(0, 255, 0), 3);
            // 生成类别和得分的标签文本
            string label = $"{classify}: {score:F2}";
            // 在图像上添加标签文本
            Cv2.PutText(img, label, new OpenCvSharp.Point(box.X, box.Y - 10), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 255, 0), 2);
        }
        // 将图像复制输出返回
        Mat result_image0 = img.Clone();
        return result_image0;
        // 将处理后的图像保存为文件
        // Cv2.ImWrite("result.jpg", img);
    }
}

下面是detection.py的代码

# import the necessary packages
from .config import NMS_THRESH, MIN_CONF, People_Counter
import numpy as np
import cv2

def detect_people(frame, net, ln, personIdx=0):
	# grab the dimensions of the frame and  initialize the list of
	# results
	(H, W) = frame.shape[:2]
	results = []

	# construct a blob from the input frame and then perform a forward
	# pass of the YOLO object detector, giving us our bounding boxes
	# and associated probabilities
	blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),
		swapRB=True, crop=False)
	net.setInput(blob)
	layerOutputs = net.forward(ln)

	# initialize our lists of detected bounding boxes, centroids, and
	# confidences, respectively
	boxes = []
	centroids = []
	confidences = []

	# loop over each of the layer outputs
	for output in layerOutputs:
		# loop over each of the detections
		for detection in output:
			# extract the class ID and confidence (i.e., probability)
			# of the current object detection
			scores = detection[5:]
			classID = np.argmax(scores)
			confidence = scores[classID]

			# filter detections by (1) ensuring that the object
			# detected was a person and (2) that the minimum
			# confidence is met
			if classID == personIdx and confidence > MIN_CONF:
				# scale the bounding box coordinates back relative to
				# the size of the image, keeping in mind that YOLO
				# actually returns the center (x, y)-coordinates of
				# the bounding box followed by the boxes' width and
				# height
				box = detection[0:4] * np.array([W, H, W, H])
				(centerX, centerY, width, height) = box.astype("int")

				# use the center (x, y)-coordinates to derive the top
				# and and left corner of the bounding box
				x = int(centerX - (width / 2))
				y = int(centerY - (height / 2))

				# update our list of bounding box coordinates,
				# centroids, and confidences
				boxes.append([x, y, int(width), int(height)])
				centroids.append((centerX, centerY))
				confidences.append(float(confidence))

	# apply non-maxima suppression to suppress weak, overlapping
	# bounding boxes
	idxs = cv2.dnn.NMSBoxes(boxes, confidences, MIN_CONF, NMS_THRESH)
	
	# ensure at least one detection exists
	if len(idxs) > 0:
		# loop over the indexes we are keeping
		for i in idxs.flatten():
			# extract the bounding box coordinates
			(x, y) = (boxes[i][0], boxes[i][1])
			(w, h) = (boxes[i][2], boxes[i][3])

			# update our results list to consist of the person
			# prediction probability, bounding box coordinates,
			# and the centroid
			r = (confidences[i], (x, y, x + w, y + h), centroids[i])
			results.append(r)

	# return the list of results
	return results


代码实现演示(实现社交距离的检测识别)

在这里插入图片描述

源码下载链接

Baumer高防护相机如何通过YoloV8深度学习模型实现社交距离的检测识别(python) 源码

工业相机通过YoloV8模型实现社交距离的检测识别的行业应用

工业相机 + YOLOv8 实现「社交距离检测识别」的 6 大行业应用速览
(信息均来自 2024-2025 年公开项目,含源码、数据集)

场景业务痛点工业相机/硬件形态技术要点现场效果 & 一键资源
① 工厂车间设备维护、质检时人员过近碰撞风险4K 全局快门 30 fps + 顶装YOLOv8 + 像素-物理转换公式报警距离误差 < 10 cm,GitHub 源码
② 物流仓库叉车通道人车混行双目 5 MP 工业相机YOLOv8-seg + 深度图30 ms/帧,误报 < 1 %,CSDN 案例
③ 公交站台候车乘客密集8 K 线阵相机 20 kHzYOLOv8 + DeepSort 轨迹实时热力图,阿里云方案
④ 地铁站安检安检通道排队过近防爆 2 MP 云台相机改进 YOLOv8(BiFPN+Attention)识别率 96 %,专利源码
⑤ 智慧园区疫情期间人员管控广角 4K + 边缘 GPUYOLOv8 + 距离阈值联动门禁无需人工巡检,博客园完整系统
⑥ 教学/科研低成本算法验证USB3.0 120 fps 相机PyQt5 GUI + YOLOv8CPU 80 ms,一键切换模型

关键技术细节

距离公式:实际距离 = (像素距离 × 标定板物理尺寸) / (像素标定长度)
模型:YOLOv8n/s/x + 注意力/轻量化,INT8 量化 3-5 MB
部署:Jetson Orin Nano 15-25 ms/帧;x86 IPC + RTX 3060 8 路并发 <30 W
合规:原始视频 72 h 自动覆盖,仅留事件 JSON,符合《个人信息保护法》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

格林威

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值