效果

工程

代码

using Microsoft.ML.OnnxRuntime;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms; namespace OpenCvSharp_Yolov5Net_Onnx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private string PicFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = PicFilter;
if (ofd.ShowDialog() != DialogResult.OK) return; var imagefile = ofd.FileName;
var image = Image.FromFile(imagefile);
Yolov5Net.Scorer.YoloScorer<Yolov5Net.Scorer.Models.YoloCocoP5Model> scorer = new Yolov5Net.Scorer.YoloScorer<Yolov5Net.Scorer.Models.YoloCocoP5Model>("yolov5s.onnx"); List<Yolov5Net.Scorer.YoloPrediction> predictions = scorer.Predict(image); var graphics = Graphics.FromImage(image); foreach (var prediction in predictions) // iterate predictions to draw results
{
double score = Math.Round(prediction.Score, 2); graphics.DrawRectangles(new Pen(prediction.Label.Color, 2), new[] { prediction.Rectangle }); PointF p = new PointF(prediction.Rectangle.X - 3, prediction.Rectangle.Y - 23); graphics.DrawString(prediction.Label.Name + " (" + score + ")"
, new Font("Arial", 16, GraphicsUnit.Pixel)
, new SolidBrush(prediction.Label.Color)
, p);
} pictureBox1.Image = image;
} FrameSource frame;
Thread t;
private void button2_Click(object sender, EventArgs e)
{
//获取视频设备
frame = Cv2.CreateFrameSource_Camera(0);
t = new Thread(CameraDet);
t.Start();
} private void CameraDet()
{ Mat mat = new Mat();
//yolov5+onnx检测
var scorer = new Yolov5Net.Scorer.YoloScorer<Yolov5Net.Scorer.Models.YoloCocoP5Model>("yolov5s.onnx"); //逐帧放入
while (true)
{
if (frame.IsDisposed)
{
return;
}
//从摄像头读视频帧
frame.NextFrame(mat);
//先将Mat图像转换成Stream流,再将流转成Bitmap
var image = new Bitmap(mat.ToMemoryStream()) as Image;
//var image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
List<Yolov5Net.Scorer.YoloPrediction> predictions = scorer.Predict(image);
//在图片上绘制目标标识
var graphics = Graphics.FromImage(image);
foreach (var prediction in predictions) // iterate predictions to draw results
{
double score = Math.Round(prediction.Score, 2); graphics.DrawRectangles(new Pen(prediction.Label.Color, 2),
new[] { prediction.Rectangle }); var (x, y) = (prediction.Rectangle.X - 3, prediction.Rectangle.Y - 23); graphics.DrawString($"{prediction.Label.Name} ({score})",
new Font("Arial", 16, GraphicsUnit.Pixel), new SolidBrush(prediction.Label.Color),
new PointF(x, y)); pictureBox1.Image = image;
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (frame != null)
{
frame.Dispose();
frame = null;
t.Abort();
}
} }
}

Demo下载​​​​​​​

