Vous êtes sur la page 1sur 9

Decodage yolo.

cpp
https://github.com/doleron/yolov5-opencv-cpp-python/blob/main/cpp/
yolo.cpp
The code snippet provided is an implementation of You Only Look Once version 5
(YOLOv5) for object detection using OpenCV in C++ programming language. This
code reads the classes list and the pre-trained model configuration file from disk,
loads the YOLOv5 model into memory, and applies it to input images for object
detection [1].

The main function in this code is the detect function, which takes an input image, the
YOLOv5 neural network, a list of class names, and an output vector to store the
detected objects' information, such as class ID, confidence, and bounding box
coordinates. This function resizes the input image, converts it into a blob, feeds it into
the neural network, and extracts the output predictions. It then processes the
predictions to extract the relevant information and store them in the output vector
[2].

The YOLOv5 model is loaded using the load_net function, which reads the pre-trained
weights from the onnx model file and sets the target backend to either OpenCV or
CUDA if available [3].

In conclusion, the provided code is a C++ implementation of the YOLOv5 object


detection algorithm, which can be used to detect objects in images with high
accuracy. It uses OpenCV and reads the pre-trained model configuration from disk.
The detect function is responsible for detecting objects in input images and storing
the relevant information in an output vector. The code can be modified to detect
objects in videos or real-time streams as well.

L'extrait de code fourni est une implémentation de You Only Look Once version 5 (YOLOv5)
pour la détection d'objets à l'aide d'OpenCV dans le langage de programmation C++. Ce
code lit la liste des classes et le fichier de configuration du modèle pré-formé à partir du
disque, charge le modèle YOLOv5 en mémoire et l'applique aux images d'entrée pour la
détection d'objets [1].

La fonction principale de ce code est la fonction de détection, qui prend une image
d'entrée, le réseau de neurones YOLOv5, une liste de noms de classe et un vecteur de
sortie pour stocker les informations des objets détectés, telles que l'ID de classe, la
confiance et la boîte englobante. coordonnées. Cette fonction redimensionne l'image
d'entrée, la convertit en un blob, l'introduit dans le réseau de neurones et extrait les
prédictions de sortie. Il traite ensuite les prédictions pour extraire les informations
pertinentes et les stocker dans le vecteur de sortie [2].

Le modèle YOLOv5 est chargé à l'aide de la fonction load_net, qui lit les poids pré-formés à
partir du fichier de modèle onnx et définit le backend cible sur OpenCV ou CUDA si
disponible [3].

En conclusion, le code fourni est une implémentation C++ de l'algorithme de détection


d'objets YOLOv5, qui peut être utilisé pour détecter des objets dans des images avec une
grande précision. Il utilise OpenCV et lit la configuration du modèle pré-formé à partir du
disque. La fonction de détection est responsable de la détection des objets dans les images
d'entrée et du stockage des informations pertinentes dans un vecteur de sortie. Le code
peut également être modifié pour détecter des objets dans des vidéos ou des flux en
temps réel.

This code is for object detection using YOLOv5 in C++. It reads a pre-trained model
and detects objects in an image. The code includes functions for loading the class list
and the pre-trained model, formatting the input image, and performing the
detection.

The load_class_list() function loads a list of class names from a text file [1]. The load_net()
function reads a pre-trained YOLOv5 model from a file and sets the preferable
backend and target for running the model on either CPU or GPU. The format_yolov5()
function formats the input image to a square image with zero-padding, preserving
the aspect ratio. The detect() function takes the formatted input image, runs it through
the pre-trained model, and returns the detected objects in a vector of Detection
structs, which contain the class ID, confidence score, and bounding box of each
detected object. The detect() function also takes the class name list as input [2][3].

Overall, this code appears to be a simple implementation of YOLOv5 in C++ for


object detection in images. It can be used as a basis for more complex applications
involving YOLOv5, such as video object detection, object tracking, and real-time
object detection, with appropriate modifications to the code.

Ce code est destiné à la détection d'objets à l'aide de YOLOv5 en C++. Il lit un modèle pré-
formé et détecte des objets dans une image. Le code comprend des fonctions permettant
de charger la liste de classes et le modèle pré-formé, de formater l'image d'entrée et
d'effectuer la détection.
La fonction load_class_list() charge une liste de noms de classes à partir d'un fichier texte
[1]. La fonction load_net() lit un modèle YOLOv5 pré-formé à partir d'un fichier et définit le
backend et la cible préférables pour exécuter le modèle sur CPU ou GPU. La fonction
format_yolov5() formate l'image d'entrée en une image carrée avec zéro-padding, en
préservant le rapport d'aspect. La fonction detect() prend l'image d'entrée formatée,
l'exécute dans le modèle pré-formé et renvoie les objets détectés dans un vecteur de
structures de détection, qui contiennent l'ID de classe, le score de confiance et le cadre de
délimitation de chaque objet détecté. La fonction detect() prend également la liste des
noms de classe en entrée [2][3].

