@Fiona.Chen Thanks for help! Does that mean I can only run deepstream properly on a native linux server or pc? Is there a way to develop a deepstream app in a virtual machine on windows?
Besides can I use opencv in bbox_generated_probe_after_analytics() to extract image without changing the pipeline?
How can I extract images from surface objects if there is more than 1 stream src - #16 by st123010
I learn it from upper post but I met segmentation fault when converting images.
static void
bbox_generated_probe_after_analytics (AppCtx * appCtx, GstBuffer * buf,
NvDsBatchMeta * batch_meta, guint index)
{
......
GstMapInfo inmap = GST_MAP_INFO_INIT;
if (!gst_buffer_map (buf, &inmap, GST_MAP_READ)) {
GST_ERROR ("input buffer mapinfo failed");
return;
}
NvBufSurface *ip_surf = (NvBufSurface *) inmap.data;
const gchar *calc_enc_str = g_getenv ("CALCULATE_ENCODE_TIME");
gboolean calc_enc = !g_strcmp0 (calc_enc_str, "yes");
int countTest = 0;
NvBufSurface *uni_surf = nullptr;
if (ip_surf->memType == NVBUF_MEM_CUDA_DEVICE) {
NvBufSurfaceCreateParams create_params;
memset(&create_params, 0, sizeof(NvBufSurfaceCreateParams));
create_params.gpuId = ip_surf->gpuId;
create_params.width = ip_surf->surfaceList[0].width;
create_params.height = ip_surf->surfaceList[0].height;
create_params.layout = NVBUF_LAYOUT_PITCH;
create_params.size = 0;
create_params.colorFormat = ip_surf->surfaceList[0].colorFormat;
create_params.memType = NVBUF_MEM_CUDA_UNIFIED; // 或者 NVBUF_MEM_HOST
NvBufSurfTransformRect src_rect, dst_rect;
int batch_size= ip_surf->batchSize;
src_rect.top = 0;
src_rect.left = 0;
src_rect.width = (guint) ip_surf->surfaceList[0].width;
src_rect.height= (guint) ip_surf->surfaceList[0].height;
dst_rect.top = 0;
dst_rect.left = 0;
dst_rect.width = (guint) ip_surf->surfaceList[0].width;
dst_rect.height= (guint) ip_surf->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;
cudaError_t cuda_err;
cuda_err = cudaSetDevice (ip_surf->gpuId);
cudaStream_t cuda_stream;
cuda_err=cudaStreamCreate (&cuda_stream);
NvBufSurfTransformConfigParams transform_config_params;
NvBufSurfTransform_Error err;
transform_config_params.compute_mode = NvBufSurfTransformCompute_Default;
transform_config_params.gpu_id = ip_surf->gpuId;
transform_config_params.cuda_stream = cuda_stream;
err = NvBufSurfTransformSetSessionParams (&transform_config_params);
if (NvBufSurfaceCreate(&uni_surf, ip_surf->batchSize, &create_params) != 0) {
g_print("Failed to create NvBufSurface for unified memory\n");
gst_buffer_unmap(buf, &inmap);
return;
}
NvBufSurfaceMemSet (uni_surf, -1, -1, 0);
err = NvBufSurfTransform (ip_surf, uni_surf, &nvbufsurface_params);
if (err != NvBufSurfTransformError_Success) {
g_print ("NvBufSurfTransform failed with error %d while converting buffer\n", err);
return;
}
NvBufSurfaceMap(uni_surf, -1, -1, NVBUF_MAP_READ);
NvBufSurfaceSyncForCpu(ip_surf, -1, -1);
printf("uni_surf->gpuId = %d\n", uni_surf->gpuId);
printf("uni_surf->batchSize = %d\n", uni_surf->batchSize);
printf("uni_surf->memType = %d\n", uni_surf->memType);
printf("uni_surf->surfaceList = %p\n", uni_surf->surfaceList);
printf("uni_surf->surfaceList size = %d\n", sizeof(uni_surf->surfaceList)/sizeof(uni_surf->surfaceList[0]));
printf("uni_surf->numFilled = %d\n", uni_surf->numFilled);
int num_p = uni_surf->surfaceList[0].planeParams.num_planes;
for (NvDsMetaList * l_frame = batch_meta->frame_meta_list; l_frame != NULL;
l_frame = l_frame->next) {
NvDsFrameMeta *frame_meta = (NvDsFrameMeta *) l_frame->data;
int sur_id = frame_meta->batch_id;
printf("uni_surf->surfaceList[%d] = %p\n", sur_id, uni_surf->surfaceList[sur_id]);
printf("uni_surf->surfaceList[%d].colorFormat = %d\n", sur_id, uni_surf->surfaceList[sur_id].colorFormat);
for (int j = 0; j < num_p; ++j) {
printf("uni_surf->surfaceList[%d].mappedAddr.addr[%d] = %p\n", sur_id, j, uni_surf->surfaceList[sur_id].mappedAddr.addr[j]);
printf("uni_surf->surfaceList[%d].planeParams.pitch[%d] = %d\n", sur_id, j, uni_surf->surfaceList[sur_id].planeParams.pitch[j]);
printf("uni_surf->surfaceList[%d].planeParams.width[%d] = %d\n", sur_id, j, uni_surf->surfaceList[sur_id].planeParams.width[j]);
printf("uni_surf->surfaceList[%d].planeParams.height[%d] = %d\n", sur_id, j, uni_surf->surfaceList[sur_id].planeParams.height[j]);
printf("uni_surf->surfaceList[%d].planeParams.offset[%d] = %d\n", sur_id, j, uni_surf->surfaceList[sur_id].planeParams.offset[j]);
printf("uni_surf->surfaceList[%d].planeParams.psize[%d] = %d\n", sur_id, j, uni_surf->surfaceList[sur_id].planeParams.psize[j]);
printf("uni_surf->surfaceList[%d].planeParams.bytesPerPix[%d] = %d\n", sur_id, j, uni_surf->surfaceList[sur_id].planeParams.bytesPerPix[j]);
}
void* y_plane = uni_surf->surfaceList[0].mappedAddr.addr[0];
void* uv_plane = uni_surf->surfaceList[0].mappedAddr.addr[1];
int height = uni_surf->surfaceList[0].planeParams.height[0];
int width = uni_surf->surfaceList[0].planeParams.width[0];
int step = uni_surf->surfaceList[0].planeParams.pitch[0];
printf("height = %d, width = %d, step = %d\n", height, width, step);
cv::Mat y_mat(height, width, CV_8UC1, y_plane, step);
cv::Mat uv_mat(height / 2, width / 2, CV_8UC2, uv_plane, step);
cv::Mat nv12_mat(height + height / 2, width, CV_8UC1);
// Segmentation fault immediately.
y_mat.copyTo(nv12_mat(cv::Rect(0, 0, width, height)));
uv_mat.copyTo(nv12_mat(cv::Rect(0, height, width, height / 2)));
cv::Mat rgb_mat;
cv::cvtColor(nv12_mat, rgb_mat, cv::COLOR_YUV2RGB_NV12);
std::vector<uchar> jpeg_buffer;
cv::imencode(".jpg", rgb_mat, jpeg_buffer);
printf("dot base64\n");
gchar* base64_jpeg = g_base64_encode(jpeg_buffer.data(), jpeg_buffer.size());
printf("Base64 JPEG:\n%s\n", base64_jpeg);
......
Do we have some similar examples?