OpenCvSharp+Yolov5Net+Onnx 完整Demo的更多相关文章

  1. 百度编辑器UEditor与UEditor 公式插件完整Demo

    1.下载UEditor(我的是.net项目) 2.下载UEditor公式插件 3.新建解决方案和项目 4.在浏览器中预览index.html页面 结果: 5.index.html源码 <!DOC ...

  2. 谷歌开源项目Google Preview Image Extractor(PIEX) (附上完整demo代码)

    前天偶然看到谷歌开源项目中有一个近乎无人问津的项目Google Preview Image Extractor(PIEX) . 项目地址: https://github.com/google/piex ...

  3. jQuery实现的分页功能,包括ajax请求,后台数据,有完整demo

    一:需求分析 1)需要首页,末页功能 2)有点击查看上一页,下一页功能 3)页码到当前可视页码最后一页刷新页面 二:功能实现思路 也是分为三部分处理 1)点击首页,末页直接显示第一页或者最后一页内容, ...

  4. REST风格框架实战:从MVC到前后端分离(附完整Demo)

    既然MVC模式这么好,难道它就没有不足的地方吗?我认为MVC至少有以下三点不足:(1)每次请求必须经过“控制器->模型->视图”这个流程,用户才能看到最终的展现的界面,这个过程似乎有些复杂 ...

  5. 前后端分离开发,基于SpringMVC符合Restful API风格Maven项目实战(附完整Demo)!

    摘要: 本人在前辈<从MVC到前后端分离(REST-个人也认为是目前比较流行和比较好的方式)>一文的基础上,实现了一个基于Spring的符合REST风格的完整Demo,具有MVC分层结构并 ...

  6. MQTT(一)C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整Demo下载)

    https://blog.csdn.net/panwen1111/article/details/79245161 目录MQTT(一)C#使用 MQTTnet 快速实现 MQTT 通信(文末有完整De ...

  7. 【转】REST风格框架实战:从MVC到前后端分离(附完整Demo)

    版权声明:欢迎转载,注明作者和出处就好!如果不喜欢或文章存在明显的谬误,请留言说明原因再踩哦,谢谢,我也可以知道原因,不断进步! https://blog.csdn.net/justloveyou_/ ...

  8. Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)

    标题很大,想说的很多,不知道从那开始~~直接步入正题吧 个人也是由于公司的人员的现状和项目的特殊情况,今年年中后开始折腾web自动化这块:整这个原因很简单,就是想能让自己偷点懒.也让减轻一点同事的苦力 ...

  9. 小程序微信支付完整demo,包含退款

    最近刚完成一个商场小程序(http://market.zhenzikj.com/detail/121.html), 使用到了微信支付功能,其中遇到了很多的抗,所以,我把支付这块摘出来,以免大家少走弯路 ...

  10. NVelocity学习笔记一——linq2sql+NVelocity完整demo

    (一)前言      刚刚进入新公司,看公司的项目,发现开发流程几乎和以前的完全不同,再看看页面布局竟然没有发现html.神马情况????一番探究发现使用了NVelocity模板引擎开发的.于是乎花了 ...

随机推荐

  1. 【Unity3D】激光灯、碰撞特效

    1 需求描述 ​ 本文将模拟激光灯(或碰撞)特效,详细需求如下: 从鼠标位置发射屏幕射线,检测是否与物体发生碰撞 当与物体发生碰撞时,在物体表面覆盖一层激光灯(或碰撞)特效 ​ 本文代码见→激光灯.碰 ...

  2. Js捕获异常的方法

    Js捕获异常的方法 JavaScript的异常主要使用try catch finally语句以及窗口对象window的onerror事件来捕获. try catch finally try catch ...

  3. springboot和jquery.form.js实现监听文件上传进度

    说明 文件上传作为程序开发最常用的功能之一,上传进度展示也是必须的.但是有时候进度并不准,进度100%了实际上后台尚未接收完毕,本篇就介绍如何利用jquery的form插件来实时反馈文件上传进度. 实 ...

  4. Oracle设置和删除不可用列

    Oracle设置和删除不可用列 1.不可用列是什么? 就是表中的1个或多个列被ALTER TABLE-SET UNUSED 语句设置为无法再被程序利用的列. 2.使用场景? If you are co ...

  5. centos6关闭防火墙

    service iptable status     --查看防火墙状态 servcie iptables stop     --临时关闭防火墙 chkconfig iptables off      ...

  6. go build gcc报错 /usr/bin/ld | cannot find -ldl cannot find -lpthread cannot find -lc

    之前一直在kali或者其他ubuntu设备编译sliver, 临时在centos7上编译时报错了 # github.com/bishopfox/sliver/server /usr/local/go/ ...

  7. 常用SQL语句备查

    查询表中某一列是否有重复值 SELECT bizType, COUNT(bizType) FROM Res GROUP BY bizType HAVING COUNT(bizType) > 1 ...

  8. 【译】代码更快、更好,借助 GitHub Copilot 的新功能:斜杠命令和上下文变量

    你是否曾经希望有一个人工智能助手可以帮助你更快更好地编写代码?那就是 Visual Studio Copilot Chat 为您提供的:一个人工智能驱动的结对程序员,可以回答您的问题,建议代码片段,解 ...

  9. 多线程系列(九) -ReentrantLock常用方法详解

    一.简介 在上一篇文章中,我们介绍了ReentrantLock类的一些基本用法,今天我们重点来介绍一下ReentrantLock其它的常用方法,以便对ReentrantLock类的使用有更深入的理解. ...

  10. 【认知服务 Azure Cognitive Service】使用Azure Search中Create an Demo的示例时,出现空白页面的问题

    问题描述 在根据Azure 认知服务的Search功能文档创建示例时(快速入门:在门户中创建演示应用(Azure 认知搜索)).完全相同的步骤,在中国区创建后下载Demo,查询结果一片空白:如下: 而 ...