Low Cost Speed Camera using CHDK and OpenCV - Creative Uses of CHDK - CHDK Forum

Low Cost Speed Camera using CHDK and OpenCV

  • 4 Replies
Low Cost Speed Camera using CHDK and OpenCV
« on: 13 / June / 2014, 12:39:20 »
This project is very much still at the prototype stage but the objective is to build a fairly accurate speed detector for motor-sport, road traffic, RC hobby vehicles, athletes, etc. using little more than a Powershot camera running CHDK, and a BeagleBone Black (or Raspberry Pi) running OpenCV.

I wanted to meet a few primary requirements:
  • Low cost (suitable machine vision cameras start at £400 excl. lens, storage, etc.!)
  • Good image resolution (target detail such as competitor numbers - 720p)
  • Suitable for slow and fast moving targets (60mph +)
  • Quick to set up on location
  • Handle busy scenes (multiple targets e.g. one target overtaking another)

Now, I know it certainly wouldn't be the first camera-based speed detection solution but CHDK allows it to be done with cameras which you can get for less than £30 on eBay! ... I also know it's not too difficult to build Arduino-based speed detectors where the moving targets physically trigger simple sensors such as a laser beam but these methods often involve tedious equipment set-up and sometimes the hardware placement is impractical due to the location.

Failed Attempts
I started off trying to get two of my cameras (A810 & SX200) to take two images is quick succession but I couldn't get anywhere near the minimum 3FPS I needed for it to be really useful with fast moving targets. I tried various tricks from this forum, including digital teleconverter, low ISO, fast SD cards, manual settings, shoot_half, etc. but never achieved much better than about 1.5 frames per second. I then bought a SX220 with fast continuous shooting but this raised the next issue.  I needed the exact timestamp of each photo with millisecond accuracy, in order to get accurate results. All my attempts here failed miserably and I couldn't see any solution other than having some sort of physical millisecond clock display in the scene. The SX220 also has a CMOS sensor which raised yet another issue (see background below).

The method I settled for uses code from the excellent Manual Exposure Control for Video script (videxp3) by fudgey, waterwingz & msl to increase the video shutter speed. Setting the Tv to 1/1000 produces frames with very little motion blur, even when targets like cars are moving at 60mph at medium range. Removing the IR cut filter helped a bit with the low light performance at these high shutter speeds and it's easy to do to many Powershot cameras. To use video for speed calculations, I still needed the frames to be timed with millisecond accuracy but I wasn't sure these low cost cameras would be precise enough. So I made a crude millisecond display (described below) and filmed it at 1/1000 using my two CCD sensor cameras. The A810 used 25FPS and SX200 at 30FPS. To my surprise, both cameras only drifted about 1ms every 60 seconds and every frame interval was perfectly timed. The drift might even be due to the Crystal Oscillator, which tends to be a bit temperature sensitive.

A bit more work
One of the few CHDK challenges I still have to address involves reducing the camera FPS. With 720p resolution at the camera's native frame rate, the video files are massively oversized for the requirements and it's limiting any extended operation. The object tracking and subsequent speed calculations in OpenCV can be done efficiently at 5FPS so it will drastically reduce the storage and processing overhead if I can get the camera to record at 5FPS. At the moment, I simply discard 4 out of 5 frames from the 25fps video clips in openCV. A very exciting post by funnel about results he achieved controlling the FPS in video with a SX220HS (100a) showed this is possible, but I'll need to figure out how to replicate the same for one of my CCD cameras. It involves hunting for register values and disabling audio which is daunting for someone like me with no disassembly experience. I'll add a post in his thread to see if someone would be kind enough to give me a few pointers.

I'll post info about the OpenCV development once it starts to yield useful results, if anyone is interested. I'm using Ubuntu 12.04 and Qt for the OpenCV development.

CHDK Rocks!
Finally, I have to take my hat off to everyone in this CHDK community. Great job!
I'm fairly technically minded but I’m certainly no expert in any of the subject areas of this project so any ideas or feedback would be most welcome.

