2012-12-30

Aquarium Surveillance

Surveilling fish in an aquarium with a simple webcam is a fun activity for young and old. The following post describes a simple software setup that was used to continuously upload live still images of a small aquarium.


Nothing more is required than an aquarium, a webcam and a Linux computer with Internet connection. Optionally the aquarium may be substituted by a hamster cage. ;)

Probably ffmpeg could be used as an alternative to stream live video. However it was found that ffmpeg crashes rather often and that the encoding of popular video formats is CPU intense, which is especially infeasible on older computers. Another alternative for live video would be to use mjpegstreamer, which is especially resource preserving. Unfortunately the project seems to be inactive now and no 64-bit binaries are provided.

Therefore it was concluded that the best compromise would be to stream still images at the maximum resolution of the webcam and a refresh rate of a couple seconds. Many programs are suitable to acquire images. In this case fswebcam was chosen, because it is small and simple and can be found in the Debian/Ubuntu repositories.

Watching the fish should also have a social component. Therefore an Xajax sample program was used to provide a simple comment function ("graffiti wall"). The sample program can be tested and downloaded here.

The following packages need to be conveniently installed from the software repositories if not already present:
sudo apt-get install apache2 php5 fswebcam

Normally the images acquired with fswebcam would be written to disk continuously. To avoid letting the hard disk suffer too much, a ramdisk is created. This can easily be done by adding the following line to /etc/fstab:
#ramdisk for aquarium photos
ramfs  /var/www/aquarium/aqua_ramdisk ramfs defaults 0 0

You can download the script and the modified xajax web program here.
The webcam.sh script needs to registered for automatic startup. A description how to do this can be found here. The web program can simply be copied to the apache web folder eg. /var/www/aquarium.

2012-12-27

"Distributed" Video Mass Converting

Assume the following situation that occurred to me recently:

There is a slow computer A with lots of disk space (eg. an old computer that is used as a NAS) and a faster computer B with only little disk space. A holds gigabytes of video clips in many different files that you want to do some CPU intense processing on, eg. re-encoding to another video format.

So what you probably want to do for every video clip is the following:
1. download video clip from A to B
2. process video clip on B
3. upload converted clip from B to A
4. delete original (and converted) clip on B

It is assumed that B can ssh/scp into A and that public key authentication is used. Otherwise it would be necessary to enter the password for every individual file. Moreover it is assumed that A and B are connected by a sufficiently fast network.

 A simple script (runs on B) that was used to perform the steps 1-4 can be found here. An additional example script is provided that uses ffmpeg to scale video clips down to 720p.

2012-10-24

OpenCV Background Subtraction in Python

Recently, I tried  finding an example of Background Subtraction being done in OpenCV and Python without success. The code is not complicated or special in any way. However, I guess, the example might be helpful, if you want to get started with Background Subtraction quickly.

The script reads a video file and writes out a mask for every frame.
#!/usr/bin/env python
#-*- encoding: utf-8 -*-

import cv2
import numpy

bgs = cv2.BackgroundSubtractorMOG(24*60, 1, 0.9, 0.01)

capture = cv2.VideoCapture("00185.MTS.mp4")
cv2.namedWindow("input")

a = 0
while(True):
    
    f, img = capture.read()
    fgmask = bgs.apply(img)
    
    #cv2.imshow("input", fgmask)
    #cv2.waitKey(1)
    cv2.imwrite("./pngs/image-"+str(a).zfill(5)+".png", fgmask)

    a = a + 1
    print(a)

Unfortunately there seem to be no Python bindings for BackgroundSubtractorMOG2 at the time of this writing.

2012-10-06

Buggy projectM Music Visualizations

Today I tried visualizing some music using projectM and qmmp. Using a qmmp plugin provided in the Ubuntu repositories, music visualization was especially easy to set up and use.

Unfortunately I found that some of the visualizations are quite buggy and can easily end your music listening experience with a nasty crash of the music player.
The faulty visualizations are part of the Ubuntu package projectm-data (version 2.0.1+dfsg-12).

Here is a script that removes all visualizations that are faulty in my opinion. I do not claim this list to be exhaustive, but in my case it ended disturbing crashes.

2012-09-20

Using the National Geographic Gimp Filter to postprocess video clips

Are you also a bit unsatisfied with movie editing programs like Kdenlive or OpenShot? In image processing programs like Gimp, you have such fine grained control and myriads of available filters to alter still images in every possible way. In contrast to that, movie editing programs like the ones mentioned above only offer you a handful of filters to create an individual look for your video project.

Fortunately it is quite easy to use a combination of ffmpeg and Python Gimp-fu to exploit all of Gimp's capabilities for batch processing video clips frame by frame. Here is a small example, where I applied Gimp's National Geographic Filter on a short clip. Depending on your system, you might need some patience, if you plan to process longer clips.



The scripts that were used to this can be found here. It is quite easy to modify it to use any other arbitrary set of Gimp filters, image processing operations, etc.

2012-07-09

Creating Timelapse Videos with a Webcam

