Showing posts with label Lucid Puppy. Show all posts
Showing posts with label Lucid Puppy. Show all posts

Sunday, 12 January 2014

Not-so-spontaneous installing of OpenCV

Spontaneous Projects is a chronicle of all of my projects, a reference for me, and a way to prevent others from making the same mistakes. 

I think I've finally succeeded, it still needs some testing with other code, because I don't know how the integrated test programs function, but I think I managed it.

For the first section of this little saga check the older post here.

For the most part I followed these directions from murga-linux

Preparation


First, download and install Lucid Puppy (5.2.8 works)
Then, you need to download the devx file.

Then we need a few PET files, some of the ones in the linked post cause issues, with crashing xwin, or crashing the console somehow. On top of that they don't seem to be needed, it's possible that they are now rolled into the distro or the devx package.

DO NOT get new gtk+ files, they cause the xwin problem, and are not needed.

Download these from http://distro.ibiblio.org/puppylinux/pet_packages-4/
  • ffmpeg-20080731-i486.pet
  • ffmpeg_DEV-20080731-i486.pet
  • libjpeg-6b.pet
  • libpng-1.2.22-patched1-pup4.pet
  • libtiff-3.7.4.pet
  • fontconfig-2.6.0-prescott.pet
  • fontconfig_DEV-2.6.0-prescott.pet
  • x264-20080731-2245-i486.pet 
as well as

Now we add the devx sfs using SFS on-the-fly (Menu>Setup>Setup Puppy)

Back up your save file here, with wireless, a browser, the devx package and everything else needed for basic functionality.

Then install the PET's, just click run, or save and run them later. 

I reboot every now and then, because the xwin issues didn't manifest till the reboot, but given that I 
tracked them to xlib it shouldn't be an issue.

Installing


make the directory where you want OpenCV (</...OpenCV>)

#cd </...OpenCV>
#svn co http://opencvlibrary.svn.sourceforge.net/svnroot/opencvlibrary/trunk .

#cd </...OpenCV>trunk/opencv
#mkdir release
#cd release

#cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON ..

#make
#make install

#ldconfig -v
#export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
#cd </...OpenCV>/trunk/opencv
#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

These tests didn't run from terminal, I think they are actually in a different directory. You can find them manually though

#cd </...OpenCV>/opencv/trunk/release/bin
#./cxcoretest
#./cvtest -d </...OpenCV>/trunk/opencv/tests/cv/testdata/cv



I had no failures on the cxcore test, But the cv one had some, likely due to lack of inputs - it also had a segementation fault, so hopefully that isn't problematic.

Update: 13/1/14
I've compiled the samples, so it looks like it's all working as expected.
To run the samples, navigate to

</...OpenCV>/trunk/opencv/sample/c

and open a terminal. For LuPu you need to specify that you wish to use shell, so enter

sh build_all.sh

./edge
./motempl (if you have a webcam)
./ <any other program>
Use Esc to exit any of the samples

motempl in action, from what I can gather it identifies regions that have changed between frames

A little more movement in this one

And lots of movement as I move my hand and wave it about

Friday, 10 January 2014

Installing OpenCV (or trying to)

Spontaneous Projects is a chronicle of all of my projects, a reference for me, and a way to prevent others from making the same mistakes. 

I've spent the last few days trying to install openCV, an open source Computer Vision library.

It's not designed for Lucid Puppy, but there are posts about that indicate that it is possible. I've tried following the instructions for ubuntu on OpenCV's site, and got to 68% completion, but I was unable to find exact matches of some of the packages, and I think I may have forgotten to install dependecies on another - I thought it was an optional one, but maybe not.

I then tried the process shown at Murga-linux
As I was installing .pet files the computer siezed up a little, and seemed to stop installing them, on a reboot it appears that I have corrupted xwin. This isn't exactly surprising, given that I didn't remove the old packages, or make any effort to clean up.

So it looks like I'll be reinstalling LuPu and trying again using the Murga approach, but given that I was already planning on a fresh install soon it's not really an issue.

I was also able to copy my working directory from the SSD to a USB from the command line, so it appears that it's just xwin that is corrupt - I could probably try to fix it, but a reinstall will be easier.

Monday, 6 January 2014

Image Capture on linux

Spontaneous Projects is a chronicle of all of my projects, a reference for me, and a way to prevent others from making the same mistakes. 

For the first part of migrating to using the netbook as a computer vision device, I need to get an image from it.
I used a program called uvccapture as a basis, and modified it heavily, so that it runs in YUY mode all of the time, and has a lot less options, as I plan to use a modified version of this to capture for actual processing later.

The following was developed for a EeePC 701sd, running lucid puppy and compiled using g++ and a make file, the name is still the original, as I haven't fully got my head around how the makefiles work yet, so I kept it simple.
Lucid Puppy, (right) an older image showing both lack of centering and bad colours
(left) a recent pic, taken at the full res of the camera, in worse lighting, but with 1 second for the camera to adjust



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <jpeglib.h>
#include <time.h>
#include <linux/videodev.h>

