Pencil Icon

Predicting fly movements with OpenCV and Python

A few weeks ago I filmed a fly just flying around on my iPhone, I wanted to see if I could create a program that after a while predict it’s next turn and it’s direction either left or right.

In short the process was:

  1. Cache frames in the iPhone clip
  2. Detect static objects (walls and lamp shade)
  3. Motion track fly into a path
  4. Detect changes in course/corners
  5. Print time taken between corners (without another perspective calculating acceleration was a failure because I couldn’t tell if the fly was coming towards or away from the camera)
  6. I could however get 3D info from the scene and as I filmed it again without camera movements I didn’t worry about tracking it in 3D

Most of this was done with OpenCV with Python bindings and math, data was encoded in JSON.

Calculating the % of left/right started with a random guess from and then built up confidence with each corner. I might put up the code on GitHub at some point if I finish it properly (the code is a bit of a mess atm)