Guest User

Untitled

a guest
Jan 8th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.23 KB | None | 0 0
  1.     auto startPoint = std::chrono::high_resolution_clock::now();
  2.     char path[] = "/opt/nvidia/deepstream/deepstream-4.0/samples/configs/deepstream-app/tracker_config.yml";
  3.  
  4.     NvMOTQuery query{};
  5.     {
  6.         const auto status = NvMOT_Query( sizeof( path ), path, &query );
  7.         if( status != NvMOTStatus_OK ) {
  8.             std::cout << "Error";
  9.         }
  10.     }
  11.  
  12.     /* INIT */
  13.  
  14.     void* yDevMem;
  15.     const uint32_t width = static_cast< uint32_t >( m_currentFrame.cols );
  16.     const uint32_t height = static_cast< uint32_t >( m_currentFrame.rows );
  17.     const uint32_t pitch = static_cast< uint32_t >( m_currentFrame.step1() );
  18.     //cudaMallocManaged( &yDevMem, width * height, cudaMemAttachGlobal );
  19.     yDevMem = m_currentFrame.data;
  20.     const uint32_t fullSize = pitch * height;
  21.  
  22.     static bool initCompleted{ false };
  23.     static NvMOTContextHandle pContextHandle{};
  24.  
  25.     if( !initCompleted ) {
  26.         // IN params
  27.         NvMOTPerTransformBatchConfig batchConfig[ 1 ]{};
  28.         batchConfig->bufferType = NVBUF_MEM_CUDA_UNIFIED;
  29.         batchConfig->colorFormat = NVBUF_COLOR_FORMAT_GRAY8;
  30.         batchConfig->maxHeight = height;
  31.         batchConfig->maxPitch = pitch;
  32.         batchConfig->maxSize = fullSize;
  33.         batchConfig->maxWidth = width;
  34.  
  35.         NvMOTConfig pConfigIn{};
  36.         pConfigIn.computeConfig = NVMOTCOMP_CPU;                    /**< Compute target. see NvMOTCompute */
  37.         pConfigIn.maxStreams = 1;                                   /**< Maximum number of streams in a batch. */
  38.         pConfigIn.numTransforms = 1;                                /**< Number of NvMOTPerTransformBatchConfig entries in perTransformBatchConfig */
  39.         pConfigIn.perTransformBatchConfig = batchConfig;            /**< List of numTransform batch configs including type and resolution, one for each transform*/
  40.         pConfigIn.miscConfig.gpuId = 0;                             /**< GPU to be used. */
  41.         pConfigIn.miscConfig.maxObjPerBatch = 0;                    /**< Max number of objects to track per stream. 0 means no limit. */
  42.         pConfigIn.miscConfig.maxObjPerStream = 0;                   /**< Max number of objects to track per batch. 0 means no limit. */
  43.         pConfigIn.customConfigFilePathSize = sizeof( path ) ;       /**< The char length in customConfigFilePath */
  44.         pConfigIn.customConfigFilePath = path;                      /**< Path to the tracker's custom config file. Null terminated */
  45.  
  46.         // OUT Params
  47.         NvMOTConfigResponse pConfigResponse{};
  48.  
  49.         {
  50.             const auto status = NvMOT_Init( &pConfigIn, &pContextHandle, &pConfigResponse );
  51.             if( status != NvMOTStatus_OK ) {
  52.                 std::cout << "Error";
  53.             } else {
  54.                 initCompleted = true;
  55.             }
  56.         }
  57.     }
  58.  
  59.     /* PROCESS */
  60.  
  61.     // IN Params
  62.     NvBufSurfaceParams bufferParam[ 1 ]{};
  63.     bufferParam->width = width;                             /** width of buffer */
  64.     bufferParam->height = height;                           /** height of buffer */
  65.     bufferParam->pitch = pitch;                             /** pitch of buffer */
  66.     bufferParam->colorFormat = NVBUF_COLOR_FORMAT_GRAY8;        /** color format */
  67.     bufferParam->layout = NVBUF_LAYOUT_PITCH;               /** BL or PL for Jetson, ONLY PL in case of dGPU */
  68.     bufferParam->dataSize = fullSize;                           /** size of allocated memory */
  69.     bufferParam->dataPtr = yDevMem;                     /** pointer to allocated memory, Not valid for NVBUF_MEM_SURFACE_ARRAY and NVBUF_MEM_HANDLE */
  70.     bufferParam->planeParams.num_planes = 1;                        /** Number of planes */
  71.     bufferParam->planeParams.width[ 0 ] = width;                /** width of planes */
  72.     bufferParam->planeParams.height[ 0 ] = height;              /** height of planes */
  73.     bufferParam->planeParams.pitch[ 0 ] = pitch;                /** pitch of planes in bytes */
  74.     bufferParam->planeParams.offset[ 0 ] = 0;                       /** offsets of planes in bytes */
  75.     bufferParam->planeParams.psize[ 0 ] = pitch * height;   /** size of planes in bytes */
  76.     bufferParam->planeParams.bytesPerPix[ 0 ] = 1;                  /** bytes taken for each pixel */
  77.  
  78.     bufferParam->mappedAddr.addr[ 0 ] = yDevMem;            /** pointers of mapped buffers. Null Initialized values.*/
  79.     bufferParam->mappedAddr.eglImage = nullptr;
  80.     //bufferParam->bufferDesc;  /** dmabuf fd in case of NVBUF_MEM_SURFACE_ARRAY and NVBUF_MEM_HANDLE type memory. Invalid for other types. */
  81.  
  82.     NvBufSurfaceParams* bufferParamPtr{ bufferParam };
  83.  
  84.     static std::vector< NvMOTObjToTrack > inObjectVec{};
  85.     static std::vector< NvMOTTrackedObj > outObjectVec{};
  86.  
  87.     size_t currObjectCount{ 0 };
  88.     for( const TMetadataV2ObjectHeader::TObjectType currType: objectTypes ) {
  89.         currObjectCount += m_objects.at( currType ).size();
  90.     }
  91.     if( inObjectVec.size() < currObjectCount ) {
  92.         inObjectVec.resize( currObjectCount );
  93.     }
  94.     if( outObjectVec.size() < currObjectCount ) {
  95.         outObjectVec.resize( currObjectCount );
  96.     }
  97.  
  98.     size_t currInObjectIndex{ 0 };
  99.  
  100.     /* Iterate through the possible object types */
  101.     /* The inner loop operates on the vector of tracked objects from that type */
  102.     /* You can mock this section with generated objects */
  103.     for( const TMetadataV2ObjectHeader::TObjectType currObjType: objectTypes ) {
  104.         for( const auto& currObj: m_objects.at( currObjType ) ) {
  105.             NvMOTObjToTrack& currInObject = inObjectVec[ currInObjectIndex++ ];
  106.             currInObject.classId = static_cast< uint16_t >( currObjType );              /**< Class of the object to be tracked. */
  107.             currInObject.bbox.x = currObj->getBBoxImage().x;                /**< Bounding box. */
  108.             currInObject.bbox.y = currObj->getBBoxImage().y;
  109.             currInObject.bbox.width = currObj->getBBoxImage().width;
  110.             currInObject.bbox.height = currObj->getBBoxImage().height;
  111.             currInObject.confidence = 1.f;          /**< Detection confidence of the object. */
  112.             currInObject.doTracking = true;         /**< True: track this object.  False: do not initiate  tracking on this object. */
  113.         }
  114.     }
  115.  
  116.     NvMOTFrame frame{};
  117.     frame.streamID = 0;                             /**< The stream source for this frame. */
  118.     frame.frameNum = m_frameNum;                                /**< Frame number sequentially identifying the frame within a stream. */
  119.     frame.timeStamp = m_currFrameTimestamp;                         /**< Timestamp of the frame at the time of capture. */
  120.     frame.timeStampValid = true;                    /**< The timestamp value is properly populated. */
  121.     frame.doTracking = true;                        /**< True: track objects in this frame; False: do not track this frame. */
  122.     frame.reset = false;                            /**< True: reset tracking for the stream. */
  123.     frame.numBuffers = 1;                           /**< Number of entries in bufferList. */
  124.     frame.bufferList = &bufferParamPtr;             /**< Array of pointers to buffer params. */
  125.     frame.objectsIn.detectionDone = ( m_frameNum % 2 == 0 ); // We detect on every second image
  126.     frame.objectsIn.numAllocated = inObjectVec.size();
  127.     frame.objectsIn.numFilled = currObjectCount;
  128.     frame.objectsIn.list = inObjectVec.data();
  129.  
  130.     NvMOTProcessParams processParams{};
  131.     processParams.numFrames = 1;
  132.     processParams.frameList = &frame;
  133.  
  134.     // OUT Params
  135.     NvMOTTrackedObjBatch outTrackedBatch{};
  136.     NvMOTTrackedObjList outBatchObjects{};
  137.     outBatchObjects.list = outObjectVec.data();
  138.     outBatchObjects.streamID = 0;      /**< Stream associated with objects in the list. */
  139.     outBatchObjects.frameNum = m_frameNum;    /**< Frame number for objects in the list. */
  140.     outBatchObjects.valid = true;             /**< This entry in the batch is valid */
  141.     outBatchObjects.numAllocated = outObjectVec.size();  /**< Number of blocks allocated for the list. */
  142.     outBatchObjects.numFilled = /*outObjVec.size()*/0;     /**< Number of populated blocks in the list. */
  143.  
  144.     outTrackedBatch.numAllocated = 1;
  145.     outTrackedBatch.numFilled = 1;
  146.     outTrackedBatch.list = &outBatchObjects;
  147.  
  148.     {
  149.         const auto status = NvMOT_Process( pContextHandle, &processParams, &outTrackedBatch );
  150.         if( status != NvMOTStatus_OK ) {
  151.             std::cout << "Error";
  152.         }
  153.     }
  154.  
  155.     for( size_t outIndex = 0; outIndex < outBatchObjects.numFilled; ++outIndex ) {
  156.         const auto& currOutObj{ outObjectVec[ outIndex ] };
  157.         const auto currOutAssociated{ currOutObj.associatedObjectIn };
  158.         if( currOutAssociated != nullptr ) {
  159.             std::cout << "Ref [x: " << currOutAssociated->bbox.x << " y: " << currOutAssociated->bbox.y
  160.                       << " w: " << currOutAssociated->bbox.width << " h: " << currOutAssociated->bbox.height << "] "
  161.                       << " Tracked [x: " << currOutObj.bbox.x << " y: " << currOutObj.bbox.y
  162.                       << " w: " << currOutObj.bbox.width << " h: " << currOutObj.bbox.height << "]" << std::endl;
  163.         } else {
  164.             std::cout << "No association" << std::endl;
  165.         }
  166.     }
  167.  
  168.     auto endPoint = std::chrono::high_resolution_clock::now();
  169.     std::chrono::duration<float, std::milli> fdur = endPoint - startPoint;
  170.     std::cout << "Count: " << inObjectVec.size() << " Runtime: " << fdur.count() << " Avg runtime: " << fdur.count() / inObjectVec.size() << std::endl;
Add Comment
Please, Sign In to add comment