Implementing this USB video adapter to capture video from the small analog cameras in the hutch took a bit of doing.

First, the adapter must be plugged directly into the computer.  Using the USB hub beneath the monitor makes for an unreliable interface to the camera.

Second, the file /etc/udev/rules.d/99-usb-camera-capture.rules is needed to set permissions on /dev/video0 correctly when the adapter is plugged in.

ACTION!="add|change", GOTO="webcam_capture_end"
SUBSYSTEM=="usb", ATTRS{idVendor}=="534d", ATTRS{idProduct}=="0021", MODE="0666"
KERNEL=="video*", ATTRS{idVendor}=="534d", ATTRS{idProduct}=="0021", GROUP="video", MODE="0666"
KERNEL=="video*", ATTRS{idVendor}=="534d", ATTRS{idProduct}=="0021", ATTRS{avoid_reset_quirk}=1
KERNEL=="video*", ATTRS{idVendor}=="534d", ATTRS{idProduct}=="0021", ATTRS{quirks}=0x100
LABEL="webcam_capture_end"

This recognizes the vendor and product IDs of the specific adapter that I bought.  When inserted, it sets the device to RW for all users and sets a couple of possibly relevant attributes.  (This udev rules file was adapted from the rules file that comes with the easycap dc60 package – info and links here).

Next a small script was written as a wrapper around fswebcam to grab frames from the camera and draw a cross hair on the image.  (This requires installing the fswebcam and imagemagick debian packages.)  The script is basically a wrapper around a call to fswebcam like so:

fswebcam -d /dev/video0 -r 640x480 -S 30 -F 5 foo.jpg

along with some image processing using python's wand package.

Required debian packages: fswebcam, python-wand

This whole setup is filled with quirk.  There is a delay accessing the video capture.  The -S switch builds in a 1 second delay, giving the capture device enough time to begin displaying the image.  The -F switch tells the script how many frames to accumulate for good signal.  5 is probably overkill.

In any case, it is now possible to grab screen shots of the currently displayed analog video while collecting data.

All of this is implemented in 47-snapshot.py for use in BlueSky. The heart of the implementation is a system call to fswebcam.

To do: For some reason, capturing from the B&W Sony camera does not work.


bravel@xf06bm-ws1 [~] > ./anacam.py -h
usage: anacam.py [-h] [-d DIRECTORY] [-v DEVICE] [-s SKIP] [-f FRAMES]
                 [-b BRIGHTNESS] [-x X] [-y Y] [-l LINECOLOR] [-n] [-r] [-q]

Capture a screenshot from the currently displayed analog camera

optional arguments:
  -h, --help            show this help message and exit
  -d DIRECTORY, --directory DIRECTORY
                        output image directory (/home/bravel)
  -v DEVICE, --device DEVICE
                        input device (/dev/video0)
  -s SKIP, --skip SKIP  number of frames to skip (30)
  -f FRAMES, --frames FRAMES
                        number of frames to accumulate (5)
  -b BRIGHTNESS, --brightness BRIGHTNESS
                        percent brightness (20)
  -x X                  x-position of cross hairs (320)
  -y Y                  y-position of cross hairs (240)
  -l LINECOLOR, --linecolor LINECOLOR
                        cross hair line color (white)
  -n, --nocrosshair     flag suppressing cross hair (default is to draw cross
                        hair at x,y)
  -r, --reset           flag for attempting to reset device (default is not to
                        try)
  -q, --quiet           flag suppressing screen comments (default is false)