2013-01-27

Typesetting Word-By-Word Translations

Recently I was asked how to typeset documents with a word-by-word translation like the following:

Welche Farbe hat der gelbe Bus?
Which color has the yellow bus?

As I have learned, linguists have the fancy word Interlinear Gloss for this. There are several Latex packages available for this purpose. Among them is gb4e which I decided to use.

For simplicity it is assumed that the text to be 'glossed' is provided as a plain text file with sentences delimited by '.  ', '?  ' or '!  ' (2 spaces) and words separated by individual spaces. The implementation of a small script that creates a document with nicely aligned words is very straight forward. The dictionary needs to be provided as a .csv file.

Unfortunately the task can not be fully automated. Breaking text into sentences requires some knowledge about a specific language. So does breaking sentences into words. Ideally the dictionary should also have some capability to detect flections, etc. The script just generates a Latex file that can be modified manually.

The script can be downloaded here.

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.