Share yolo opencv c++

tunganhblue22

New member
### yolo với opencv trong c ++

Bạn chỉ nhìn một lần (Yolo) là một thuật toán phát hiện đối tượng hiện đại có thể được sử dụng để phát hiện các đối tượng trong hình ảnh và video.Đó là một thuật toán nhanh và chính xác rất dễ đào tạo và triển khai.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách sử dụng Yolo với OpenCV 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ó trình biên dịch C ++
* Cài đặt OpenCV
* Tệp cấu hình và trọng lượng yolo

** Tải xuống các tệp Cấu hình và Trọng lượng Yolo **

Các tệp Cấu hình và trọng số Yolo có thể được tải xuống từ [Trang web Yolo] (YOLO: Real-Time Object Detection).

** Đào tạo một mô hình yolo **

Bạn có thể đào tạo mô hình Yolo của riêng mình bằng khung [Darknet] (https://pjreddie.com/darknet/).Tuy nhiên, đối với hướng dẫn này, chúng tôi sẽ sử dụng mô hình Yolo được đào tạo trước.

** Chạy Yolo với OpenCV **

Để chạy Yolo với OpenCV, bạn sẽ cần tạo đối tượng `cv :: Videocapture` để đọc video đầu vào và đối tượng` cv :: dnn :: net` để tải mô hình yolo.Sau đó, bạn có thể sử dụng hàm `cv :: dnn :: net :: forward ()` để phát hiện các đối tượng trong video.

Mã sau đây cho thấy cách sử dụng Yolo với OpenCV để phát hiện các đối tượng trong video:

`` `C ++
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main ()
{
// Tạo một đối tượng Videocapture để đọc video đầu vào.
cv :: Videocapture Cap ("input.mp4");

// Tải mô hình Yolo.
cv :: dnn :: net net = cv :: dnn :: readnetfromdarknet ("yolov3.weights", "yolov3.cfg");

// Tạo một cửa sổ để hiển thị video đầu ra.
CV :: Được đặt tênWindow ("đầu ra");

// Loop trên các khung của video.
while (đúng)
{
// Nhận khung tiếp theo từ video.
CV :: Khung Mat;
Cap >> khung;

// Nếu khung trống, thoát ra khỏi vòng lặp.
if (frame.empty ())
{
phá vỡ;
}

// Phát hiện các đối tượng trong khung bằng Yolo.
std :: vector <cv :: dnn :: DetectionObject> phát hiện;
net.detect (khung, phát hiện);

// Vẽ các hộp giới hạn xung quanh các đối tượng được phát hiện.
for (const auto & phát hiện: phát hiện)
{
CV :: Hình chữ nhật (khung, phát hiện.boundingbox, CV :: Scalar (0, 255, 0));
}

// Hiển thị video đầu ra.
CV :: imshow ("đầu ra", khung);

// Đợi một máy ép chìa khóa.
int key = CV :: Waitkey (1);

// Nếu người dùng nhấn phím "Q", hãy thoát ra khỏi vòng lặp.
if (key == 'q')
{
phá vỡ;
}
}

trả lại 0;
}
`` `

** Đầu ra **

Đầu ra của thuật toán Yolo sẽ là danh sách các hộp giới hạn xung quanh các đối tượng được phát hiện.Các hộp giới hạn sẽ được vẽ trên video đầu ra.

** hashtags **

* #yolo
* #OpenCV
* #C ++
* #object phát hiện
* #Tầm nhìn máy tính
=======================================
### YOLO with OpenCV in C++

You Only Look Once (YOLO) is a state-of-the-art object detection algorithm that can be used to detect objects in images and videos. It is a fast and accurate algorithm that is easy to train and deploy. In this tutorial, we will show you how to use YOLO with OpenCV in C++.

**Prerequisites**

To follow this tutorial, you will need the following:

* A computer with a C++ compiler
* OpenCV installed
* The YOLO weights and configuration files

**Downloading the YOLO weights and configuration files**

The YOLO weights and configuration files can be downloaded from the [YOLO website](https://pjreddie.com/darknet/yolo/).

**Training a YOLO model**

You can train your own YOLO model using the [Darknet](https://pjreddie.com/darknet/) framework. However, for this tutorial, we will be using a pre-trained YOLO model.

**Running YOLO with OpenCV**

To run YOLO with OpenCV, you will need to create a `cv::VideoCapture` object to read the input video, and a `cv::dnn::Net` object to load the YOLO model. You can then use the `cv::dnn::Net::forward()` function to detect objects in the video.

The following code shows how to use YOLO with OpenCV to detect objects in a video:

```c++
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>

int main()
{
// Create a VideoCapture object to read the input video.
cv::VideoCapture cap("input.mp4");

// Load the YOLO model.
cv::dnn::Net net = cv::dnn::readNetFromDarknet("yolov3.weights", "yolov3.cfg");

// Create a window to display the output video.
cv::namedWindow("Output");

// Loop over the frames of the video.
while (true)
{
// Get the next frame from the video.
cv::Mat frame;
cap >> frame;

// If the frame is empty, break out of the loop.
if (frame.empty())
{
break;
}

// Detect objects in the frame using YOLO.
std::vector<cv::dnn::DetectionObject> detections;
net.detect(frame, detections);

// Draw the bounding boxes around the detected objects.
for (const auto& detection : detections)
{
cv::rectangle(frame, detection.boundingBox, cv::Scalar(0, 255, 0));
}

// Display the output video.
cv::imshow("Output", frame);

// Wait for a key press.
int key = cv::waitKey(1);

// If the user pressed the "q" key, break out of the loop.
if (key == 'q')
{
break;
}
}

return 0;
}
```

**Output**

The output of the YOLO algorithm will be a list of bounding boxes around the detected objects. The bounding boxes will be drawn on the output video.

**Hashtags**

* #yolo
* #OpenCV
* #C++
* #object detection
* #computer vision
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top