namespace AIPlateformSystem
{
public partial class FrmCameraOnnxTest : Form
{
public FrmCameraOnnxTest()
{
InitializeComponent();
InitializeOpenGLControl();
}
// OpenCV 视频捕获
private VideoCapture _capture;
private bool _isRunning;
// OpenGL 组件
private GLControl _glControl;
private int _textureId;
private Size _videoSize = new Size(1920, 1080); // 默认视频尺寸
private void InitializeOpenGLControl()
{
// 创建 OpenGL 控件
_glControl = new GLControl(new GraphicsMode(32, 24, 0, 4))
{
Dock = DockStyle.Fill,
BackColor = System.Drawing.Color.Black
};
_glControl.Load += GlControl_Load;
_glControl.Paint += GlControl_Paint;
_glControl.Resize += GlControl_Resize;
this.Controls.Add(_glControl);
}
private void GlControl_Load(object sender, EventArgs e)
{
// 初始化 OpenGL
GL.Enable(EnableCap.Texture2D);
GL.GenTextures(1, out _textureId);
GL.BindTexture(TextureTarget.Texture2D, _textureId);
// 设置纹理参数
GL.TexParameter(TextureTarget.Texture2D,
TextureParameterName.TextureMinFilter,
(int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D,
TextureParameterName.TextureMagFilter,
(int)TextureMagFilter.Linear);
// 创建空纹理
GL.TexImage2D(TextureTarget.Texture2D, 0,
PixelInternalFormat.Rgb,
_videoSize.Width, _videoSize.Height, 0,
PixelFormat.Bgr, PixelType.UnsignedByte,
IntPtr.Zero);
}
private void GlControl_Paint(object sender, PaintEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// 绑定纹理并渲染到全屏四边形
GL.BindTexture(TextureTarget.Texture2D, _textureId);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0, 1); GL.Vertex2(-1, -1); // 左下
GL.TexCoord2(1, 1); GL.Vertex2(1, -1); // 右下
GL.TexCoord2(1, 0); GL.Vertex2(1, 1); // 右上
GL.TexCoord2(0, 0); GL.Vertex2(-1, 1); // 左上
GL.End();
_glControl.SwapBuffers(); // 显示渲染结果
}
private void GlControl_Resize(object sender, EventArgs e)
{
// 调整视口大小
GL.Viewport(0, 0, _glControl.Width, _glControl.Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1, 1, -1, 1, -1, 1);
_glControl.Invalidate();
}
private void ProcessVideo()
{
using (Mat frame = new Mat())
{
while (_isRunning && _capture != null && _capture.IsOpened())
{
if (_capture.Read(frame) && !frame.Empty())
{
// 使用委托安全更新OpenGL纹理
_glControl.Invoke((MethodInvoker)delegate {
UpdateTexture(frame);
_glControl.Invalidate(); // 触发重绘
});
RenderFrame(frame);
}
Thread.Sleep(1); // 最小延迟
}
}
}
// 关键方法:将Mat数据更新到OpenGL纹理
private void UpdateTexture(Mat frame)
{
GL.BindTexture(TextureTarget.Texture2D, _textureId);
// 直接传输Mat数据到GPU (避免CPU复制)
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0,
frame.Width, frame.Height,
PixelFormat.Bgr, PixelType.UnsignedByte,
frame.Data);
}
private void StartVideo()
{
try
{
// 初始化视频捕获 (替换为你的RTSP地址)
string rtsp_url = "rtsp://admin:P2%40ink668803@10.21.0.188:554/cam/realmonitor?channel=1&subtype=0";
string optimizedUrl = "rtsp://admin:P2%40ink668803@10.21.0.188:554/cam/realmonitor?channel=1&subtype=0&transport=tcp";
_capture = new VideoCapture(rtsp_url);
if (!_capture.IsOpened())
throw new Exception("无法打开视频流");
// 获取实际视频尺寸
_videoSize = new Size(
(int)_capture.Get(VideoCaptureProperties.FrameWidth),
(int)_capture.Get(VideoCaptureProperties.FrameHeight)
);
_isRunning = true;
new Thread(ProcessVideo).Start();
}
catch (Exception ex)
{
MessageBox.Show($"初始化错误: {ex.Message}");
}
}
private Mat currentFrame = new Mat();
private int frameCount=0;
public string[] class_names;
public int class_num;
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now;
int input_height;
int input_width;
float ratio_height;
float ratio_width;
// 在渲染循环中添加推理
private List<DetectionResult> currentResults = new List<DetectionResult>();
public void RenderFrame(Mat currentFrame)
{
// 调整图像尺寸到640×640
int targetWidth = 640;
int targetHeight = 640;
// 计算保持宽高比的缩放比例
double scaleWidth = (double)targetWidth / currentFrame.Width;
double scaleHeight = (double)targetHeight / currentFrame.Height;
double scale = Math.Min(scaleWidth, scaleHeight);
// 计算缩放后的尺寸
int width = (int)(currentFrame.Width * scale);
int height = (int)(currentFrame.Height * scale);
// 调整图像尺寸
// Mat resizedImg = currentFrame.Resize(width, height);
// 如果需要强制设置为640x640(可能会变形),可以使用以下代码:
Mat resizedImg = new Mat(new Size(targetWidth, targetHeight), currentFrame.Type());
Cv2.Resize(currentFrame, resizedImg, new Size(targetWidth, targetHeight));
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// 1. 上传视频帧到GPU纹理
UploadFrameToTexture(resizedImg);
// 2. 执行模型推理(每5帧一次)
if (frameCount % 5 == 0)
{
currentResults = RunInference(resizedImg.Clone());
}
// 3. 绘制视频纹理
// GL.BindTexture(TextureTarget.Texture2D, videoTextureID);
// DrawFullscreenQuad();
// 4. 绘制推理结果
DrawDetectionResults(currentResults);
// glControl.SwapBuffers();
frameCount++;
}
InferenceSession onnx_session;
int box_num;
float conf_threshold;
float nms_threshold;
List<DetectionResult> RunInference(Mat frame)
{
Mat input_img = new Mat();
input_img = frame;
Tensor<float> input_tensor = new DenseTensor<float>(new[] { 1, 3, 640, 640 });
for (int y = 0; y < input_img.Height; y++)
{
for (int x = 0; x < input_img.Width; x++)
{
input_tensor[0, 0, y, x] = input_img.At<Vec3b>(y, x)[0] / 255f;
input_tensor[0, 1, y, x] = input_img.At<Vec3b>(y, x)[1] / 255f;
input_tensor[0, 2, y, x] = input_img.At<Vec3b>(y, x)[2] / 255f;
}
}
List<NamedOnnxValue> input_container = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("images", input_tensor)
};
// 执行推理
var inputs = new List<NamedOnnxValue>
{
NamedOnnxValue.CreateFromTensor("images", input_tensor)
};
//推理
dt1 = DateTime.Now;
var ort_outputs = onnx_session.Run(input_container).ToArray();
dt2 = DateTime.Now;
float[] data = Transpose(ort_outputs[0].AsTensor<float>().ToArray(), 4 + class_num, box_num);
float[] confidenceInfo = new float[class_num];
float[] rectData = new float[4];
List<DetectionResult> detResults = new List<DetectionResult>();
for (int i = 0; i < box_num; i++)
{
Array.Copy(data, i * (class_num + 4), rectData, 0, 4);
Array.Copy(data, i * (class_num + 4) + 4, confidenceInfo, 0, class_num);
float score = confidenceInfo.Max(); // 获取最大值
int maxIndex = Array.IndexOf(confidenceInfo, score); // 获取最大值的位置
int _centerX = (int)(rectData[0] * ratio_width);
int _centerY = (int)(rectData[1] * ratio_height);
int _width = (int)(rectData[2] * ratio_width);
int _height = (int)(rectData[3] * ratio_height);
detResults.Add(new DetectionResult(
maxIndex,
class_names[maxIndex],
new Rect(_centerX - _width / 2, _centerY - _height / 2, _width, _height),
score));
}
//NMS
CvDnn.NMSBoxes(detResults.Select(x => x.Rect), detResults.Select(x => x.Confidence), conf_threshold, nms_threshold, out int[] indices);
detResults = detResults.Where((x, index) => indices.Contains(index)).ToList();
int personCount = detResults.Where(w => w.ClassId == 0).Count();
//绘制结果
Mat result_image = input_img.Clone();
foreach (DetectionResult r in detResults)
{
Cv2.PutText(result_image, $"{r.Class}:{r.Confidence:P0}", new OpenCvSharp.Point(r.Rect.TopLeft.X, r.Rect.TopLeft.Y - 10), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
Cv2.Rectangle(result_image, r.Rect, Scalar.Red, thickness: 2);
}
pictureBox2.Invoke((MethodInvoker)delegate
{
if (result_image != null)
{
pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
}
textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
});
// 解析结果
return detResults;
}
void DrawDetectionResults(List<DetectionResult> results)
{
GL.Disable(EnableCap.Texture2D);
GL.LineWidth(2.0f);
foreach (var r in results)
{
// 将矩形坐标转换到屏幕空间 [-1,1]
float x0 = (r.Rect.X / (float)currentFrame.Width) * 2 - 1;
float y0 = (r.Rect.Y / (float)currentFrame.Height) * 2 - 1;
float x1 = ((r.Rect.X + r.Rect.Width) / (float)currentFrame.Width) * 2 - 1;
float y1 = ((r.Rect.Y + r.Rect.Height) / (float)currentFrame.Height) * 2 - 1;
// 绘制边界框
GL.Color3(1.0f, 0.0f, 0.0f); // 红色
GL.Begin(PrimitiveType.LineLoop);
GL.Vertex2(x0, y0);
GL.Vertex2(x1, y0);
GL.Vertex2(x1, y1);
GL.Vertex2(x0, y1);
GL.End();
// 绘制标签
//DrawText($"{r.Class}:{r.Confidence:F2}",
// x0, y0 - 0.05f, 0.03f);
}
GL.Enable(EnableCap.Texture2D);
}
// 使用生产者-消费者模式
private BlockingCollection<Mat> inferenceQueue = new BlockingCollection<Mat>(5);
private ConcurrentQueue<List<DetectionResult>> resultsQueue = new ConcurrentQueue<List<DetectionResult>>();
// 专用推理线程
private void StartInferenceThread()
{
new Thread(() =>
{
while (true)
{
if (inferenceQueue.TryTake(out Mat frame, 50))
{
var results = RunInference(frame);
resultsQueue.Enqueue(results);
frame.Dispose();
}
}
}).Start();
}
//// 渲染循环中
//if (frameCount % 5 == 0 && inferenceQueue.Count< 3)
//{
// inferenceQueue.Add(currentFrame.Clone());
//}
//if (resultsQueue.TryDequeue(out var newResults))
//{
// currentResults = newResults;
//}
/// <summary>
/// 上传视频帧到GPU纹理
/// </summary>
/// <param name="frame"></param>
private void UploadFrameToTexture(Mat frame)
{
// GL.BindTexture(TextureTarget.Texture2D, videoTextureID);
GL.TexImage2D(TextureTarget.Texture2D, 0,
PixelInternalFormat.Rgba,
frame.Width, frame.Height, 0,
PixelFormat.Bgra, PixelType.UnsignedByte,
frame.Data);
}
public static Mat PreprocessFrame(Mat inputFrame,
Size targetSize ,
NormTypes normType = NormTypes.MinMax,
double minVal = 0,
double maxVal = 1
)
{
// 1. 颜色空间转换:BGR→RGB
Mat rgbFrame = new Mat();
Cv2.CvtColor(inputFrame, rgbFrame, ColorConversionCodes.BGR2RGB);
// 2. 尺寸缩放
Mat resizedFrame = new Mat();
Cv2.Resize(rgbFrame, resizedFrame, targetSize, 0, 0, InterpolationFlags.Linear);
// 3. 数据类型转换
Mat convertedFrame = new Mat();
// resizedFrame.ConvertTo(convertedFrame, outputType);
// 4. 归一化处理
Mat normalizedFrame = new Mat();
Cv2.Normalize(convertedFrame, normalizedFrame, minVal, maxVal, normType);
// 释放中间资源
rgbFrame.Dispose();
resizedFrame.Dispose();
convertedFrame.Dispose();
return normalizedFrame;
}
public unsafe float[] Transpose(float[] tensorData, int rows, int cols)
{
float[] transposedTensorData = new float[tensorData.Length];
fixed (float* pTensorData = tensorData)
{
fixed (float* pTransposedData = transposedTensorData)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
int index = i * cols + j;
int transposedIndex = j * rows + i;
pTransposedData[transposedIndex] = pTensorData[index];
}
}
}
}
return transposedTensorData;
}
string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
string image_path = "";
string model_path;
string classer_path;
private void FrmCameraOnnxTest_Load(object sender, EventArgs e)
{
model_path = @"D:\资料\图片视屏大模型\WinformTestYolov11\AIPlateformSystem\AIPlatformSystem\model\\yolo11n.onnx";
//创建输出会话,用于输出模型读取信息
SessionOptions options = new SessionOptions();
options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
// 创建推理模型类,读取模型文件
onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
input_height = 640;
input_width = 640;
box_num = 8400;
conf_threshold = 0.5f;
nms_threshold = 0.7f;
// classer_path = @"D:\\workplace\\repos\\WindowsFormsApp12\\WindowsFormsApp12\\model\\label.txt";
classer_path = @"D:\资料\图片视屏大模型\C# ONNXRuntime部署yoloV11\WindowsFormsApp12\WindowsFormsApp12\model\label.txt";
class_names = File.ReadAllLines(classer_path, Encoding.UTF8);
class_num = class_names.Length;
StartVideo();
}
}
}以上代码如何在前端页面中实时标注推理的结果数据,代码如何修改