用deepstream50运行yolov5已经可以识别物体现在如何提取目标的坐标位置并打印出来
要提取目标的坐标位置并打印出来,可以在deepstream5.0的配置文件中添加一个元素,用于定义要执行的action。具体步骤如下:
-
打开deepstream5.0的配置文件,通常为
<your_project>/configs/deepstream-app/sourceX_<your_source>.com。 -
在
[primary-gie]部分下添加以下元素:[primary-gie] enable=1 gpu-id=0 model-engine-file=<path/to/your/trt/engine/file> batch-size=1 # add this line to define your custom action custom-lib-path=<path/to/your/custom/lib> custom-lib-name=<your_custom_lib_name>其中
<path/to/your/custom/lib>是你自定义的库文件的路径,<your_custom_lib_name>是库文件的名称。 -
创建一个C++文件,用于定义你的自定义action。文件名可以任意取,这里假设为
my_custom_action.cpp。 -
在
my_custom_action.cpp文件中添加以下代码:#include <iostream> #include <vector> #include "nvdsinfer_context.h" #include "nvdsinfer_custom_impl.h" #include "nvdsinfer_customparser.h" #include "nvdsinfer_custombboxparser.h" using namespace std; extern "C" bool my_custom_action_impl (NvDsInferContext *ctx, NvDsInferContextBatchInput *inputLayersData, NvDsInferContextBatchOutput *outputLayersData, void *userCtx); bool my_custom_action_impl (NvDsInferContext *ctx, NvDsInferContextBatchInput *inputLayersData, NvDsInferContextBatchOutput *outputLayersData, void *userCtx) { // get the output layer data NvDsInferContextBuffer outputLayerBuffer = outputLayersData->inferBuffers[0]; // get the output layer dimensions NvDsInferDims outputLayerDims = outputLayerBuffer->dims; // get the number of detections int numDetections = outputLayerDims.numElements / outputLayerDims.d[0]; // get the output layer data as float32 float *outputLayerData = (float *) outputLayerBuffer->data; // iterate over the detections for (int i = 0; i < numDetections; i++) { // get the detection data float *detectionData = outputLayerData + i * outputLayerDims.d[1]; // get the confidence score float confidenceScore = detectionData[2]; // check if the confidence score is above a threshold if (confidenceScore > 0.5) { // get the class ID int classId = (int) detectionData[1]; // get the bounding box coordinates (in normalized format) float xMin = detectionData[3]; float yMin = detectionData[4]; float xMax = detectionData[5]; float yMax = detectionData[6]; // convert the bounding box coordinates to pixel format int width = outputLayerDims.d[3]; int height = outputLayerDims.d[2]; int x1 = xMin * width; int y1 = yMin * height; int x2 = xMax * width; int y2 = yMax * height; // print the coordinates to the console cout << "Class: " << classId << ", Coordinates: (" << x1 << ", " << y1 << ", " << x2 << ", " << y2 << ")" << endl; } } return true; }这个函数会被deepstream5.0调用,用于执行你的自定义action。在这个函数中,我们首先获取输出层数据,并计算出有多少个检测结果。然后,我们遍历所有检测结果,并获取每个检测结果的置信度、类别ID和边界框坐标。最后,我们将边界框坐标转换为像素格式,并打印到控制台上。
-
编译
my_custom_action.cpp文件,并将生成的库文件放到之前配置文件中的<path/to/your/custom/lib>路径下。 -
运行deepstream5.0应用程序,你会看到控制台输出所有检测结果的边界框坐标
原文地址: https://www.cveoy.top/t/topic/ezQ1 著作权归作者所有。请勿转载和采集!