中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

YOLO C#版本對比Python版

c#
小樊
82
2024-07-20 23:23:02
欄目: 編程語言

這里是一個簡單的比較Python和C#中的YOLO實現的例子:

Python版本:

import cv2

net = 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
瑞丽市| 启东市| 彭山县| 平远县| 呼玛县| 江源县| 五台县| 天峨县| 称多县| 韶山市| 舒兰市| 九台市| 定远县| 中宁县| 祁门县| 武穴市| 民丰县| 靖州| 神池县| 会昌县| 石泉县| 福贡县| 临湘市| 商水县| 武平县| 鞍山市| 成安县| 乐都县| 盐源县| 科尔| 旬邑县| 神木县| 青铜峡市| 宁强县| 潼关县| 南充市| 鄂温| 南投县| 广汉市| 黔东| 宣汉县|