亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
信宜市| 洪江市| 东安县| 龙里县| 渝北区| 抚远县| 武乡县| 永清县| 怀来县| 乐东| 永嘉县| 泊头市| 平昌县| 平顶山市| 扎赉特旗| 调兵山市| 镇雄县| 定结县| 隆尧县| 宜黄县| 阿瓦提县| 莱芜市| 白河县| 常熟市| 宜章县| 田阳县| 宾川县| 迭部县| 棋牌| 罗城| 凤冈县| 西宁市| 运城市| 永宁县| 长沙县| 新余市| 屏东市| 钟山县| 西城区| 唐河县| 新源县|