Just some further background:

My millisecond clock  (picture attached)
To test the video frame rate accuracy, I built a crude millisecond display.
All you need is:
   1 x IQXO-22C  - 32.7680MHz Crystal Oscillator
   1 x M74HC4020B1R - 14 Stage Binary Counter (to divide the frequency down to 2000Hz)
   2 x CD74HC4518E - Dual BCD Synchronous Counter
   12 x LEDs and Current Limiting Resistors
   Decoupling Capacitors (optional)

The 14 stage binary counter divides the oscillator frequency down to 2000Hz. The two dual BCD counters gives you a total of 4. Use the first stage of the first counter to divide the frequency one more time to get 1000Hz. The remaining three counters are then cascaded to drive 3 sets of LEDs showing the 1/1000, 1/100 and 1/10 values respectively.

Rolling Shutter
The main reason for using cameras with a CCD sensor is that they typically don't suffer from rolling shutter which would reduce the accuracy. Many machine vision cameras use CMOS sensors with global shutter features but that's not available in these low cost consumer cameras AFIK.

To allow accurate results from the video clips:
  • the camera must remain in a fixed position
  • the shutter speed must be high enough to eliminate motion blur
  • focal length must remain static
  • targets should move through the scene in a predictable straight line (roughly)
  • a calibration target must be filmed once the camera is mounted and ready (see below)

The calibration target mentioned earlier is used to compensate for the parallax effect and perspective distortion of the real world 3D scene in the 2D image. It also provide a way to superimpose reference distance measurements into the scene, used to calculate distance travelled between 2 frames for example. It can even be used to compensate for lens distortions from wide angle or fish-eye lenses, although this shouldn't be required with most Powershot cameras as the firmware already applies these corrections to the JPEG. The calibration target is simply a flat surface of suitable size with a chequered pattern like a chess board. You typically only need about 5x5 squares. This calibration target is then filmed in the scene where the real targets are expected to appear. The orientation can be vertical or horisontal, depending on the relative position of the camera. Once you've captured a few frames with the target in appropriate positions, it can be removed from the scene. In some cases, you may not need an artificial calibration target, as long as the shape, size and orientation of your real targets (or other objects in the scene) are know and suitable.


Offline ahull

  • *****
  • 634
Re: Low Cost Speed Camera using CHDK and OpenCV
« Reply #1 on: 13 / June / 2014, 19:28:57 »
I'll be watching this project with interest as there is a busy road outside my house.
It would be fascinating to get some accurate figures for the number of speed freaks passing the door.

I don't need to know who they are (and there are probably no doubt rules against gathering that kind of information, besides prosecuting them is not my job, the local traffic cops regularly sit in the village and reel them in by the dozen). It would just be  intriguing to know how many of them there are, and the rough distribution of traffic speed throughout the day. The idea appeals.... then again perhaps I'm just too much of a geek and should get out more  ;) .

Re: Low Cost Speed Camera using CHDK and OpenCV
« Reply #2 on: 14 / June / 2014, 04:36:59 »
perhaps I'm just too much of a geek and should get out more  ;) .

or stay in and build something totally pointless http://joyofblending.com/measure-blender-speed-methods


Offline ahull

  • *****
  • 634
Re: Low Cost Speed Camera using CHDK and OpenCV
« Reply #3 on: 14 / June / 2014, 08:29:07 »
perhaps I'm just too much of a geek and should get out more  ;) .

or stay in and build something totally pointless http://joyofblending.com/measure-blender-speed-methods

Interestingly enough I also thought of using the sound of the passing cars to try to determine their speed, I may revisit the idea.


Offline reyalp

  • ******
  • 14036
Re: Low Cost Speed Camera using CHDK and OpenCV
« Reply #4 on: 14 / June / 2014, 17:19:38 »
Another approach would be to use a single, relatively long exposure and measure the motion blur. This could be hard to automate on arbitrary scenes, but given a bright point source on the target should be pretty doable. You could use MD to detect when the target enters the frame.
Don't forget what the H stands for.


Related Topics