Whytehorse
Posts: 31
Joined: Wed Oct 22, 2014 4:03 am

UV4L Text Overlay info needed

Wed Mar 04, 2015 9:32 pm

Hello everyone!
I was wondering if anybody knows how to overlay the current time into the video captured by the uv4l drivers? I can turn on text overlay in uv4l-raspicam.conf and it works.. It pulls from a json text file. It even has a dynamic variable $fps that it shows. So what is the variable for time? I tried $t and $T and $date but nothing works and I can't find any documentation...

RpiName
Posts: 713
Joined: Sat Jul 06, 2013 3:14 am

Re: UV4L Text Overlay info needed

Wed Mar 04, 2015 10:41 pm

Whytehorse wrote: I was wondering if anybody knows how to overlay the current time into the video captured by the uv4l drivers? I can turn on text overlay in uv4l-raspicam.conf and it works.. It pulls from a json text file. It even has a dynamic variable $fps that it shows. So what is the variable for time? I tried $t and $T and $date but nothing works and I can't find any documentation...
According to the documentation, at the moment $fps is the only meta information available for text overlay.

jerry7580
Posts: 3
Joined: Mon Jan 25, 2016 11:00 pm

Re: UV4L Text Overlay info needed

Thu Jul 14, 2016 1:39 pm

Whytehorse wrote:Hello everyone!
I was wondering if anybody knows how to overlay the current time into the video captured by the uv4l drivers? I can turn on text overlay in uv4l-raspicam.conf and it works.. It pulls from a json text file. It even has a dynamic variable $fps that it shows. So what is the variable for time? I tried $t and $T and $date but nothing works and I can't find any documentation...
As far as I can tell, what you want to do requires a program to continuously update the json file and then notify the driver to reread that file. Here is my version of a program to do just that. Hope this helps, although with the passage of time you may already have some alternate resolution to this.

Code: Select all

/*
 * uv4l-time-overlay.cpp
 * g++ -Ofast -std=c++11 uv4l-time-overlay.cpp -o uv4l-time-overlay
 * Original Program: Text Scrolling Demo; Author Luca Risolia
 * 
 * This program will update the json file associated with uv4l_raspicam.
 * Text overlay must be enabled and the json text file must be identified
 * in /etc/uv4l/uv4l-raspicam.conf. This program is a stripped down/modified
 * version of uv4l-overlay.cpp that was downloaded from http://www.linux-projects.org.
 * This version assumes a 640 x 480 resolution and puts the current date at
 * bottom left with the current time at bottom right. It updates time and date
 * once per second. I am running this at 2 frames per second.
*/

#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>


int main(int argc, char **argv) {
  if (argc != 3) {
    std::cerr << "Usage: uv4l-overlay <video_device> <filename>\n";
    return EXIT_FAILURE;
  }
  std::cout << "Press Ctrl-C to terminate\n";
//simplex, plain, duplex, complex, triplex, complex_small, script_simplex, script_complex, italic

  char myAscTime[10];
  char myAscDate[10];
  time_t rawTime;
  std::ofstream jsonFile;
  
  for (;; sleep(5)) {
    auto fd = open(argv[1], O_RDWR | O_NONBLOCK);
    if (fd == -1) {
      std::cerr << "Failed to open " << argv[1] << '\n';
      continue;
    }
    struct v4l2_queryctrl queryctrl{};
    struct v4l2_control ctrl{0, 1};
    for (queryctrl.id = V4L2_CID_PRIVATE_BASE;; ++queryctrl.id) {
      if (!ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
        const std::string name{std::begin(queryctrl.name), std::end(queryctrl.name)};
        if (name.find("overlay") != std::string::npos) {
          ctrl.id = queryctrl.id;
          break;
        }
      } else {
        break;
      }
    }
    if (!ctrl.id) {
      std::cerr << "Text overlay not supported\n";
      return EXIT_FAILURE;
    }
    struct v4l2_format fmt{};
    do {
      time(&rawTime);
      strftime(myAscTime, 9, "%T", localtime(&rawTime));
      strftime(myAscDate, 9, "%D", localtime(&rawTime));
      jsonFile.open("/home/pi/text.json");
      jsonFile << "[\n  {\n";
      jsonFile << "    \"font\": \"script_complex\",\n";
      jsonFile << "    \"scale\": 1,\n";
      jsonFile << "    \"thickness\": 2,\n";
      jsonFile << "    \"red\": 200,\n";
      jsonFile << "    \"green\": 200,\n";
      jsonFile << "    \"blue\": 200,\n";
      jsonFile << "    \"line_type\": 8,\n";
      jsonFile << "    \"text_line\": \"" << myAscDate << "\",\n";
      jsonFile << "    \"x\": 5,\n";
      jsonFile << "    \"y\": 470\n";
      jsonFile << "  },\n  {\n";
      jsonFile << "    \"text_line\": \"" << myAscTime << "\",\n";
      jsonFile << "    \"x\": 490,\n";
      jsonFile << "    \"y\": 470\n";
      jsonFile << "  }\n]\n";
      jsonFile.close();
      if (ioctl(fd, VIDIOC_S_CTRL, &ctrl) == -1) {
        std::cerr << "Failed to notify the driver\n";
        break;
      }
      usleep(1000000);
    } while(true);
    close(fd);
    std::cout << "Retrying to open in 5 seconds...\n";
  }
}

