Piping numpy arrays to other processes in python

To pipe data from one process to another as a stream in python we need to pickle the object and pass it to the pipe stream.
In this example I’ve used Numpy arrays but this could be applied to any object that can be pickled in Python.
This took far too long to get working and I could find little information online on how to put it all together so here it is.
This code is Python 3 only, I’ve only run this on a Mac.

I’ve used binary as the stream rather than text purley becuase of effiencies. Numpy arrays can get huge! This means readline() is not
going to work. Instead, I send a single control byte , 1, for data and 0 for stop. This could be extended to include other control operations.
I then send the length of the data as a 8 byte int, followed by the data itself.

simpleSend.py

import numpy as np
import pickle
import sys
import io
import time

#define some control bytes
control_data=bytes([1])
control_stop=bytes([0])

def send_data(arr):
    dataStr=pickle.dumps(arr)  #pickle the data array into a byte array
    dlen=len(dataStr).to_bytes(8, byteorder='big') #find the length of the array and
    print(control_data.decode('latin-1'),end='',flush=True)  #convert this to a byte array
    print(dlen.decode('latin-1'), end='', flush=True)   #encode the data and write it
    print(dataStr.decode('latin-1'), end='', flush=True)  # end='' will remove that extra \r\n

def send_stop():
    print(control_stop.decode('latin-1'), end='', flush=True) 

#set the stdout such that it prints in latin-1,   sys.stdout.detach() is a binary stream
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding='latin-1')

for p in range(10):
    arr=np.ones((5000,500))*p  #generate some data
    send_data(arr)
    #the sleep is purely for testing and can be removed, ie does the reader fall over after a long delay
    time.sleep(.1)
send_stop()        

simpleReceiver.py

import numpy as np
import sys
import pickle

#define some control bytes
control_data=bytes([1])
control_stop=bytes([0])

while True:
    data=sys.stdin.buffer.read(1)   #read the control byte
    if data==control_data:
        data=sys.stdin.buffer.read(8)  #read the data length
        dlen=int.from_bytes(data, byteorder='big')
        print('data lenght %d'%dlen)        
        data=sys.stdin.buffer.read(dlen) #read the data        
        npd=pickle.loads(data)  #unpickle
        print(npd.shape)
        print(npd.max())
    elif data==control_stop:
        print('stopped')
        break
    else:
        print('Oh no')

to run this
python simpleSend.py | python simpleReceiver.py

If we want to use Python’s subprocess module to start simpleReceiver.py we basically need to write to the STDIN instead of print

import numpy as np
import pickle
import sys
import subprocess as sp

#define some control bytes
control_data=bytes([1])
control_stop=bytes([0])

def send_data(arr,buff):
    dataStr=pickle.dumps(arr)  #pickle the data array into a byte array
    dlen=len(dataStr).to_bytes(8, byteorder='big') #find the length of the array and
    mp.stdin.write(control_data)
    mp.stdin.write(dlen)
    mp.stdin.write(dataStr)
    mp.stdin.flush() #not sure this needed
     
def send_stop(mp):
    mp.stdin.write(control_stop)
    mp.stdin.flush()
     
try:
    mp = sp.Popen("python3 simpleReceiver.py",  shell = True,stdin=sp.PIPE)   
except sp.CalledProcessError as err:
    print('ERROR:', err)
    sys.exit(-1)

for p in range(10):
    arr=np.ones((5000,5000))*p  #generate some data
    send_data(arr,mp)
send_stop(mp)        

With such a large array 5000×5000 this takes sometime. Running it through the python profiler indicates about 75% of the time is taken by pickle.dumps and most of the rest of the remaining 25% is taken by the write operation. Numpy’s own method gives a speed increase. Replacing dataStr=pickle.dumps(arr) with dataStr=arr.tobytes() and npd=pickle.loads(data) with npd=np.frombuffer(data) more than halves the time taken but lose the shape and dtype information. This would have to be sent along with the data.

view raw

numpyPipe.md

hosted with ❤ by GitHub

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 ""

How to write lossless video in Python

OpenCV does a reasonable job of reading videos from file or webcams. It’s simple and mostly works. When it comes to writing videos,
it however leaves a lot to be desired. There is little control over the codecs and it is almost impossible to know which codecs are installed. It also wants to know things like the frame size at intailisation. This isn’t always a problem, but if you don’t know it yet it means you have to set up the video writer inside your main processing loop.

To make something as cross-platform compatible as possible it would be nice to use FFmpeg. There are a few python wrappers around, but as far as I can tell they are mainly used for transcoding type applications. One solution is run FFmpeg as a subprocess and set its input to accept a pipe. Then every video frame is passed through the pipe. You write this yourself, in fact it’s only a few lines of code. However, the scikit-video package will do this for us, with some nice boilerplate to make life easier.

The steps are:

  1. install FFmpeg — if you running on Linux use your system’s package manager if it’s not already installed. If you’re unlucky enough to be using Windows you need to download the zip file from here, and add the bin directory to your system’s path.
  2. install scikit-video –I tried installing scikit-video via pip on my Anaconda distro but the version was too old. Instead, I cloned the github version and installed that. Instructions are provided on github.

Below is a simple example the grabs from your webcam and records lossless video.

#test recording of video
import cv2
import skvideo.io


capture=cv2.VideoCapture(0) #open the default webcam
outputfile = "test.mp4"   #our output filename
writer = skvideo.io.FFmpegWriter(outputfile, outputdict={
  '-vcodec': 'libx264',  #use the h.264 codec
  '-crf': '0',           #set the constant rate factor to 0, which is lossless
  '-preset':'veryslow'   #the slower the better compression, in princple, try 
                         #other options see https://trac.ffmpeg.org/wiki/Encode/H.264
}) 
while True:
    ret,frame=capture.read()
    if ret==False:
        print("Bad frame")
        break
    cv2.imshow('display',frame)
    writer.writeFrame(frame[:,:,::-1])  #write the frame as RGB not BGR
    ret=cv2.waitKey(10)
    if ret==27: #esc
        break

writer.close() #close the writer
capture.release()
cv2.destroyAllWindows()

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.