Last week I had some fun creating timelapse videos using just an inexpensive webcam and a laptop. Timelapse videos are a nice way to visualize processes which are otherwise to slow to observe directly (eg. the movement of clouds or the growth of plants). Technically the the solution to all the world's problems seem to be little Python scripts, which can be downloaded here. ;)
For simplicity a lot of things are hardcoded in these scripts, so you most certainly have to make changes. The scripts should be easy to understand, as they are quite short and simple. However if anything is unclear, just drop me a comment.

timelapse.py, timelapse_copy.py

timelapse.py uses ffmpeg for capturing images from the webcam (support for v4l assumed). You do not need this script, if you have any other means of acquiring the individual frames. The filenames of the acquired images contain a consecutive nr. and the time and date when the image was taken. Unfortunately it turned out later that times and dates in the filename confuse ffmpeg and have to stripped away to join the individual frames to a single video file. Besides the nr. needs to have leading zeros. The timelapse_copy.py script was made to compensate for that.


timelapse_filter.py

As mentioned before, an inexpensive webcam was used for simplicity. Therefore a Gimp plugin is used to sharpen and denoise the pictures a little. The script uses Gimp and the package gimp-plugin-registry (for the wavelet denoise). You can skip this step if you have a good camera or are not willing to wait, because the filtering can take quite some time, depending on which filtering operations you want to run on the individual frames. Do not forget to copy this script to ~/.gimp-2.6/plug-ins and to make it executable.

timelapse_ffmpeg.py

A third script creates a video from the filtered images again using ffmpeg.

2012-07-04

Überweisungsträger bedrucken (German)

Die Großmutter bedruckt nach Großmutters Art Überweisungsträger mit dem PC. Dabei nutzt sie (leider) folgende Vordrucke+Software: Avery Zweckform 2816
Die zugehörige Software hat die Qualität, die man von einem Etikettenhersteller erwarten kann (Vorlage für Microsoft Word mit Makro). Da der Firma offenbar schon bewusst ist, dass ihre Software teilweise nur "eingeschränkt" funktioniert, (kleiner Tipp: Die Schrift ist auf dem Formular verschoben, wenn man die Schriftgröße bei Windows auf 125% stellt.) gibt es noch eine Webapplikation. Diese unterstützt allerdings nicht das Formular 2816.

Hier ein kleines Python Skript, dass die gleiche Funktionalität für das 2816 Formular erfüllt. Ich hoffe, dass ich damit auch anderen weiter helfen kann, die sonst auf die Avery Software angewiesen wären.


Das Programm füllt eine HTML Vorlage aus und konvertiert diese mittels wkhtmltopdf (muss installiert sein, z.B unter Ubuntu mit sudo apt-get install wkhtmltopdf) in ein Pdf, dass man leicht ausdrucken kann. Für die Justierung kann man leicht in der Konfigurationsdatei die beiden Offset-Parameter umstellen und evt. auch die Ausrichtung für die einzelnen Textfelder verbessern bzw. das Programm für eine andere Vorlage umschreiben. Wer die Justierung ganz perfekt machen will, sollte mit der justierten Avery Software einen Überweisungsträger bedrucken und dann nochmal mit diesem Programm auf die gleiche Seite. Danach kann man mit dem Lineal die Abweichungen ausmessen und in der Konfigurationsdatei entsprechend korrigieren. Nach der Installation und Justierung sind aber zur reinen Benutzung der Software keine tiefergehenden technischen Kenntnisse erforderlich.

Das Programm wurde unter Ubuntu getestet. Falls notwendig, sollte eine Portierung auf andere Platformen aber nicht zu schwierig sein. Im Gegensatz zur Originalsoftware von Avery kommt man ohne den Kauf von Microsoft Office aus. Die kleine Tkinter-GUI zeigt nur die Elemente, die für die reine Nutzanwendung notwendig sind und sollen so weniger erfahrene Computernutzer nicht überfordern.

Das erzeugte Pdf wird jeweils unter ~/Dokumente/btof/ mit Datum im Dateinamen abgelegt. Dadurch werden die Überweisungen leicht nachvollziehbar archiviert, ohne dass sich der Nutzer über ein besonderes Ordnungsprinzip bzw. Dateien und Dateinamen überhaupt Gedanken machen muss.

Hier nochmal der Link zum Download

2012-05-20

Checking Opto-Fibers with a Smartphone

Todays discovery is simple, but nevertheless practical. Do you need the check optical fibers (e.g. from SFPs or AVAGOs) from time to time? It's sometimes hard to see if they are on, plus you probably do not want to direct a small laser right into your eye.
The solution is to use the camera of a smartphone, webcam, etc. This approach is especially useful, if the frequency is infrared or close to it, because the CMOS sensor is more sensitive in this frequency range.

2012-03-18

Using Latex to create a Bibliography with different categories

Have you ever tried to create a Latex bibliography with different categories, e.g. textbooks, internal documents, WWW? Fortunately the multibib package allows us to do so.



If your document class is book, scrbook or something similar, you will probably not going to be very delighted about the result. multibib does not much more than creating a new bibliography for each category. But in the document classes mentioned above, bibliographies are regarded as chapters, which have page breaks and an empty page after them.

To fix this, the definition of the bibliography command of your document class needs to be renewed to declare bibliographies as sections instead of chapters. If you happen to use scrbook, these style files might be helpful.