Unable to display text using nvosd_put_text()

I am modifying the sample program backend and am unable to get nvosd_put_text() to work as expected. The API declared in nvosd.h is clear enough but nothing gets displayed on the screen. None of the provided examples use this function so I may well be doing something incorrectly. My suspicion is that I specified the font name incorrectly.

Attached is the snippet of code I added to v4l2_backend_main.cpp. This was inserted after the block containing the call to nvosd_draw_rectangles(). I have also shown this as a patch file at the bottom of the message.

char text[] = "Hello world";
            char font[] = "Arial";

            NvOSD_TextParams textParams;
            textParams.display_text = text;
            textParams.x_offset = textX;
            textParams.y_offset = textY;
            textParams.font_params.font_name = font;
            textParams.font_params.font_size = 18;
            textParams.font_params.font_color.red = 1.0;
            textParams.font_params.font_color.green = 0.0;
            textParams.font_params.font_color.blue = 1.0;
            textParams.font_params.font_color.alpha = 1.0;
            nvosd_put_text(ctx->nvosd_context, MODE_CPU, buffer->planes[0].fd, 1, &textParams);

Thank you for your help.

— v4l2_backend_main.cpp 2017-08-04 17:26:38.962814334 -0600
+++ /home/nvidia/tegra_multimedia_api/samples/backend/v4l2_backend_main.cpp 2017-03-01 22:15:06.000000000 -0700
@@ -76,9 +76,6 @@
#define IS_NAL_UNIT_START1(buffer_ptr) (!buffer_ptr[0] && !buffer_ptr[1] &&
(buffer_ptr[2] == 1))

-const int textX = 800;
-const int textY = 50;

using namespace std;

#ifdef ENABLE_GIE
@@ -434,25 +431,7 @@
nvosd_draw_rectangles(ctx->nvosd_context, MODE_HW,
buffer->planes[0].fd, temp_bbox.g_rect_num, temp_bbox.g_rect);
}

#endif

  •        //////////////////// Brad's attempt to add text to the OSD
    
  •        char text[] = "Hello world";
    
  •        char font[] = "Arial";
    
  •        NvOSD_TextParams textParams;
    
  •        textParams.display_text = text;
    
  •        textParams.x_offset = textX;
    
  •        textParams.y_offset = textY;
    
  •        textParams.font_params.font_name = font;
    
  •        textParams.font_params.font_size = 18;
    
  •        textParams.font_params.font_color.red = 1.0;
    
  •        textParams.font_params.font_color.green = 0.0;
    
  •        textParams.font_params.font_color.blue = 1.0;
    
  •        textParams.font_params.font_color.alpha = 1.0;
    
  •        nvosd_put_text(ctx->nvosd_context, MODE_CPU, buffer->planes[0].fd, 1, &textParams);
    
  •        //////////////////// End of hacking in some text
    
  •        // EglRenderer requires the fd of the 0th plane to render
           ctx->renderer->render(buffer->planes[0].fd);
           // Write raw video frame to file and return the buffer to converter
    

@@ -534,7 +513,6 @@

#ifdef ENABLE_GIE

void *gie_thread(void *data)
{
int buf_num = 0;

1 Like

HI bmears,

I am checking this internally. Thanks for your patience.

HI bmears,

Because nvosd_put_text with MODE_CPU only accepts RGBA buffer and backend sample is using YUV buffer, you need to convert it to RGBA buffer first.

1 Like

A simple patch for you by using 02_video_dec_cuda

---
 .../samples/02_video_dec_cuda/videodec.h           |  1 +
 .../samples/02_video_dec_cuda/videodec_main.cpp    | 25 +++++++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec.h b/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec.h
index 00d86c4..2727ad3 100644
--- a/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec.h
+++ b/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec.h
@@ -73,6 +73,7 @@ typedef struct
     char *osd_file_path;
     std::ifstream *osd_file;
     NvOSD_RectParams g_rect[MAX_RECT_NUM];
+    NvOSD_TextParams textParams;
     int  g_rect_num;
 
     std::queue < NvBuffer * > *conv_output_plane_buf_queue;
diff --git a/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec_main.cpp b/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec_main.cpp
index 08b8c5d..21b2ab1 100644
--- a/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec_main.cpp
+++ b/multimedia_api/ll_samples/samples/02_video_dec_cuda/videodec_main.cpp
@@ -162,6 +162,20 @@ abort(context_t *ctx)
         pthread_cond_broadcast(&ctx->queue_cond);
     }
 }
+static void
+set_text(context_t* ctx)
+{
+
+    ctx->textParams.display_text = strdup("Hello world");
+    ctx->textParams.x_offset = 400;
+    ctx->textParams.y_offset = 30;
+    ctx->textParams.font_params.font_name = strdup("Arial");
+    ctx->textParams.font_params.font_size = 18;
+    ctx->textParams.font_params.font_color.red = 1.0;
+    ctx->textParams.font_params.font_color.green = 0.0;
+    ctx->textParams.font_params.font_color.blue = 1.0;
+    ctx->textParams.font_params.font_color.alpha = 1.0;
+}
 
 static void
 get_rect(context_t *ctx)
@@ -352,6 +366,12 @@ conv0_capture_dqbuf_thread_callback(struct v4l2_buffer *v4l2_buf,
                               ctx->g_rect_num,
                               ctx->g_rect);
      }
+        set_text(ctx);
+        nvosd_put_text(ctx->nvosd_context,
+                              MODE_CPU,
+                              buffer->planes[0].fd,
+                              1,
+                              &ctx->textParams);
 
     // Write raw video frame to file and return the buffer to converter
     // capture plane
@@ -530,7 +550,7 @@ query_and_set_capture(context_t * ctx)
                    error);
 
         ret = ctx->conv->setCapturePlaneFormat((ctx->out_pixfmt == 1 ?
-                                                    V4L2_PIX_FMT_NV12M :
+                                                    V4L2_PIX_FMT_ABGR32 :
                                                     V4L2_PIX_FMT_YUV420M),
                                                 crop.c.width,
                                                 crop.c.height,
@@ -810,6 +830,7 @@ main(int argc, char *argv[])
         return -1;
     }
 
+	ctx.nvosd_context = nvosd_create_context();
     if (ctx.enable_osd) {
         ctx.nvosd_context = nvosd_create_context();
         cout << "ctx.osd_file_path:" << ctx.osd_file_path << endl;
@@ -1050,6 +1071,8 @@ cleanup:
         nvosd_destroy_context(ctx.nvosd_context);
         ctx.nvosd_context = NULL;
     }
+        nvosd_destroy_context(ctx.nvosd_context);
+        ctx.nvosd_context = NULL;
 
     if (error)
     {
-- 
2.1.4

I have a similar question on the NvOSD circle and arrow functions. They both throw an “unsupported color format” error on NV12 and ARGB32 type NvBuffers, using either MODE_HW or MODE_CPU. Are these implemented? If so, what type of buffer do they work with? thanks!

Please share a patch on 02_video_dec_cuda so that we can check the issue. Thanks.

A BRGA buffer does work with circles and arrows, but the RGBA does not. I can move forward, since I have a solution. Thanks.