#include "v4l2uvc.h"

static const char version[] = VERSION;
int run = 1;

void
sigcatch (int sig)
{
  fprintf (stderr, "Exiting...\n");
  run = 0;
}

void
usage (void)
{
  fprintf (stderr, "uvccapture version %s\n", version);
  fprintf (stderr, "Usage is: uvccapture [options]\n");
  fprintf (stderr, "Options:\n");
  //fprintf (stderr, "-v\t\tVerbose\n");
  //fprintf (stderr, "-o<filename>\tOutput filename(default: snap.jpg)\n");
  //fprintf (stderr, "-d<device>\tV4L2 Device(default: /dev/video0)\n");
  //fprintf (stderr,
  //"-x<width>\tImage Width(must be supported by device)(>960 activates YUYV capture)\n");
  //fprintf (stderr,
  //"-y<height>\tImage Height(must be supported by device)(>720 activates YUYV capture)\n");
  //fprintf (stderr,
  //"-c<command>\tCommand to run after each image capture(executed as <command> <output_filename>)\n");
  //fprintf (stderr,
  //"-t<integer>\tTake continuous shots with <integer> seconds between them (0 for single shot)\n");
  //fprintf (stderr,
  //"-q<percentage>\tJPEG Quality Compression Level (activates YUYV capture)\n");
  //fprintf (stderr, "-r\t\tUse read instead of mmap for image capture\n");
  //fprintf (stderr,
  //"-w\t\tWait for capture command to finish before starting next capture\n");
  fprintf (stderr, "-m\t\tToggles capture mode to YUYV capture\n");
  //fprintf (stderr, "Camera Settings:\n");
  //fprintf (stderr, "-B<integer>\tBrightness\n");
  //fprintf (stderr, "-C<integer>\tContrast\n");
  //fprintf (stderr, "-S<integer>\tSaturation\n");
  //fprintf (stderr, "-G<integer>\tGain\n");
  exit (8);
}

int
spawn (char *argv[], int wait, int verbose)
{
  pid_t pid;
  int rv;

  switch (pid = fork ()) {
  case -1:
    return -1;
  case 0:
    // CHILD
    execvp (argv[0], argv);
    fprintf (stderr, "Error executing command '%s'\n", argv[0]);
    exit (1);
  default:
    // PARENT
    if (wait == 1)  
      waitpid (pid, &rv, 0);
 
    else {
      // Clean zombies
      waitpid (-1, &rv, WNOHANG);
      rv = 0;
    }
    break;
  }

  return rv;
}

int
compress_yuyv_to_jpeg (struct vdIn *vd, FILE * file, int quality)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  JSAMPROW row_pointer[1];
  unsigned char *line_buffer, *yuyv;
  int z;
  sleep(1);
  line_buffer = calloc (vd->width * 3, 1);
  yuyv = vd->framebuffer;

  cinfo.err = jpeg_std_error (&jerr);
  jpeg_create_compress (&cinfo);
  jpeg_stdio_dest (&cinfo, file);

  cinfo.image_width = vd->width;
  cinfo.image_height = vd->height;
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;

  jpeg_set_defaults (&cinfo);
  jpeg_set_quality (&cinfo, quality, TRUE);

  jpeg_start_compress (&cinfo, TRUE);

  z = 0;
  while (cinfo.next_scanline < cinfo.image_height) {
    int x;
    unsigned char *ptr = line_buffer;

    for (x = 0; x < vd->width; x++) {
      int r, g, b;
      int y, u, v;

      if (!z)
y = yuyv[0] << 8;
      else
y = yuyv[2] << 8;
      u = yuyv[1] - 128;
      v = yuyv[3] - 128;

      r = (y + (359 * v)) >> 8;
      g = (y - (88 * u) - (183 * v)) >> 8;
      b = (y + (454 * u)) >> 8;

      *(ptr++) = (r > 255) ? 255 : ((r < 0) ? 0 : r);
      *(ptr++) = (g > 255) ? 255 : ((g < 0) ? 0 : g);
      *(ptr++) = (b > 255) ? 255 : ((b < 0) ? 0 : b);

      if (z++) {
z = 0;
yuyv += 4;
      }
    }

    row_pointer[0] = line_buffer;
    jpeg_write_scanlines (&cinfo, row_pointer, 1);
  }

  jpeg_finish_compress (&cinfo);
  jpeg_destroy_compress (&cinfo);

  free (line_buffer);

  return (0);
}

