Two weeks ago, I discussed an outline of how to predict when then level crossing near my flat is open or closed. I made an aside that I could use a camera to detect when the crossing is open or closed.
I’ve realised that I have an old tablet that will work great for this purpose. I can put it on the windowsill plugged into it’s charger and use it to report back on the level crossing.
Now I just need to implement the technology to make that happen. I’ve got two choices here: it’s a windows based tablet so I can create an executable using something like openCV or I can create a browser based application.
A while back I started doing a project which relied on HTML5 to do some basic image recognition. I’ve decided I’m going to try that approach again because it makes my solution so easy to port. If I get a new device, as long as it runs the Chrome web browser then it should be able to do the exact same task. The development also requires only two really simple tools, the browser and a text editor to write code in.
Here’s the plan of attack:
- I’m going to create a page with a HTML5 video element and a 2D canvas element
- The video from the device’s camera is set to be the source of the video
- At fixed time periods the video is copied into the canvas
- Once an image is loaded into the canvas, the individual pixels can be read
- The pixels are read in an intelligent way to determine if the level crossing lights are flashing or not.
- This data is sent to the server
The first four steps are fairly trivial to implement once you know what you are doing. The fifth step is where the magic happens.
This image recognition task is actually quite simple. Determining if a light is on or off is a simple task. What makes the task more difficult is the fact that the lights are sufficiently far away from my flat that they are quite small, they are also right behind a set of traffic lights and the lighting conditions change dramatically throughout the day.
My first approach is to try to resolve the barrier lights from the traffic lights. Its just about possible with the camera on the tablet. I simply need to compare the red value of the point where the lights are against the red value of another reference point just to the side of the barrier lights. I need to make sure I choose this reference point such that it is always darker than the lights no matter what the illumination conditions.
Now I just need to set the camera very still on my windowsill!
I set the webpage to requests a PHP page whenever the state of the crossing changes. The PHP page updates a MySQL database. I’ve then created another simple PHP page which reads the data from the database and displays a pretty graph. In my third post I’ll report on my data findings.