Share yolo c#

congtuan679

New member
## yolo c#

[Liên kết đến bài viết tham khảo] (https://www.learnopencv.com/yolo-object-detection-with-python-and-opencv/)

Yolo (bạn chỉ nhìn một lần) là một thuật toán phát hiện đối tượng phổ biến có thể được sử dụng để phát hiện các đối tượng trong hình ảnh và video.Yolo nhanh và chính xác, và nó có thể được sử dụng để phát hiện nhiều đối tượng khác nhau.

Yolo được triển khai trong C ++, nhưng nó cũng có thể được sử dụng trong các ngôn ngữ lập trình khác, chẳng hạn như C#.Hướng dẫn này sẽ chỉ cho bạn cách sử dụng Yolo trong C#.

### Điều kiện tiên quyết

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một máy tính có Windows, MacOS hoặc Linux
* Visual Studio 2019 trở lên
* Thư viện OpenCV
* Mô hình Yolo Darknet

### Bắt đầu

Bước đầu tiên là cài đặt thư viện OpenCV.Bạn có thể làm điều này bằng cách làm theo các hướng dẫn trên trang web OpenCV.

Khi bạn đã cài đặt OpenCV, bạn có thể tải xuống mô hình Darknet Yolo.Bạn có thể tìm thấy mô hình trên trang web Darknet.

### Tạo một dự án

Khi bạn có thư viện OpenCV và mô hình Darknet Yolo, bạn có thể tạo một dự án mới trong Visual Studio.

Trong Visual Studio, tạo một dự án C# mới.Đặt tên cho dự án "Yolo".

### Thêm thư viện OpenCV

Bước tiếp theo là thêm thư viện OpenCV vào dự án của bạn.Để thực hiện việc này, nhấp chuột phải vào dự án của bạn trong Giải pháp Explorer và chọn "Thêm tham chiếu".Trong hộp thoại "Trình quản lý tham chiếu", chọn "Duyệt" và điều hướng đến vị trí mà bạn đã cài đặt thư viện OpenCV.Chọn tệp "opencv.dll" và nhấp vào "Thêm".

### Thêm mô hình Yolo Darknet

Bước tiếp theo là thêm mô hình Darknet Yolo vào dự án của bạn.Để thực hiện việc này, nhấp chuột phải vào dự án của bạn trong Giải pháp Explorer và chọn "Thêm"> "Mục hiện tại".Trong hộp thoại "Thêm mục hiện tại", điều hướng đến vị trí mà bạn đã tải xuống mô hình Darknet Yolo.Chọn tệp "Yolov3. Umeights" và nhấp vào "Thêm".

### viết mã

Bây giờ bạn đã thêm thư viện OpenCV và mô hình Darknet Yolo vào dự án của bạn, bạn có thể viết mã.

Mã sau đây cho thấy cách sử dụng Yolo để phát hiện các đối tượng trong một hình ảnh:

`` `C#
// Tải mô hình Yolo.
var net = cv2.dnn.readnet ("yolov3.weights", "yolov3.cfg");

// Tạo một đốm màu từ hình ảnh.
var blob = cv2.dnn.blobfromimage (hình ảnh, 1.0/255, kích thước mới (416, 416), vô hướng mới (0, 0, 0), đúng, sai);

// Đặt blob đầu vào vào mạng.
net.setInput (blob);

// Chạy mạng.
var inputs = net.forward ();

// Nhận các hộp giới hạn và điểm số tự tin cho các đối tượng.
hộp var = đầu ra [0] .getMatarray ();
điểm var = đầu ra [1] .getMatarray ();

// Vẽ các hộp giới hạn và điểm số tự tin trên hình ảnh.
for (int i = 0; i <hộp.Rows (); i ++) {
// Nhận tọa độ hộp giới hạn.
var x = hộp [i, 0];
var y = hộp [i, 1];
var w = hộp [i, 2];
var h = hộp [i, 3];

// Nhận được điểm tin cậy.
var niềm tin = điểm số ;

// Vẽ hộp giới hạn.
CV2.Rectangle (hình ảnh, điểm mới (x, y), điểm mới (x + w, y + h), vô hướng mới (0, 255, 0), 2);

// Vẽ điểm tin cậy.
cv2.puttext (hình ảnh, tự tin.toString (), điểm mới (x, y - 10), cv2.font_hers
=======================================
## YOLO C#

[Link to reference article](https://www.learnopencv.com/yolo-object-detection-with-python-and-opencv/)

YOLO (You Only Look Once) is a popular object detection algorithm that can be used to detect objects in images and videos. YOLO is fast and accurate, and it can be used to detect a wide variety of objects.

YOLO is implemented in C++, but it can also be used in other programming languages, such as C#. This tutorial will show you how to use YOLO in C#.

### Prerequisites

To follow this tutorial, you will need the following:

* A computer with Windows, macOS, or Linux
* Visual Studio 2019 or later
* The OpenCV library
* The Darknet YOLO model

### Getting Started

The first step is to install the OpenCV library. You can do this by following the instructions on the OpenCV website.

Once you have installed OpenCV, you can download the Darknet YOLO model. You can find the model on the Darknet website.

### Creating a Project

Once you have the OpenCV library and the Darknet YOLO model, you can create a new project in Visual Studio.

In Visual Studio, create a new C# project. Name the project "YOLO".

### Adding the OpenCV Library

The next step is to add the OpenCV library to your project. To do this, right-click on your project in the Solution Explorer and select "Add Reference". In the "Reference Manager" dialog box, select "Browse" and navigate to the location where you installed the OpenCV library. Select the "opencv.dll" file and click "Add".

### Adding the Darknet YOLO Model

The next step is to add the Darknet YOLO model to your project. To do this, right-click on your project in the Solution Explorer and select "Add" > "Existing Item". In the "Add Existing Item" dialog box, navigate to the location where you downloaded the Darknet YOLO model. Select the "yolov3.weights" file and click "Add".

### Writing the Code

Now that you have added the OpenCV library and the Darknet YOLO model to your project, you can write the code.

The following code shows how to use YOLO to detect objects in an image:

```c#
// Load the YOLO model.
var net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg");

// Create a blob from the image.
var blob = cv2.dnn.blobFromImage(image, 1.0/255, new Size(416, 416), new Scalar(0, 0, 0), true, false);

// Set the input blob to the network.
net.setInput(blob);

// Run the network.
var outputs = net.forward();

// Get the bounding boxes and confidence scores for the objects.
var boxes = outputs[0].getMatArray();
var scores = outputs[1].getMatArray();

// Draw the bounding boxes and confidence scores on the image.
for (int i = 0; i < boxes.rows(); i++) {
// Get the bounding box coordinates.
var x = boxes[i, 0];
var y = boxes[i, 1];
var w = boxes[i, 2];
var h = boxes[i, 3];

// Get the confidence score.
var confidence = scores;

// Draw the bounding box.
cv2.rectangle(image, new Point(x, y), new Point(x + w, y + h), new Scalar(0, 255, 0), 2);

// Draw the confidence score.
cv2.putText(image, confidence.ToString(), new Point(x, y - 10), cv2.FONT_HERS
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top