首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# E2Pose人体关键点检测(OpenVINO推理)

  • 25-02-19 03:20
  • 3574
  • 5196
blog.csdn.net

C# E2Pose人体关键点检测(OpenVINO推理)

目录

效果

模型信息

项目

代码

下载


效果

模型信息

Inputs
-------------------------
name:inputimg
tensor:Float[1, 3, 512, 512]
---------------------------------------------------------------

Outputs
-------------------------
name:kvxy/concat
tensor:Float[1, 341, 17, 3]
name:pv/concat
tensor:Float[1, 341, 1, 1]
---------------------------------------------------------------

项目

代码

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace OpenVINO_Demo
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string imgFilter = "图片|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

        E2Pose e2Pose;
        Mat image;

        string image_path = "";
        string model_path;

        string video_path = "";
        string videoFilter = "视频|*.mp4;*.avi";
        VideoCapture vcapture;
        VideoWriter vwriter;
        bool saveDetVideo = false;

        ///


        /// 单图推理
        ///

        ///
        ///
        private void button2_Click(object sender, EventArgs e)
        {

            if (image_path == "")
            {
                return;
            }

            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";

            Application.DoEvents();

            image = new Mat(image_path);

            Mat result_image = image.Clone();
            e2Pose.Detect(result_image);

            if (pictureBox2.Image != null)
            {
                pictureBox2.Image.Dispose();
            }
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = e2Pose.DetectTime();

            button2.Enabled = true;

        }

        ///


        /// 窗体加载,初始化
        ///

        ///
        ///
        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "test/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);

            model_path = "model/e2epose_resnet50_1x3x512x512.onnx";

            e2Pose = new E2Pose(model_path);
        }

        ///


        /// 选择图片
        ///

        ///
        ///
        private void button1_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = imgFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);

            textBox1.Text = "";
            pictureBox2.Image = null;
        }

        ///


        /// 选择视频
        ///

        ///
        ///
        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = videoFilter;
            ofd.InitialDirectory = Application.StartupPath + "\\test";
            if (ofd.ShowDialog() != DialogResult.OK) return;

            video_path = ofd.FileName;

            textBox1.Text = video_path;
        }

        ///


        /// 视频推理
        ///

        ///
        ///
        private void button3_Click(object sender, EventArgs e)
        {
            if (video_path == "")
            {
                MessageBox.Show("请先选择视频!");
                return;
            }

            textBox1.Text = "开始检测";

            Application.DoEvents();

            Thread thread = new Thread(new ThreadStart(VideoDetection));

            thread.Start();
            thread.Join();

            textBox1.Text = "检测完成!";
        }

        void VideoDetection()
        {
            vcapture = new VideoCapture(video_path);
            if (!vcapture.IsOpened())
            {
                MessageBox.Show("打开视频文件失败");
                return;
            }


            Mat frame = new Mat();

            // 获取视频的fps
            double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
            // 计算等待时间(毫秒)
            int delay = (int)(1000 / videoFps);
            Stopwatch _stopwatch = new Stopwatch();

            if (checkBox1.Checked)
            {
                vwriter = new VideoWriter("out.mp4", FourCC.X264, vcapture.Fps, new OpenCvSharp.Size(vcapture.FrameWidth, vcapture.FrameHeight));
                saveDetVideo = true;
            }
            else
            {
                saveDetVideo = false;
            }

            Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
            Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth, vcapture.FrameHeight);

            while (vcapture.Read(frame))
            {
                if (frame.Empty())
                {
                    MessageBox.Show("读取失败");
                    return;
                }

                _stopwatch.Restart();

                delay = (int)(1000 / videoFps);

                e2Pose.Detect(frame);

                Cv2.PutText(frame, "preprocessTime:" + e2Pose.preprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                Cv2.PutText(frame, "inferTime:" + e2Pose.inferTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 70), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                Cv2.PutText(frame, "postprocessTime:" + e2Pose.postprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 110), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                Cv2.PutText(frame, "totalTime:" + e2Pose.totalTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 150), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                Cv2.PutText(frame, "video fps:" + videoFps.ToString("F2"), new OpenCvSharp.Point(10, 190), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
                Cv2.PutText(frame, "det fps:" + e2Pose.detFps.ToString("F2"), new OpenCvSharp.Point(10, 230), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);

                if (saveDetVideo)
                {
                    vwriter.Write(frame);
                }

                Cv2.ImShow("DetectionResult 按下ESC,退出", frame);

                // for test
                // delay = 1;
                delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
                if (delay <= 0)
                {
                    delay = 1;
                }
                //Console.WriteLine("delay:" + delay.ToString()) ;
                if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
                {
                    Cv2.DestroyAllWindows();
                    vcapture.Release();
                    break;
                }
            }

            Cv2.DestroyAllWindows();
            vcapture.Release();
            if (saveDetVideo)
            {
                vwriter.Release();
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (video_path == "")
            {
                MessageBox.Show("请先选择视频!");
                return;
            }
            button5.Enabled = false;
            textBox1.Text = "开始检测";
            Application.DoEvents();

            Task task = new Task(() =>
            {
                VideoCapture vcapture = new VideoCapture(video_path);
                if (!vcapture.IsOpened())
                {
                    MessageBox.Show("打开视频文件失败");
                    return;
                }

                E2Pose2 e2Pose2 = new E2Pose2(model_path);

                Mat frame = new Mat();

                // 获取视频的fps
                double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
                // 计算等待时间(毫秒)
                int delay = (int)(1000 / videoFps);
                Stopwatch _stopwatch = new Stopwatch();

                Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
                Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth * 2, vcapture.FrameHeight);

                while (vcapture.Read(frame))
                {
                    if (frame.Empty())
                    {
                        MessageBox.Show("读取失败");
                        return;
                    }

                    _stopwatch.Restart();

                    delay = (int)(1000 / videoFps);

                    Mat result = e2Pose2.Detect(frame);

                    Cv2.ImShow("DetectionResult 按下ESC,退出", result);

                    // for test
                    // delay = 1;
                    delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
                    if (delay <= 0)
                    {
                        delay = 1;
                    }
                    //Console.WriteLine("delay:" + delay.ToString()) ;
                    // 如果按下ESC或点击关闭,退出循环
                    if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
                    {
                        Cv2.DestroyAllWindows();
                        vcapture.Release();
                        break;
                    }
                }

                Cv2.DestroyAllWindows();
                vcapture.Release();

                textBox1.Invoke(new Action(() =>
                {
                    textBox1.Text = "检测结束!";
                    button5.Enabled = true; ;

                }));

            });
            task.Start();

        }

        //保存
        SaveFileDialog sdf = new SaveFileDialog();
        private void button6_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }

        }

    }

}
 

  1. using OpenCvSharp;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace OpenVINO_Demo
  10. {
  11. public partial class Form2 : Form
  12. {
  13. public Form2()
  14. {
  15. InitializeComponent();
  16. }
  17. string imgFilter = "图片|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  18. E2Pose e2Pose;
  19. Mat image;
  20. string image_path = "";
  21. string model_path;
  22. string video_path = "";
  23. string videoFilter = "视频|*.mp4;*.avi";
  24. VideoCapture vcapture;
  25. VideoWriter vwriter;
  26. bool saveDetVideo = false;
  27. /// <summary>
  28. /// 单图推理
  29. /// </summary>
  30. /// <param name="sender"></param>
  31. /// <param name="e"></param>
  32. private void button2_Click(object sender, EventArgs e)
  33. {
  34. if (image_path == "")
  35. {
  36. return;
  37. }
  38. button2.Enabled = false;
  39. pictureBox2.Image = null;
  40. textBox1.Text = "";
  41. Application.DoEvents();
  42. image = new Mat(image_path);
  43. Mat result_image = image.Clone();
  44. e2Pose.Detect(result_image);
  45. if (pictureBox2.Image != null)
  46. {
  47. pictureBox2.Image.Dispose();
  48. }
  49. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  50. textBox1.Text = e2Pose.DetectTime();
  51. button2.Enabled = true;
  52. }
  53. /// <summary>
  54. /// 窗体加载,初始化
  55. /// </summary>
  56. /// <param name="sender"></param>
  57. /// <param name="e"></param>
  58. private void Form1_Load(object sender, EventArgs e)
  59. {
  60. image_path = "test/1.jpg";
  61. pictureBox1.Image = new Bitmap(image_path);
  62. model_path = "model/e2epose_resnet50_1x3x512x512.onnx";
  63. e2Pose = new E2Pose(model_path);
  64. }
  65. /// <summary>
  66. /// 选择图片
  67. /// </summary>
  68. /// <param name="sender"></param>
  69. /// <param name="e"></param>
  70. private void button1_Click_1(object sender, EventArgs e)
  71. {
  72. OpenFileDialog ofd = new OpenFileDialog();
  73. ofd.Filter = imgFilter;
  74. if (ofd.ShowDialog() != DialogResult.OK) return;
  75. pictureBox1.Image = null;
  76. image_path = ofd.FileName;
  77. pictureBox1.Image = new Bitmap(image_path);
  78. textBox1.Text = "";
  79. pictureBox2.Image = null;
  80. }
  81. /// <summary>
  82. /// 选择视频
  83. /// </summary>
  84. /// <param name="sender"></param>
  85. /// <param name="e"></param>
  86. private void button4_Click(object sender, EventArgs e)
  87. {
  88. OpenFileDialog ofd = new OpenFileDialog();
  89. ofd.Filter = videoFilter;
  90. ofd.InitialDirectory = Application.StartupPath + "\\test";
  91. if (ofd.ShowDialog() != DialogResult.OK) return;
  92. video_path = ofd.FileName;
  93. textBox1.Text = video_path;
  94. }
  95. /// <summary>
  96. /// 视频推理
  97. /// </summary>
  98. /// <param name="sender"></param>
  99. /// <param name="e"></param>
  100. private void button3_Click(object sender, EventArgs e)
  101. {
  102. if (video_path == "")
  103. {
  104. MessageBox.Show("请先选择视频!");
  105. return;
  106. }
  107. textBox1.Text = "开始检测";
  108. Application.DoEvents();
  109. Thread thread = new Thread(new ThreadStart(VideoDetection));
  110. thread.Start();
  111. thread.Join();
  112. textBox1.Text = "检测完成!";
  113. }
  114. void VideoDetection()
  115. {
  116. vcapture = new VideoCapture(video_path);
  117. if (!vcapture.IsOpened())
  118. {
  119. MessageBox.Show("打开视频文件失败");
  120. return;
  121. }
  122. Mat frame = new Mat();
  123. // 获取视频的fps
  124. double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
  125. // 计算等待时间(毫秒)
  126. int delay = (int)(1000 / videoFps);
  127. Stopwatch _stopwatch = new Stopwatch();
  128. if (checkBox1.Checked)
  129. {
  130. vwriter = new VideoWriter("out.mp4", FourCC.X264, vcapture.Fps, new OpenCvSharp.Size(vcapture.FrameWidth, vcapture.FrameHeight));
  131. saveDetVideo = true;
  132. }
  133. else
  134. {
  135. saveDetVideo = false;
  136. }
  137. Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
  138. Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth, vcapture.FrameHeight);
  139. while (vcapture.Read(frame))
  140. {
  141. if (frame.Empty())
  142. {
  143. MessageBox.Show("读取失败");
  144. return;
  145. }
  146. _stopwatch.Restart();
  147. delay = (int)(1000 / videoFps);
  148. e2Pose.Detect(frame);
  149. Cv2.PutText(frame, "preprocessTime:" + e2Pose.preprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 30), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
  150. Cv2.PutText(frame, "inferTime:" + e2Pose.inferTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 70), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
  151. Cv2.PutText(frame, "postprocessTime:" + e2Pose.postprocessTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 110), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
  152. Cv2.PutText(frame, "totalTime:" + e2Pose.totalTime.ToString("F2") + "ms", new OpenCvSharp.Point(10, 150), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
  153. Cv2.PutText(frame, "video fps:" + videoFps.ToString("F2"), new OpenCvSharp.Point(10, 190), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
  154. Cv2.PutText(frame, "det fps:" + e2Pose.detFps.ToString("F2"), new OpenCvSharp.Point(10, 230), HersheyFonts.HersheySimplex, 1, Scalar.Red, 2);
  155. if (saveDetVideo)
  156. {
  157. vwriter.Write(frame);
  158. }
  159. Cv2.ImShow("DetectionResult 按下ESC,退出", frame);
  160. // for test
  161. // delay = 1;
  162. delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
  163. if (delay <= 0)
  164. {
  165. delay = 1;
  166. }
  167. //Console.WriteLine("delay:" + delay.ToString()) ;
  168. if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
  169. {
  170. Cv2.DestroyAllWindows();
  171. vcapture.Release();
  172. break;
  173. }
  174. }
  175. Cv2.DestroyAllWindows();
  176. vcapture.Release();
  177. if (saveDetVideo)
  178. {
  179. vwriter.Release();
  180. }
  181. }
  182. private void button5_Click(object sender, EventArgs e)
  183. {
  184. if (video_path == "")
  185. {
  186. MessageBox.Show("请先选择视频!");
  187. return;
  188. }
  189. button5.Enabled = false;
  190. textBox1.Text = "开始检测";
  191. Application.DoEvents();
  192. Task task = new Task(() =>
  193. {
  194. VideoCapture vcapture = new VideoCapture(video_path);
  195. if (!vcapture.IsOpened())
  196. {
  197. MessageBox.Show("打开视频文件失败");
  198. return;
  199. }
  200. E2Pose2 e2Pose2 = new E2Pose2(model_path);
  201. Mat frame = new Mat();
  202. // 获取视频的fps
  203. double videoFps = vcapture.Get(VideoCaptureProperties.Fps);
  204. // 计算等待时间(毫秒)
  205. int delay = (int)(1000 / videoFps);
  206. Stopwatch _stopwatch = new Stopwatch();
  207. Cv2.NamedWindow("DetectionResult 按下ESC,退出", WindowFlags.Normal);
  208. Cv2.ResizeWindow("DetectionResult 按下ESC,退出", vcapture.FrameWidth * 2, vcapture.FrameHeight);
  209. while (vcapture.Read(frame))
  210. {
  211. if (frame.Empty())
  212. {
  213. MessageBox.Show("读取失败");
  214. return;
  215. }
  216. _stopwatch.Restart();
  217. delay = (int)(1000 / videoFps);
  218. Mat result = e2Pose2.Detect(frame);
  219. Cv2.ImShow("DetectionResult 按下ESC,退出", result);
  220. // for test
  221. // delay = 1;
  222. delay = (int)(delay - _stopwatch.ElapsedMilliseconds);
  223. if (delay <= 0)
  224. {
  225. delay = 1;
  226. }
  227. //Console.WriteLine("delay:" + delay.ToString()) ;
  228. // 如果按下ESC或点击关闭,退出循环
  229. if (Cv2.WaitKey(delay) == 27 || Cv2.GetWindowProperty("DetectionResult 按下ESC,退出", WindowPropertyFlags.Visible) < 1.0)
  230. {
  231. Cv2.DestroyAllWindows();
  232. vcapture.Release();
  233. break;
  234. }
  235. }
  236. Cv2.DestroyAllWindows();
  237. vcapture.Release();
  238. textBox1.Invoke(new Action(() =>
  239. {
  240. textBox1.Text = "检测结束!";
  241. button5.Enabled = true; ;
  242. }));
  243. });
  244. task.Start();
  245. }
  246. //保存
  247. SaveFileDialog sdf = new SaveFileDialog();
  248. private void button6_Click(object sender, EventArgs e)
  249. {
  250. if (pictureBox2.Image == null)
  251. {
  252. return;
  253. }
  254. Bitmap output = new Bitmap(pictureBox2.Image);
  255. sdf.Title = "保存";
  256. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
  257. if (sdf.ShowDialog() == DialogResult.OK)
  258. {
  259. switch (sdf.FilterIndex)
  260. {
  261. case 1:
  262. {
  263. output.Save(sdf.FileName, ImageFormat.Jpeg);
  264. break;
  265. }
  266. case 2:
  267. {
  268. output.Save(sdf.FileName, ImageFormat.Png);
  269. break;
  270. }
  271. case 3:
  272. {
  273. output.Save(sdf.FileName, ImageFormat.Bmp);
  274. break;
  275. }
  276. }
  277. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  278. }
  279. }
  280. }
  281. }

下载

源码下载

带异步推理版源码下载

OnnxRuntime 版(CPU推理、GPU推理)源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/139497753"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top