PantherStyle
Posts: 1
Joined: Sat Mar 11, 2017 12:04 pm

Re: UV4L Text Overlay info needed

Sat Mar 11, 2017 12:15 pm

That's great stuff Jerry. Unfortunately I get a compile error when trying to compile your code:

Code: Select all

error: 'fd' does not name a type
auto fd = open(argv[1], O_RDWR | O_NONBLOCK);
I am new to c++ so my debugging skills are extremely lacking. I'm guessing you just need to define the data type for fd before assigning the value to it, but I don't know how to do that. Any ideas?

For anyone else attempting to do this, I just created a new file:

Code: Select all

sudo nano uv4l-time-overlay.cpp
then pasted in Jerry's code and saved.
I then attempted to compile Jerry's code to a file I can run using the c++ built-in compiler for Raspian Jessie (g++):

Code: Select all

g++ -o uv4l-time-overlay uv4l-time-overlay.cpp

User avatar
scramble45
Posts: 2
Joined: Mon May 22, 2017 5:06 pm
Location: Lenexa, KS

Re: UV4L Text Overlay info needed

Thu Jun 22, 2017 6:07 pm

add this:

Code: Select all

-std=c++11
and then it should compile.

TheWaterbug
Posts: 77
Joined: Fri Feb 05, 2016 12:35 am
Location: Los Angeles, CA, USA, Earth

Re: UV4L Text Overlay info needed

Tue Feb 06, 2018 10:37 pm

scramble45 wrote:
Thu Jun 22, 2017 6:07 pm
add this:

Code: Select all

-std=c++11
and then it should compile.
I tried this, and I'm still getting errors:

Code: Select all

[email protected]:~ $ g++ -o uv4l-time-overlay uv4l-time-overlay.cpp -std=c++11
/usr/bin/ld: final link failed: No space left on device
collect2: error: ld returned 1 exit status
[email protected]:~ $ sudo mount -o remount rw /
[email protected]:~ $ g++ -o uv4l-time-overlay uv4l-time-overlay.cpp -std=c++11
/usr/bin/ld: final link failed: No space left on device
collect2: error: ld returned 1 exit status
I'm attempting to do this on top of the PHP/Bootstrap Web Interface for the Pi Camera project, which has a write-protected fs at boot, which is why I tried sudo mount -o remount rw / before compiling, but it's still not working.

I'm not a programmer :cry: , so I'm limited to just following step-by-step directions, but any help would be welcome!

print3dxyz
Posts: 1
Joined: Thu Apr 04, 2019 4:28 pm

Re: UV4L Text Overlay info needed

Thu Apr 04, 2019 4:46 pm

Just used your code jerry and it works grand...
Now... anyone know how to get other info.. im using a compass and rotary encoder giving me distance and direction, which id like to put on screen... ive got both running with a python program. But should i try combine both into one python program to ADD to the json file, or do i need to use a revised uv4l-overlay-time-andotherstuff.cpp

RpiName
Posts: 713
Joined: Sat Jul 06, 2013 3:14 am

Re: UV4L Text Overlay info needed

Fri Apr 05, 2019 12:18 am

print3dxyz wrote:
Thu Apr 04, 2019 4:46 pm
Just used your code jerry and it works grand...
Now... anyone know how to get other info.. im using a compass and rotary encoder giving me distance and direction, which id like to put on screen... ive got both running with a python program. But should i try combine both into one python program to ADD to the json file, or do i need to use a revised uv4l-overlay-time-andotherstuff.cpp
you can stay with python to update your json file and call v4l2-ctl to signal uv4l to reparse it. see also:
https://www.linux-projects.org/uv4l/tut ... t-overlay/

Return to “Camera board”