分享好友 物流大全首页 物流大全分类 切换频道

吉日象物流货运网

YOLO C#版本对比Python版

2024-10-063150

这里是一个简单的比较Python和C#中的YOLO实现的例子:

Python版本:

import cv2net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")classes = []with open("coco.names", "r") as f:    classes = [line.strip() for line in f.readlines()]layer_names = net.getLayerNames()output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]image = cv2.imread("image.jpg")blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False)net.setInput(blob)outs = net.forward(output_layers)for out in outs:    for detection in out:        scores = detection[5:]        class_id = np.argmax(scores)        confidence = scores[class_id]        if confidence > 0.5:            center_x = int(detection[0] * image.shape[1])            center_y = int(detection[1] * image.shape[0])            width = int(detection[2] * image.shape[1])            height = int(detection[3] * image.shape[0])            x = int(center_x - width / 2)            y = int(center_y - height / 2)            cv2.rectangle(image, (x, y), (x + width, y + height), (0, 255, 0), 2)            cv2.putText(image, classes[class_id], (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)cv2.imshow("Image", image)cv2.waitKey(0)cv2.destroyAllWindows()

C#版本:

using System;using System.Collections.Generic;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Web;using System.Web.Http;public class YoloController : ApiController{    [HttpPost]    public IHttpActionResult DetectObjects()    {        string modelFilePath = HttpContext.Current.Server.MapPath("~/yolov3.weights");        string configFilePath = HttpContext.Current.Server.MapPath("~/yolov3.cfg");        string classesFilePath = HttpContext.Current.Server.MapPath("~/coco.names");                var classes = File.ReadAllLines(classesFilePath);                var net = CvDnn.ReadNet(modelFilePath, configFilePath);                var layerNames = net.GetLayerNames();        var outputLayers = layerNames.Where(l => net.GetUnconnectedOutLayers().Contains(l)).ToList();                var image = HttpContext.Current.Request.Files["image"];        var bitmap = new Bitmap(image.InputStream);        var blob = CvDnn.BlobFromImage(bitmap, 0.00392, new Size(416, 416), new Scalar(0, 0, 0), true, false);                net.SetInput(blob);        var outs = net.Forward(outputLayers.ToArray());                foreach (var detection in outs)        {            var scores = detection.Skip(5);            var classId = Array.IndexOf(scores, scores.Max());            var confidence = scores[classId];                        if (confidence > 0.5)            {                var centerX = (int)(detection[0] * bitmap.Width);                var centerY = (int)(detection[1] * bitmap.Height);                var width = (int)(detection[2] * bitmap.Width);                var height = (int)(detection[3] * bitmap.Height);                var x = centerX - width / 2;                var y = centerY - height / 2;                                using (var graphics = Graphics.FromImage(bitmap))                {                    graphics.DrawRectangle(Pens.Green, x, y, width, height);                    graphics.DrawString(classes[classId], new Font("Arial", 12), Brushes.Green, new PointF(x, y - 15));                }            }        }                var outputStream = new MemoryStream();        bitmap.Save(outputStream, ImageFormat.Jpeg);                return Ok(Convert.ToBase64String(outputStream.ToArray()));    }}

这两个版本都使用了OpenCV的深度学习模块来实现YOLO目标检测算法。虽然Python和C#的代码语法和结构有所不同,但它们的实现原理是相似的。在Python版本中,我们使用了cv2来读取模型和图像,并进行目标检测的操作。而在C#版本中,我们使用了Emgu.CV库来实现相同的功能。无论是Python还是C#,都可以很容

举报
打赏
打赏主播是什么意思
打赏主播是指观众(用户)在观看网络直播节目时,自愿通过直播平台提供的支付渠道,将一定数额的金钱或虚拟礼物赠送给主播的行为

0评论2025-03-26878

主打是什么意思
“主打” 这个词常见的有以下几种意思:一、在商业、产品领域主要营销、重点推广当我们说某产品是公司的 “主打产品” 时,是指

0评论2025-03-26644

电台路是什么意思
“电台路” 通常是因与电台相关的因素而得名的道路,以下是一些具体例子:上海电台路:位于宝山区顾村镇,呈南北走向,南起宝安

0评论2025-03-26340

打狙的窍门是什么意思
“打狙的窍门” 通常是指在射击游戏或实际射击场景中,使用狙击步枪时能够提高射击准确性、效率和生存能力等的一些技巧和方法。

0评论2025-03-26654

路上的创作原声是什么意思
“路上的创作原声” 通常是指以 “在路上” 的状态、经历、感悟等为主题或灵感来源而创作的原始声音作品,在不同艺术领域有不同

0评论2025-03-26383

打印照片回执是什么意思
打印照片回执是指在完成照片拍摄并经相关系统审核通过后,将记录照片合格信息以及个人身份等相关内容的电子凭证,通过打印机输出

0评论2025-03-26295

棒打鸳鸯是什么意思
“棒打鸳鸯” 是一个汉语成语,意思是用木棒打散一对鸳鸯,比喻拆散恩爱的夫妻或情侣。该成语的来源和用法如下:来源与出处:出

0评论2025-03-26672

主打三棺是什么意思
“主打三棺” 是一种网络用语,用反语的方式讽刺人们没有正常的思维观念和底线。该梗起源于贴吧网友之间的讽刺话语,具体出自抗

0评论2025-03-26223

常用香料是什么意思
常用香料是指在烹饪、食品加工、香水制作、药品生产等领域中经常使用的具有独特香气和味道的物质。它们可以增添风味、改善气味、

0评论2025-03-26975

潮汕话香芋是什么意思
在潮汕话中,“香芋” 通常指的就是芋头。不过,潮汕方言中芋头的发音是 “麦筲”(mài shāo)。芋头在潮汕地区的饮食文化和民

0评论2025-03-26504

关于我们  |  网站留言
(c)2026 吉日象物流货运网-大件运输货运平台 www.mykuaidi.com