Computer Vision : SAP internship week 3

Sean King
3 min readJun 27, 2017

--

So, last week I decided to pick up a Computer Vision project that had been started by another engineer on the team. The idea is to use a camera connected to a raspberry pi to track the score of a foosball game and to use data gathered from the match to create statistics that can be later used to build leader boards. Essentially the goal is to build a ‘Smart Foosball Table’. Initially the project included only a single script that had some basic tracking functionality.

The first step was to take a video of a foosball game being played (I used a mini foosball table that we had on hand in the room) and then attempt to get the ball being tracked around the screen. I’m using OpenCV which is an open source computer vision library with API’s for most of the popular languages. As I’m using the raspberry pi as my development machine (with a pycam module installed) I decided to use python (who doesn’t like using python?).

OpenCV allows you to easily pass a video via cmd argument and manipulate the frames of the video in various ways. The script I’ve written so far (heavily borrowed from a number of tutorials from pyimagesearch.com) essentially creates a binary mask of each frame (white on black background) whereby the color of my choosing (in this case I need to track a yellow foosball) shows up as white and everything else shows up as black. By finding the contours (borders) of the ball we can pinpoint the center of the object to be tracked.

(Apologies for the gif quality below)

The next step is to figure out when a player scores. Currently the version I have hacked together decides when a goal is scored by doing a check each frame to see if the ball being tracked has entered inside a border around the goal posts. There is a number of edge cases that need to be accounted for such as the possibility of the ball going over the goal posts rather than inside them. I’m considering adding additional ‘trackers’ to track the goal posts. That way if the table or the camera moves slightly it won’t make any difference in keeping track of scored goals.

Currently the entire program is running inside a single script. Once I have a few additional features written for the initial prototype I plan to break the program up into separate classes and functions. The code is already becoming difficult to look at and has very little structure.

--

--