Technical Blog

Create a video with the AR.Drone

The AR.Drone is an efficient source of images: it can fly, being remotely controlled, etc. We will see in this article how to create avi files from its cameras. We will use OpenCV to create it, so you’ll probably need first to take a look at Use OpenCV with the AR.Drone SDK.

OpenCV code

We will use the CvVideoWriter structure to build our avi file.
Firstly, we need a function to initialize it.

CvVideoWriter *init_video_writer(char *fname)
{
  int isColor = 1;
  int fps     = 30;
  int frameW = 320;
  int frameH = 240;
  return cvCreateVideoWriter(fname, // with avi extension
                             CV_FOURCC('D', 'I', 'V', 'X'), //MPEG4
                             fps,
                             cvSize(frameW,frameH),
                             isColor);
}

This feature is handled in my project with a button. I added to functions that are call by the button’s callback :

static CvVideoWriter *video_writer = 0;
void init_video(void)
{
  video_writer = init_video_writer();
}

void stop_video(void)
{
  // Necessary to have a valid avi file
  cvReleaseVideoWriter(&video_writer);
}

I added a function to add a frame to the video:

inline void add_frame(IplImage *img)
{
  if (video_writer)
    cvWriteFrame(video_writer, img);
}

Finally, we need to call the add_frame function every time we receive a new frame from the drone. I added it in the output_gtk_stage_transform function in Video/video_stage.c.
Underneath the code creating the OpenCV image, I added

if (/* video saving is enabled by the user */)
    add_frame(img);

Handling different frame rates

The AR.Drone has two cameras, using two different frame rates:

  • The frontal camera has 15 FPS
  • The vertical camera has 60 FPS

In the previous code, video was created with 30FPS. Therefore, one camera will look to slow, and the other one to fast. Therefore, we can update the function this way:

CvVideoWriter *init_video_writer(char *fname, int fps)
{
  int isColor = 1;
  int fps     = fps;
  int frameW = 320;
  int frameH = 240;
  return cvCreateVideoWriter(fname, // with avi extension
                             CV_FOURCC('D', 'I', 'V', 'X'), // //MPEG4
                             fps,
                             cvSize(frameW,frameH),
                             isColor);
}

Then, we can use two ways to call it:

video_writer = init_video_writer("out_horizontal.avi", 15);

or

video_writer = init_video_writer("out_vertical.avi", 60);

Refer to this page for more information about possible codecs.

  • Mcives

    HI

    Should all of these comments be placed into Video/video_stage.c?

    Thanks