Dans l'ensemble, ce code semble être une implémentation simple de YOLOv5 en C++ pour
la détection d'objets dans les images. Il peut être utilisé comme base pour des applications
plus complexes impliquant YOLOv5, telles que la détection d'objets vidéo, le suivi d'objets
et la détection d'objets en temps réel, avec les modifications appropriées du code.

To display the number of detections by class in the top left corner of the frame using
C++, you can use the OpenCV function putText [1].

Here is an example of how to use putText to display text on an image:

cv::putText(image, "Detection: " + std::to_string(num_detection), cv::Point(10, 50),


cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 2);

In this example, image is the input image, "Detection: " + std::to_string(num_detection) is


the text to display, cv::Point(10, 50) is the position of the text in the image,
cv::FONT_HERSHEY_SIMPLEX is the font type, 1 is the font scale, cv::Scalar(0, 0, 255) is
the text color (in BGR format), and 2 is the thickness of the text.

To get the number of detections per class, you can iterate through the output vector,
which contains a list of detections. You can use the Detection struct defined in the
code to get the class ID for each detection. You can then count the number of
detections per class using a map or a vector. Here is an example of how to count the
number of detections per class:
In this example, detection_counts is a map that counts the number of detections for
each class. The key is the class ID and the value is the number of detections.

To display the number of detections per class using putText, you can iterate through
the detection_counts map and display the count for each class. Here is an example:

int y_offset = 50;


for (auto &count : detection_counts) {
std::string class_name = class_list[count.first];
int num_detection = count.second;
cv::putText(image, class_name + ": " + std::to_string(num_detection), cv::Point(10, y_offset),
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 2);
y_offset += 50;

In this example, class_list is a vector of class names, and y_offset is the vertical position
of the text in the image. The loop iterates through the detection_counts map and
displays the count for each class using putText.

Note that you need to include the header file opencv2/imgproc.hpp to use putText.
Based on your code, you can use the cv::putText() function in OpenCV to display the
number of detections by class on the frame. This function takes in the image, the text
to be displayed, the position of the text, the font type, the font scale, the color of the
text and the thickness of the text as arguments.

To display the number of detections by class in the top left corner of the frame, you
can add the following code to the detect() function:

// Get the number of detections by class std::map< int , int > class_counts; for ( int i = 0 ; i < class_ids. size ();
i++) { int class_id = class_ids[i]; if (class_counts. find (class_id) == class_counts. end ())
{ class_counts[class_id] = 1 ; } else { class_counts[class_id]++; } } // Display the number of detections by
class on the frame cv::Scalar text_color = cv:: Scalar ( 0 , 0 , 255 ); // red color int font =
cv::FONT_HERSHEY_SIMPLEX; double font_scale = 0.5 ; int thickness = 1 ; int x = 10 ; int y = 20 ; for
( const auto & count : class_counts) { std::string text = className[count.first] + ": " +
std:: to_string (count.second); cv:: putText (image, text, cv:: Point (x, y), font, font_scale, text_color, thickness);
y += 20 ; }
This code will count the number of detections for each class, and then display the
results on the frame. You can adjust the font, font scale, color and thickness of the
text by modifying the parameters passed to cv::putText() function.

You can insert this code after the nms_result loop in the detect() function, as shown
below:
std::vector<int> nms_result;
cv::dnn::NMSBoxes(boxes, confidences, SCORE_THRESHOLD, NMS_THRESHOLD, nms_result);
for (int i = 0; i < nms_result.size(); i++) {
int idx = nms_result[i];
Detection result;
...
}

Note that this code assumes that you have a std::vector<std::string> className object that
contains the names of the classes. If you don't have this object, you can modify the
load_class_list() function to return a std::map<int, std::string> object instead, where the keys
are the class IDs and the values are the class names. You can then modify the detect() function
to take in this std::map<int, std::string> object as an additional argument.

Based on the provided code, you can add the number of detections per class to the
frame using the cv::putText() function from OpenCV.

To add this functionality, you need to insert the cv::putText() function inside the detect()
function, after counting the number of detections per class. Specifically, you need to
insert the following code right after the loop that counts the number of detections
per class:
for (int i = 0; i < className.size(); i++)
{
std::string class_name = className[i];
std::string count = std::to_string(class_counts[i]);
std::string text = class_name + ": " + count;
cv::putText(image, text, cv::Point(10, 30*(i+1)), cv::FONT_HERSHEY_SIMPLEX, 1, colors[i
%colors.size()], 2);
}

This code will iterate over the class names and the corresponding detection counts
and draw the text on the top left corner of the image. You can adjust the position,
font, and color of the text by modifying the cv::putText() function parameters.

Note that you need to add a new std::vector<int> variable to store the counts of each
class, which you can increment every time a detection is made for that class. You can
insert the following code right after the className variable is loaded from the classes.txt
file:

This code initializes a vector of zeros with the same size as className, which will be
used to store the counts of each class.

References:

1. OpenCV documentation for cv::putText(): [1]


2. OpenCV: Drawing Functions
3. OpenCV documentation for cv::Point(): [2]
4.
5. C++ documentation for std::to_string(): [3]
6. std::to_string - cppreference.com

Vous aimerez peut-être aussi