Finding webcams

List all devices on windows using ffmpeg

ffmpeg -list_devices true -f dshow -i dummy

and on Linux

v4l2-ctl –list-devices

To get the device capabilites

ffmpeg -f dshow -list_options true -i video="Mobius"

where “Mobius” is the name of the camera.

On the Mac use

ffmpeg -f avfoundation -list_devices true -i ""

vim cheat-sheet

A minimal cheat-sheet to get by in vim (vi /gvim)

There’s no doubt that vim/vi or gvim is an incredibly powerful editor. it’s also very lightweight and fast, making it an ideal editor on the Raspberry Pi. It does however have a learning curve. This in a minimal cheat-sheet of commands. The ones in bold are I think the ones you need to learn to be able to use it at a basic level. Also try running vimtutor from the command line for a tutorial.

Esc key – Normal mode

Inserting text

a -append

i – insert

o – open (inserts line below current line, O open above current line)

r – replace (replaces the character under the cursor with the next one entered,

R – replace mode (overwrite text until Esc pressed)

ce – change (deletes to end of word and then switches to insert mode) c$ deletes the rest of the line

Deleting text

x -delete character

dw – delete word      (d2w delete two words)

de – delete from cursor to end of word

d$ – delete from cursor to end of line

dd – delete the line  (2dd delete two lines)

Undo

u – undo

U – undo all changes on a line

Put, Past and Cut

p – put the contents of the buffer (paste) (this is the last thing deleted or yanked)

v – visual selections mode (highlights text for eg deletions or write to file with :w FILENAME)

y – yank (copies highlighted text), yw – yanks word

Moving about

gg – go to start of the file

GG – go to end of the file

504G – goto line 504

/  – search, n find next, N find previous

% – find matching bracket (,[,{

0 – move to start of the line, ^ move to first non space character.

$ – move to end of the line

Status

^G – file and position status

! – execute external command e.g. !ls

Writing and Reading

:w – save the file

:w FILENAME – save as FILENAME

:r FILENAME – inserts content of file here, you can also insert output of commands eg :r !ls

:q! -quit discarding changes

: x – quit saving changes (if there are any as opposed to :wq which always saves)

Python for Matlab users

Python has a number of benefits over Matlab, and a number of research groups are making the switch. I’m not going to do a complete tutorial here on how to programme in python for Matlab users since there are plenty on the web. Instead I wanted to make a few note on the practical aspects such as IDEs and how to use python.

I’m running python on Mint linux but most of this should be applicable to Windows and Macs.

  • There are several types of python implementation such as CPython, Anaconda, Python(x,y). CPython is the original and probably the best to start with since it probably is the one that comes with your Linux distro. Normal Python compiles into bytecode, other implementations can compile into C (Cython, not to be confused with CPython), .NET (IronPython) among others. Unless you have good reason stick with CPython.
  • There are two slightly incompatible version of Python: 2.x and 3.x.  Which one to use, depends on who you ask. I’ve picked 3 since it’s the future. You’ll probably need to know the differences at some point since there’s a lot 2.x around.
  • Python vs iPython. Python scripts can be run from the command line, eg. python script.py  or you can use the interactive shell by simply running python. ipython is a souped up version of the interactive shell and has additional functionality. It it more like the Matlab command prompt and is the one to use when writing scripts.
  • IDEs. There are a lot of IDEs for python. I don’t think any of them are as good as the Matlab IDE just yet. Spyder is probably the closest.
  • Instead of toolboxes, Python has packages. These can be installed a number of ways. If you are running Linux your distro will have most of the more common ones that can be installed via apt-get or its equivalent. These packages will be installed system wide and probably be slightly out of date but possibly safer. Another method is to use the Python package index PyPi and uses the program pip to install them. pip will have a larger, more up to date database of packages. It also has the ability to restrict the download to your user space only or use a completely self-contained environment virtualenv to run your scripts in. Great if you need incompatible versions for different projects or don’t want to screw up other projects. I would recommend looking into this.
  • When installing using apt-get, you’ll need suffix package names with a 3 to get the python 3 version, otherwise it will install version 2. It’s the same with spyder, install spyder3. Some packages work with 2 and 3 so there’s only one version to add to the confusion.

Plotting and images

  • A graphically plotting library matplotlib is provided and is similar to Matlab
  • In ipython plots maybe inline (i.e displayed in the console). If you don’t want this run
    %matplotlib qt and  %matplotlib inline to return to inline.

Matrices and computer vision

  • Matlab’s raison d’être is matrix manipulation and but by itself python’s support is limited. Conveniently there is a package that however supports this called numpy which is part of scipy and that provides a huge scientific library. Sympy can be used for symbolic maths manipulation. It’s the numpy library that makes python such useful replacement for Matlab.
  • As far as replacing the image processing and computer vision toolboxs, there is a range of options. Opencv has a python wrapper, which conveniently uses numpy arrays to hold images. You’ll need opencv 3 to get python 3 support. Scikit-image has a huge collection of routines. They both use numpy arrays so you can use both at once although you will have to covert the data types. Another option is Pillow which is forked from the defunct PIL library.

Installing MySQL with macports

This is my notes for installing MySQL on mac. It’s mainly taken from https://trac.macports.org/wiki/howto/MySQL

sudo port install mysql56-server

which installs version 5.6, but you could check this was the latest version by search the output of

port search mysql

You need to enable the port by adding it to the system path, the easiest way is by

sudo port select mysql mysql56

Then set up the database

sudo -u _mysql mysql_install_db 
sudo chown -R _mysql:_mysql /opt/local/var/db/mysql56/ 
sudo chown -R _mysql:_mysql /opt/local/var/run/mysql56/ 
sudo chown -R _mysql:_mysql /opt/local/var/log/mysql56/ 

Then start the database

sudo port load mysql56-server

We need to set a root password

/opt/local/lib/mysql56/bin/mysqladmin -u root -p password 

You will be prompted for the old password, which currently blank so just press enter, then add your new password.

You can then add some basic security to the database by running

/opt/local/bin/mysql_secure_installation

If you need the server to connect to the network (including it seems the loopback localhost – although I’m sure there is away round this), you need to edit the configuration file in /opt/local/etc/mysql56/my.conf. The file simply calls the default config file, macports-default.cnf,  which stops only has skip-network in it. Don’t edit the default since an update may overwrite it, simply edit comment out the include line with a ! symbol.

To enable a C++ interface to MySQL install

sudo port install mysql5-connector-cpp

note that this also a port mysql-connector-cpp without that 5, which is broken! A half hour of my life I’ll never get back. Unfortunately this port is out of date and links against MySQL 5.1. It can still be used with 5.6 but it does mean macports will also install 5.1 on your system.

To edit the config file and your databases there is a GUI tool MySQL Workbench

Axis IP camera

I use axis IP camera a lot for capturing images and video. The image quality is great and they are highly customisable. I use a P1344 camera and it supports still images, MJPEG and H264. The still image are fine for capturing a one off, but too slow for video work. For this I need either MJPEG or H264. Both have there pros and cons.

MJPEG is existentially a sequence of JPEG images. It’s easy to use and the quality is good, depending on the compression settings. The downside is the bitrate over the network is larger than H264.

H264 is a lossy video compression format that has become ubiquitous on the internet these days for compressed video and blue-ray videos.

The camera can be controlled using the url. To get a H264 stream (using VLC in this case, but ffplay works perfectly well), at the command prompt type

vlc rtsp://192.168.0.103:554/axis-media/media.amp

changing the IP address to that of your camera. You can also add in the camera’s username and password using:

vlc "rtsp://192.168.0.103:554/axis-media/media.amp?user=XXX&password=XXXX"

The image resolution can be changed with

vlc rtsp://192.168.0.103:554/axis-media/media.amp?resolution=640x480

The resolution is camera dependent. There are a bunch of different settings, such as bit rate, compression, you can apply see the AXIS VAPIX documentation for the whole list. An easy way to do this is by using the cameras settings page to create a Stream Profile. There are a number built in and you can select  them like so

 vlc rtsp://192.168.0.103:554/axis-media/media.amp?streamprofile=Quality

Still images can be captured, by using

http://192.168.0.103/axis-cgi/jpg/image.cgi?resolution=320x240&compression=25

here I’ve selected the resolution and compression factor. You can grab the image by placing the above url into a browser.

Image Quality

Compression artifact noise
Compression artifact noise

Looking at the above shows subregions of example images captured at full resolution using JPG. We can see that there is significant compression artifacts in the image even at low compression ratios. Setting the compression ratio less than 40 appears to have little effect on image quality.

H264 streams appear to be similarly affected. The bitmap image shows some improvement however the data rate to transmit this is considerably larger.

Note that the RMS errors are calculated from the JPEG image with a compression factor of 0.

Bitmap image
Bitmap image

 

Benchmarking

 ffplay "http://192.168.0.103/axis-cgi/mjpg/video.cgi?resolution=640x480&fps=15"

uses 60% of one core of my Odroid XU4 and 17% on my 2.7 GHz iMac

ffplay "rtsp://192.168.0.103:554/axis-media/media.amp?resolution=640x480&fps=15"

uses 88% on the Odroid and 14% on the iMac.

 

Mac Ports Cheat Sheet

It’s worth reading the mac ports guide here, but here is my cut down cheat sheet of commands I actually use.

To install a port, e.g opencv

sudo port install opencv

To update ports definition list

sudo port selfupdate

Once, that’s finished you can update the outdated ports (this might take sometime depending on the number of updates)

sudo port upgrade outdated

or you can get a list of the outdated ports

port outdated

and upgraded them manually

sudo port upgrade opencv

This should also upgrade dependencies.

To get a list of installed ports

port installed

To search for a port, eg, PHP you could try

port search php

This will give you hundreds of port that mention PHP in their description. You can filter this, for example

port search --name --glob 'php*'

will return only the ports that start with php.

port info opencv

returns information about the opencv port (the lastest version, so watch out if your ports are out of date). A list of variants can be obtained with

port variants opencv

A port variant can be installed using the syntax:

sudo port install opencv +java

This installs the variant of opencv with java bindings. To see which variant is installed type:

port installed opencv

this lists the variant installed, plus the inactive versions of the library.

If a port fails to build it might be worth cleaning up first by running

sudo port clean opencv

and trying again.

To find out what and where port has installed files run

port contents opencv

To remove a port you use either of

sudo port uninstall opencv
sudo port uninstall --follow-dependents opencv

The second option removes the installed dependents as long as no other port is using them. If you don’t use the latter there will be a number of ports left on your system that you didn’t manually install. These are known as leaves and you can list or remove them with

port echo leaves
sudo port uninstall leaves

You might need to repeat the process since uninstalling leaves may create new leaves!

Port will also leave the outdated ports on your system. To remove them use

sudo port uninstall inactive