Wednesday, June 15, 2016

Streaming a USB Microscope with Raspberry Pi 3

Check on Amazon
When the rainy season ends, it's the long-awaited plankton season.
So I bought a USB microscope.

It's a pretty sketchy Chinese-made unit, and the bundled software doesn't run properly on Windows 10.
Yet for some reason, it works brilliantly on Raspberry Pi.

The Amazon product description is hilariously inaccurate:
  • Listed as having multiple magnification levels, but there are only 2 focal distances that actually work in focus.
    The Windows software apparently calls digital zoom "magnification change."
  • Maximum resolution listed as 1600×1200, but it's fixed at 640×480.
    The Windows software digitally stretches the image up to 1600px, which doesn't make it clearer.
  • Claims continuous zoom, but zooming in puts everything out of focus.
  • Has SNAP and ZOOM buttons that do nothing.


That said, getting a USB microscope that works with Raspberry Pi at this price isn't bad at all.

In this post I'll try two different uses: as a portable digital microscope, and as a live-streaming microscope server.

Turning it into a mobile microscope




First, display the microscope feed on the Raspberry Pi's LCD monitor.
Attach a power bank and you can take it outside.

(Of course, if you have an OTG-capable Android phone, you could just plug the microscope directly into that — but here we are.)

For how to use an LCD with Raspberry Pi, see the earlier post: Turning Raspberry Pi 3 into an all-in-one PC.

To display the microscope feed on the LCD, install luvcview:
pi@raspberrypi:~ $ sudo apt-get install luvcview


Then run it. That's it:
pi@raspberrypi:~ $ luvcview


This should work the same with regular webcams.
If you want to record footage, install guvcview instead.

I went ahead and filmed some water fleas (Daphnia):


Live-streaming microscope over the network


Displaying video on the Raspberry Pi while simultaneously capturing gets pretty choppy.
Instead, let's turn the Raspberry Pi into a streaming server and view the feed on an iPad.
This is straightforward with mjpg-streamer.

First, install the required packages and build from source:
pi@raspberrypi:~ $ sudo apt-get install subversion libjpeg-dev imagemagick
pi@raspberrypi:~ $ svn co https://svn.code.sf.net/p/mjpg-streamer/code/mjpg-streamer mjpg-streamer
pi@raspberrypi:~ $ cd mjpg-streamer
pi@raspberrypi:~ $ make
pi@raspberrypi:~ $ make install


Set LD_LIBRARY_PATH and start streaming:
pi@raspberrypi:~ $ sudo -s
root@raspberrypi:~# export LD_LIBRARY_PATH=/usr/local/lib
root@raspberrypi:~# mjpg_streamer -i "input_uvc.so -f 10 -r 640x480 -d /dev/video0 -y -n" -o "output_http.so -w /var/www/mjpg -p 8080"


Open Safari on iPad and go to http://{IP_ADDRESS}:8080/?action=stream.
It looks like this — a wall-mounted frame for watching water fleas. Oddly classy.


The zoom dial can magnify several times, but at high zoom the critters rarely stay in frame — this magnification level is the sweet spot.

To start the stream automatically on boot, create an init script like this:
#!/bin/sh
### BEGIN INIT INFO
# Provides:          mjpg
# Required-Start:    $local_fs $remote_fs $network $syslog $named
# Required-Stop:     $local_fs $remote_fs $network $syslog $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO


export LD_LIBRARY_PATH=/usr/local/lib/:${LD_LIBRARY_PATH}

case "$1" in
start)
/usr/local/bin/mjpg_streamer -i "input_uvc.so -f 10 -r 640x480 -d /dev/video0 -y -n" -o "output_http.so -w /var/www/mjpg -p 8080" &
;;
stop)
/bin/kill -9 `/bin/pidof mjpg_streamer`
;;
esac

exit 0


root@raspberrypi:~# vi /etc/init.d/mjpg
root@raspberrypi:~# chmod 755 /etc/init.d/mjpg
root@raspberrypi:~# update-rc.d mjpg defaults


You can also install motion to automatically record only when a water flea appears in frame.
Daphnia... you tiny adorable creatures...

No comments:

Post a Comment