grabv4l.cc

  1 const char *help = "\
  2 progname: grabv4l.cc\n\
  3 code2html: This program grabs images using the V4L interface.\n\
  4 version: Torch3 vision2.0, 2003-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "VideoGrabberV4L.h"
  8 #include "ImageRgb.h"
  9 #include "MTimer.h"
 10 #include "CmdLine.h"
 11 
 12 using namespace Torch;
 13 
 14 int main(int argc, char **argv)
 15 {
 16 	int device;
 17 	int width;
 18 	int height;
 19 	int n;
 20 	char *input_format;
 21 	bool verbose;
 22 	bool saveppm;
 23 
 24 	// Construct the command line
 25   	CmdLine cmd;
 26 	cmd.setBOption("write log", false);
 27 	
 28   	// Put the help line at the beginning
 29   	cmd.info(help);
 30 
 31   	cmd.addText("\nArguments:");
 32   	cmd.addICmdArg("device", &device, "device");
 33   	cmd.addText("\nOptions:");
 34   	cmd.addICmdOption("-width", &width, 384, "width");
 35   	cmd.addICmdOption("-height", &height, 288, "height");
 36   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 37   	cmd.addBCmdOption("-ppm", &saveppm, false, "save frames as ppm");
 38   	cmd.addSCmdOption("-ifmt", &input_format, "YUV420P", "YUV420 planar");
 39   	cmd.addICmdOption("-n", &n, 10, "number of frames to gran");
 40 
 41 	cmd.read(argc, argv);
 42 
 43 	ImageRgb *image = NULL; 
 44 	VideoGrabber *video = NULL;
 45 	VirtualPixmap *currentFrame = NULL;
 46 
 47 	//
 48 	image = new ImageRgb(width, height);
 49 	
 50 	//
 51 	char devname[40];
 52 	char imagename[40];
 53 	sprintf(devname, "/dev/video%d", device);
 54 
 55 	video = new VideoGrabberV4L(devname, width, height, "RGB24", input_format, -1, 0, 30, verbose);
 56 
 57 	if(video->isOk())
 58 	{
 59 		if(video->initVideoSource())
 60 		{
 61 			print("Video source initialized.\n");
 62 
 63 			if(video->openVideoSource())
 64 			{
 65 				print("Video source opened.\n");
 66 
 67 				MTimer timer;
 68 
 69 				timer.reset();
 70 
 71 				for(int i = 0 ; i < n ; i++)
 72 				{
 73 					if(verbose) print(":");
 74 					currentFrame = video->getframe();
 75 					if(verbose) print(".");
 76 
 77 					if(saveppm)
 78 					{
 79 						image->copyFrom(width, height, (unsigned char *) currentFrame->getpixmap(), "rgb");
 80 
 81 						sprintf(imagename, "frame%02d.ppm", i);
 82 						image->save(imagename);
 83 					}
 84 
 85 					video->releaseframe(currentFrame);
 86 				}
 87 
 88 				timer.stop();
 89 
 90 				if(verbose) print("\n");
 91 
 92 				print(" %d frames captured in %02dh %02dm %02ds %02dms %02dus %02dns\n", n, timer.hours, timer.minutes, timer.seconds, timer.mseconds, timer.useconds, timer.nseconds);
 93 			}
 94 				
 95 			video->closeVideoSource();
 96 			print("Video source closed.\n");
 97 		}
 98 	}
 99 	   
100 	delete video;
101 	delete image;
102 
103 	return 0;
104 }