91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
南川市| 泰来县| 富源县| 行唐县| 韶山市| 汶上县| 鹤岗市| 邢台市| 西平县| 宣汉县| 阆中市| 昆明市| 巴林左旗| 怀柔区| 渝北区| 古田县| 北海市| 永平县| 贵阳市| 盐津县| 法库县| 五华县| 文安县| 宁河县| 吉林省| 东丽区| 新绛县| 高台县| 莱阳市| 陈巴尔虎旗| 瓮安县| 崇义县| 博湖县| 宁国市| 麻江县| 剑川县| 泾源县| 丰城市| 三台县| 黑龙江省| 弥渡县|