/* * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include "gstnvdsmeta.h" #include "nvds_analytics_meta.h" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include #include #include "nvbufsurface.h" #include "nvbufsurftransform.h" /* parse_nvdsanalytics_meta_data * and extract nvanalytics metadata etc. */ extern "C" bool parse_nvdsanalytics_meta_data (NvDsObjectMeta *obj_meta) { gboolean objectIsAnalytics = FALSE; for (NvDsMetaList *l_user_meta = obj_meta->obj_user_meta_list; l_user_meta != NULL; l_user_meta = l_user_meta->next) { NvDsUserMeta *user_meta = (NvDsUserMeta *) (l_user_meta->data); if(user_meta->base_meta.meta_type == NVDS_USER_OBJ_META_NVDSANALYTICS) { // g_print("in parse_nvdsanalytics_meta_data %d"); objectIsAnalytics = TRUE; } } return objectIsAnalytics; } extern "C" char* enc_object2base64(GstBuffer * buf) { static int dump = 0; char *er = new char [5]; strcpy(er, "error"); if (dump < 150) { GstMapInfo in_map_info; NvBufSurface *surface = NULL; memset (&in_map_info, 0, sizeof (in_map_info)); if (!gst_buffer_map (buf, &in_map_info, GST_MAP_READ)) { g_print ("Error: Failed to map gst buffer\n"); gst_buffer_unmap (buf, &in_map_info); return er; } // cudaError_t cuda_err; NvBufSurfTransformRect src_rect, dst_rect; surface = (NvBufSurface *) in_map_info.data; int batch_size= surface->batchSize; // printf("\nBatch Size : %d, resolution : %dx%d \n",batch_size, // surface->surfaceList[0].width, surface->surfaceList[0].height); src_rect.top = 0; src_rect.left = 0; src_rect.width = (guint) surface->surfaceList[0].width; src_rect.height= (guint) surface->surfaceList[0].height; dst_rect.top = 0; dst_rect.left = 0; dst_rect.width = (guint) surface->surfaceList[0].width; dst_rect.height= (guint) surface->surfaceList[0].height; NvBufSurfTransformParams nvbufsurface_params; nvbufsurface_params.src_rect = &src_rect; nvbufsurface_params.dst_rect = &dst_rect; nvbufsurface_params.transform_flag = NVBUFSURF_TRANSFORM_CROP_SRC | NVBUFSURF_TRANSFORM_CROP_DST; nvbufsurface_params.transform_filter = NvBufSurfTransformInter_Default; NvBufSurface *dst_surface = NULL; NvBufSurfaceCreateParams nvbufsurface_create_params; /* An intermediate buffer for NV12/RGBA to BGR conversion will be * required. Can be skipped if custom algorithm can work directly on NV12/RGBA. */ nvbufsurface_create_params.gpuId = surface->gpuId; nvbufsurface_create_params.width = (gint) surface->surfaceList[0].width; nvbufsurface_create_params.height = (gint) surface->surfaceList[0].height; nvbufsurface_create_params.size = 0; nvbufsurface_create_params.colorFormat = NVBUF_COLOR_FORMAT_RGBA; nvbufsurface_create_params.layout = NVBUF_LAYOUT_PITCH; nvbufsurface_create_params.memType = NVBUF_MEM_DEFAULT; if(cudaSetDevice (surface->gpuId) != cudaSuccess){ g_print("cudaSetDevice error\n"); } cudaStream_t cuda_stream; if(cudaStreamCreate (&cuda_stream) != cudaSuccess){ g_print("cudaStreamCreate error\n"); } NvBufSurfaceCreate(&dst_surface,batch_size,&nvbufsurface_create_params); NvBufSurfTransformConfigParams transform_config_params; NvBufSurfTransform_Error err; transform_config_params.compute_mode = NvBufSurfTransformCompute_Default; transform_config_params.gpu_id = surface->gpuId; transform_config_params.cuda_stream = cuda_stream; err = NvBufSurfTransformSetSessionParams (&transform_config_params); if (err != NvBufSurfTransformError_Success) { g_print ("NvBufSurfTransformSetSessionParams failed with error %d while converting buffer\n", err); } NvBufSurfaceMemSet (dst_surface, 0, 0, 0); err = NvBufSurfTransform (surface, dst_surface, &nvbufsurface_params); if (err != NvBufSurfTransformError_Success) { g_print ("NvBufSurfTransform failed with error %d while converting buffer\n", err); } NvBufSurfaceMap (dst_surface, 0, 0, NVBUF_MAP_READ); NvBufSurfaceSyncForCpu (dst_surface, 0, 0); cv::Mat bgr_frame = cv::Mat (cv::Size(nvbufsurface_create_params.width, nvbufsurface_create_params.height), CV_8UC3); cv::Mat in_mat = cv::Mat (nvbufsurface_create_params.height, nvbufsurface_create_params.width, CV_8UC4, dst_surface->surfaceList[0].mappedAddr.addr[0], dst_surface->surfaceList[0].pitch); cv::cvtColor (in_mat, bgr_frame, cv::COLOR_RGBA2BGR); std::string encoded_jpeg; std::vector encode_buffer; cv::imencode(".jpg",bgr_frame,encode_buffer); auto base64_jpeg = reinterpret_cast(encode_buffer.data()); encoded_jpeg = g_base64_encode(base64_jpeg, encode_buffer.size()); char *encoded_jpeg_out = new char[encoded_jpeg.length() + 1]; strcpy(encoded_jpeg_out, encoded_jpeg.c_str()); // std::ofstream out("output.txt"); // out << encoded_jpeg; // out.close(); // char filename[64]; // snprintf(filename, 64, "/tmp/timage%03d.jpg", dump); // cv::imwrite(filename,bgr_frame); // dump ++; NvBufSurfaceUnMap (dst_surface, 0, 0); NvBufSurfaceDestroy (dst_surface); cudaStreamDestroy (cuda_stream); gst_buffer_unmap (buf, &in_map_info); return encoded_jpeg_out; } else { return er; } }