int
main (int argc, char *argv[])
{
  char *videodevice = "/dev/video0";
  char *outputfile = "new.jpg";

  //int format = V4L2_PIX_FMT_MJPEG; // OR
  int format = V4L2_PIX_FMT_YUYV;

  int grabmethod = 1;
  int width = 640;
  int height = 480;
  int brightness = 0, contrast = 0, saturation = 0;
  int delay = 0; // increased, may sort out the wrap issues, results in other issues, but fixes colours
  int quality = 95;
  time_t ref_time;
  struct vdIn *videoIn;
  FILE *file;

  (void) signal (SIGINT, sigcatch);
  (void) signal (SIGQUIT, sigcatch);
  (void) signal (SIGKILL, sigcatch);
  (void) signal (SIGTERM, sigcatch);
  (void) signal (SIGABRT, sigcatch);
  (void) signal (SIGTRAP, sigcatch);



  //Options Parsing (FIXME)
  while ((argc > 1) && (argv[1][0] == '-')) {
    switch (argv[1][1]) {

    case 'm':
       format = V4L2_PIX_FMT_YUYV;
      break;

    default: // this an unknown was entered case, nb -h is that
      fprintf (stderr, "Unknown option %s \n", argv[1]);
      format = V4L2_PIX_FMT_YUYV; // added here
      usage ();
    }
    ++argv;
    --argc;
  }


  videoIn = (struct vdIn *) calloc (1, sizeof (struct vdIn));
  if (init_videoIn
      (videoIn, (char *) videodevice, width, height, format, grabmethod) < 0)
    exit (1);

  //Reset all camera controls

  v4l2ResetControl (videoIn, V4L2_CID_BRIGHTNESS);
  v4l2ResetControl (videoIn, V4L2_CID_CONTRAST);
  v4l2ResetControl (videoIn, V4L2_CID_SATURATION);
  //v4l2ResetControl (videoIn, V4L2_CID_GAIN);

  //Setup Camera Parameters
  if (brightness != 0)
    v4l2SetControl (videoIn, V4L2_CID_BRIGHTNESS, brightness);

  if (contrast != 0)
      v4l2SetControl (videoIn, V4L2_CID_CONTRAST, contrast);

  if (saturation != 0)
     v4l2SetControl (videoIn, V4L2_CID_SATURATION, saturation);
 
  //if (gain != 0)    // It appears that gain does not exist, in this format, on this camera... who knows?
    //v4l2SetControl (videoIn, V4L2_CID_GAIN, gain);
 
 
  ref_time = time (NULL);

  while (run) {
 
    if (uvcGrab (videoIn) < 0) {
      fprintf (stderr, "Error grabbing\n");
      close_v4l2 (videoIn);
      free (videoIn);
      exit (1);
    }

    if ((difftime (time (NULL), ref_time) > delay) || delay == 0) {

      file = fopen (outputfile, "wb");
      if (file != NULL) {
switch (videoIn->formatIn) {
case V4L2_PIX_FMT_YUYV:
 compress_yuyv_to_jpeg (videoIn, file, quality);
 break;
default:
 fwrite (videoIn->tmpbuffer, videoIn->buf.bytesused + DHT_SIZE, 1,
 file);
 break;
}
fclose (file);
videoIn->getPict = 0;
      }
   
      ref_time = time (NULL);
    }
    if (delay == 0)
      break;
  }
  close_v4l2 (videoIn);
  free (videoIn);

  return 0;
}


Some Notes:

  • uvccapture uses some strange settings when initialising the camera, if there is not a delay to allow the camera to choose it's own settings it can be out of alignment, invert the colours, or be over/under saturated.
  • The gain function doesn't seem to exist for this setup, and throws an error 22 bug, once I commented out those lines it fixed itself. I'm not sure if it's hardware, software, or something else
  • There is likely still an amount of this that could be cut down, but until I know how I need the images to use them it will stay as it is. 


Early images, note the wrapping issue, and red appears blue

Recent image, with these issues corrected

Tuesday, 31 December 2013

New use for old hardwarEee

While playing with webcams and computer vision is fun in it's own right, I've been thinking about the uses of my these programs, and came to the conclusion that they need to be run on machines that won't be a huge loss if they go missing or get stolen, I have an old Eee pc 701sd lying around that fits the bill nicely. An alternative would be a Raspberry Pi system, with an external webcam, but my budget doesn't extend to that at the moment.

The Xandros that comes on the Eee it is not well suited to development, and is well out of date these days, so I set about finding a new compact distro for it, and settled on Lucid Puppy, a very light weight, compact, ubuntu compatible build that can be run from usb.

I started using the universal installer to make a usb to trial it on - an exercise of it's own, you need to load bios each time, look at the hard drive order, save, then press esc, and choose the usb...
LuPu ran well, quite fluid, despite the dreadful specs of that machine with a external mouse and keyboard, and running a 17" monitor powered by an able-hd (www.able-hd.com) adaptor.

Because of the effort required to boot to USB I chose to install LuPu onto the sdd, removing the previous installs, and have finally got to the point where i can write, and compile c programs, so there is more to come.