January 13, 2020

Spectrometer using a Webcam

I purchased a Logitech C270 webcam on Amazon for $17. The thing is tiny! And it includes a microphone!

The packaging calls it a "C270 HD Webcam" and indicates that it yields 720p at 30 fps. They say that the lens gives a 60 degree field of view. The C525 is a better unit, also 720p but does autofocus with better video and a 69 degree view.

I plug it into a USB port on my linux system, and I get:
Jan 13 16:10:23 trona kernel: usb 2-1.7: new high-speed USB device number 6 using ehci-pci
Jan 13 16:10:23 trona kernel: usb 2-1.7: New USB device found, idVendor=046d, idProduct=0825, bcdDevice= 0.12
Jan 13 16:10:23 trona kernel: usb 2-1.7: New USB device strings: Mfr=0, Product=0, SerialNumber=2
Jan 13 16:10:23 trona kernel: usb 2-1.7: SerialNumber: 2BD574E0
Jan 13 16:10:24 trona kernel: usb 2-1.7: set resolution quirk: cval->res = 384
Jan 13 16:10:24 trona kernel: videodev: Linux video capture interface: v2.00
Jan 13 16:10:24 trona kernel: uvcvideo: Found UVC 1.00 device  (046d:0825)
Jan 13 16:10:24 trona kernel: input: UVC Camera (046d:0825) as /devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.7/2-1.7:1.0/input/input11
Jan 13 16:10:24 trona kernel: usbcore: registered new interface driver uvcvideo
Jan 13 16:10:24 trona kernel: USB Video Class driver (1.1.1)
Jan 13 16:10:24 trona rtkit-daemon[1119]: Successfully made thread 13034 of process 12287 (/usr/bin/pulseaudio) owned by '1004' RT at priority 5.
And I see:
[root@trona tom]# ls -l /dev/vid*
crw-rw----+ 1 root video 81, 0 Jan 13 16:10 /dev/video0
crw-rw----+ 1 root video 81, 1 Jan 13 16:10 /dev/video1

v4l2-ctl --list-devices
UVC Camera (046d:0825) (usb-0000:00:1d.0-1.7):
	/dev/video0
	/dev/video1

v4l2-ctl -d /dev/video0 --list-ctrls
                     brightness 0x00980900 (int)    : min=0 max=255 step=1 default=128 value=128
                       contrast 0x00980901 (int)    : min=0 max=255 step=1 default=32 value=32
                     saturation 0x00980902 (int)    : min=0 max=255 step=1 default=32 value=32
 white_balance_temperature_auto 0x0098090c (bool)   : default=1 value=1
                           gain 0x00980913 (int)    : min=0 max=255 step=1 default=64 value=64
           power_line_frequency 0x00980918 (menu)   : min=0 max=2 default=2 value=2
      white_balance_temperature 0x0098091a (int)    : min=0 max=10000 step=10 default=4000 value=4000 flags=inactive
                      sharpness 0x0098091b (int)    : min=0 max=255 step=1 default=24 value=24
         backlight_compensation 0x0098091c (int)    : min=0 max=1 step=1 default=0 value=0
                  exposure_auto 0x009a0901 (menu)   : min=0 max=3 default=3 value=3
              exposure_absolute 0x009a0902 (int)    : min=1 max=10000 step=1 default=166 value=166 flags=inactive
         exposure_auto_priority 0x009a0903 (bool)   : default=0 value=1
And I can use "xawtv -c /dev/video0" to get realtime video on my screen.

Typing "J" when xawtv is running captures a JPEG image, and I take a look at it as follows:

identify snap-unknown-20200113-161807-1.jpeg
snap-unknown-20200113-161807-1.jpeg JPEG 1280x960 1280x960+0+0 8-bit sRGB 60546B 0.000u 0:00.000
This camera gives me a 1280 by 960 jpeg image. The image looks pretty good.

A simple way to grab one image via the command line

I try using "streamer -f jpeg -o image.jpeg", but this gives me only a 320x240 image. On top of that, this is extremely lame and won't recognize a "jpg" extension.

There is something called "webcam" but it is poorly documented and seems to want to shove images to a web server via FTP. It starts, reports 320 by 240 resolution and then sits there doing who knows what.

Something else called "fswebcam" is available:

su
dnf install fswebcam
This has a decent man page and will give me what I want. Simply running it without options gives me a 352x288 pixel image:
fswebcam pig.jpg
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 384x288 to 352x288.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'pig.jpg'.
[root@trona tom]# identify pig.jpg
pig.jpg JPEG 352x288 352x288+0+0 8-bit sRGB 15671B 0.000u 0:00.000
However, I can specify full resolution and get what I want:
[root@trona tom]# fswebcam -r 1280x960 pig.jpg
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
No input was specified, using the first.
--- Capturing frame...
Captured frame in 0.00 seconds.
--- Processing captured image...
Writing JPEG image to 'pig.jpg'.
[root@trona tom]# identify pig.jpg
pig.jpg JPEG 1280x960 1280x960+0+0 8-bit sRGB 107927B 0.000u 0:00.000

So, I can use xawtv for real time video and fswebcam to capture individual images.


Have any comments? Questions? Drop me a line!

Tom's Electronics pages / tom@mmto.org