Thursday, May 22, 2014

Stop Motion Movie

I have always loved these movies where you see the clouds rushing by. Where a scene is played fast forward through a day, seeing plants open up, are traffic increasing and decreasing over time.
The way that is done is usually taking a picture from the same position with some time interval and then putting them all in a movie. Or you can slowly change the position of your camera.

Anyway, I was working with the webcam last week for my object recognition project so I had the knowledge fresh in my head. I quickly made a program that finds your webcam(s) and then takes pictures with fixed time intervals (adjustable in whole minutes).
I tested it out on the view outside my window hoping for the bad weather that was said to be coming. It did not come. So here you can see a movie of pictures with a one minute interval of clouds (and sometimes a bird).



For those who are interested, I ran this program in Processing 2.2.1 (64 bit) and that worked fine, some earlier version were giving me problems.  My own program can be found here, but you'll have to change certain things to make it fit your own needs.

Webcams

I attached an extra webcam to my laptop, so there is a choice of two. But besides that, the list Processing shows gives different sizes and frame rates for you webcam(s). The list starts with 0, but you can fill in any number from the list and you should get an image.
Find the line that says:

cam = new Capture(this, cameras[0]);

Maybe there's a different number then 0, but that's the place to change it. You should find it around line 26 in the code. I picked a very low resolution for testing, so the video looks bad in my opinion. For future video's I think the only limitation is the size of my hard drive.

Other tweeking

The time step, called 'step' in the code was set to 1, but you can change it to any whole positive number between 1 and 59 you like.
It could be necessary to change the screen size, because that is set to 'size(640,480)' at the moment.
Files are save as 'hour+minute.jpg' in a directory called '/images'. You could change that to your own preferences.

Making the movie

In the original pictures I had the camera wasn't positioned horizontally and I had to rotate them. I did this in Matlab because the rotation was the same for every image and using a script was a lot faster then using Photoshop or some other image program.
When my images where rotated correctly and square again I loaded everything in Windows Movie Maker, which is downloadable for free. Ones they were all loaded it selected them all and under 'Edit' you can set the 'Duration' of every frame. I set it to 0.25 seconds which gave me a smooth running video.

I saved the movie and uploaded it to YouTube. What I think is pretty neat about YouTube is that editing is really simple. There is music to pick from and it even suggests things like video stabilization.

So I hope this was useful for somebody. Nothing is stopping you from placing your webcam somewhere and making a cool movie.

EDIT:
After a request I have modified the program so you take pictures any multiple of seconds.
Click here to download.




Tuesday, May 20, 2014

Do It Yourself Tracking Software

Introduction

In a preview I showed a radio controlled boat where I hacked the remote control. With an Arduino Uno you can send a signal to the remote which then sends it to the boat. A small video of my daughter trying it out to show it works.


My ultimate goal is to make the boat autonomous. I want to use a webcam to determine the position of the boat and then be able to steer in the right direction. So that could be making sure it doesn't crash into the wall or that it follows a certain route, I haven't made up my mind about that yet.

Choosing the system

When I started with this project I was thinking about all the obstacles and steps I would find along the way. But I soon realized that I was making the rules here. I could choose what system I was using and how difficult it was going to be. That sounds a bit vague so I guess I'll give an example. When I saw the remote control I immediately was thinking about the Arduino. Nowadays there are quite a few ways for computer-machine interaction but since I have some experience with the Arduino I knew I would have less problems implementing it.

One of the bigger challenges was using a webcam and it's images to send the right commands to the Arduino and so the boat. I followed part of a course last year on Coursera called: Creative Programming for Digital Media & Mobile Apps. All the apps they worked with were built in Processing which has a very low entry level and let's you make Android apps and JavaScript apps. It's really meant for fun user interfaces.
Bonus: It's really similar to the Arduino language and even the programs look similar. So if you know one, the other will be a cinch.
And yes you can access the webcam with processing and use the images to do whatever you want.

Object tracking

That was a long intro, bear with me.
Processing doesn't have object recognition functions built in and I figured any program you could find on the internet would not be suitable for what I wanted. So I decided to make it myself which is always more fun.
Accessing the webcam wasn't that hard (there are examples in Processing) but I needed some boundaries.
What did I want to recognize? The boat, but I didn't feel like building a Machine Learning algorithm that knows what the boat looks like and finds. Possible I guess, but pretty hard work. I wanted something simple.
I made the choice of placing a colored sticker (or maybe two) on the boat, let's say red. I intend to place the webcam above the water and I can choose what color bottom of the water will have. I could make sure that the only red thing in the webcam image is the red sticker. This is what I talked about earlier, I can make the choices of how difficult the problem is going to be. I have reduced the problem to finding a red dot in an image.

Finding the red dot

To give you a break from all this reading, here is a demo of my Processing program using the webcam on my laptop to follow a small red object.


In my hand I'm holding a small piece of red plastic. The program takes a webcam image and analyses it in two steps.
First it performs a threshold. Using the fact that red objects are red :) it assigns a '1' to pixels that have lot's of red and little green and blue (i.e. white pixels have lot's of red, green and blue) and it assigns a '0' to all the other pixels.
Next it calculates how many pixels in the neighborhood are found red and finds the pixel with the most neighboring red pixels. That neighborhood can be quite big, for instance this was done with 10 by 10 pixels.
The way these calculations are done can and will be topic of a separate blog entry.

To show the result it draws a white circle at the found position.
You can vaguely see image it works with around the white circle. That represents the number of pixels around every pixel. If you would have a stepfunction in your image the result of counting the neighboring pixels would look like something below.

Sketch of how the counting of neighboring pixels would look like
It reminds me of a Gaussian blur. Most important is that you see that the middle of the original platform results in a high peak in the second image. Important is choosing the right neighborhood size as you can imagine.


In the near future

Keeping the main goal in sight, tracking and controlling the boat there is still some work to be done.
I intend to place two stickers one the boat, a blue one and a green one and track them both. With the position of these two stickers you can determine the position and orientation of the boat. If you use previous positions and orientations you can calculate the speed and direction of the boat.

Then I have to think of some rules the boat has to follow like: don't touch the walls, or go around in circles.

So far the program is perfectly capable of doing all the calculations in real time, and I hope that it still does so when all these features are added.

To be continued...

The boat


Thursday, May 1, 2014

Friend speed

How fast can I get Facebook Friends?


41,23 friends per hour!

Awesome! You must wonder what that means and where I get this from.

A while ago I registered for a Facebook profile because:
1) All the cool kids are doing it
2) I want to watch holiday and baby pictures from friends
3) I wanted to do an experiment

Everyone I spoke about opening a FB account told me you get friends really fast. So I set out to see if this was true.

The experiment

When you don't change anything on the original settings you get an e-mail message about everything that is happening. That's why people switch this off immediately. You're on FB 24/7, you don't need see the messages twice, right?
I left the settings as were and registered every friend request and all the request I sent myself. The graph below shows you how that looks like. Yes, this was tedious work.



Most of it is quite clear I guess. On the horizontal axes we see the time in hours, and on the vertical axes the total number of friends, and the number of requests I sent myself. The numbers in the top are just the days, so we start on day one somewhere in the morning.
Funny thing is, within one minute of registering I received two friend requests without any other action then signing up.That means that those people somehow saw that I had joined, not sure how that works.
I sent maybe two or three requests, but I waited to see what would happen. The number of friends is rising, what happened?

Well in this case one of my first friends made friend suggestions and since I was in need of friends I clicked them all I guess. During day two I didn't do anything and I noticed that the rate at which my number of friends was growing was slowing down.

Sending requests

Therefore I sent out a lot of requests on day three, which you can clearly see from the quick rise of the blue line. And yes! I got an immediate response and my total number of friends sky rocketed. Let's zoom in on that event.

Total number of friends after sending lots of requests.

In this picture we can see that the total number of friends start to grow quite fast until.... people go to sleep. You can see  it stops at about hour 72 which is the end of day 3. A bit more than eight hours later it picks up again.

Friend speed

So I mentioned speed, or 'friend speed' as I like to call it. How do we calculate that? There are a couple of ways to calculate that. You could for instance look in every hour how many friends were added. Downside is, that in most hours no friends were added at all and in some others maybe four of five. This would be a very non informative graph.
A second method is calculating the time between two confirmations. Plus, you get a value for all the data points. Minus, some friends sent their confirmation at the same time (the same minute). That means the time was zero between them and so the speed was infinite at that moments. Infinite works very bad in graphs.

To solve that problem I let R (used in the graph making) draw something of an average through the data points, which is called an LOESS fit as can be seen in blue in the image below.

Blue line depicts a LOESS fit on the friend datapoints.

I think you lose 90% of your crowd with every graph you use, so I'm down to 0.1% of my readers.
On this line I performed this calculation I just mentioned of calculating the time difference between two events which should be an approximation of the speed. In the image below you can see the calculated 'speed' compared with the total friends data.



As my number of friends is rising very promising we see a terrifying event. The speed of gaining friends is dropping. We see what we expect, at nightfall the friend speed drops to almost zero, and during day 4 the speed is very low. But there is something wrong in my opinion about the speed data. Of course we used this LOESS fit as an approximation of our data which will give rise to errors. But, in this graph it looks like during day 3 the friend speed is decreasing at a constant rate. And for those who love differentiating and integrating and all, that would mean that the total number of friend should be parabolic. I therefore don't trust this graph all too much and tried it in a third way.

Tangents

Since some regions in the graph seem almost linear it's easy to draw a tangent line there and use that to estimate the speed.

Tangent lines to estimate the friend speed.

In day 3 it looked there were two areas. One in the beginning consisting of people who responded as soon they got the friend requests on their mobile device and some hours later people checking in later.
In the first section the climbed was huge and I estimated it to about 41,23 f/h !! (friends per hour of course). During the rest of the day it settled to a more moderate 3,66 f/h and the next day it dropped to a sluggish 0,70 f/h.

Conclusions

So what the message to take home? When you ask to become friends of FB, they respond pretty fast. The number of friends has to reach a maximum because you only know that many people and most friends you gain comes from the people you invite yourself. So automatically to speed slow down. Good thing too, if the speed stayed of about 40 friends per hour I would so may friends by now I wouldn't be able to keep up with the news feed.

Lesson two: you can't really say anything on such a small dataset and especially on something that is not reproducible. So there is no real science here, I just wanted to make pretty graphs.

(note 2 self: repeat experiment until friends no longer respond)




Tuesday, April 1, 2014

Work in Progress

Teaser update


I haven't posted in a long time and that is partly because I was busy on some stuff I really want to post here.
So this post is a teaser about projects I hope to finish soon.

Sudoku Solver


The first project is a Sudoku Solver. I have liked Sudoku's for a long time and love to philosophize about some interesting features. I know that making a Sudoku Solver is like a first year's Computer Science assignment, but I decided to try and figure it out by myself without any help from other sources.



So far the program is able to solve what you would call Hard Sudoku's, but not the most difficult ones.
Most puzzles only require you to take away the possibilities to find the answers.
For the hardest Sudoku's you have to take more than one thinking step to find more answers. I have plans to improve the algorithm and I believe it will be able to solve any (solvable) Sudoku.

I've made it in Matlab, which is really handy to work with on your own computer, but not so nice to show to other people across the internet. So I still have to do a lot of work to get it online.

Computer Controlled Boat


My first Arduino project was a Led Cube which was a lot of fun, but I was dying for something new and more challenging. When I found a small RC boat at my work I immediately knew what I wanted to do.
I screwed open the remote control and looked where the buttons were giving their signals. When you place a voltage on the right pins you get the same effect as pressing a button. The Arduino is perfect for that.
You can allow the Arduino to send signals to the remote control depending on some input. We could use the keyboard to control the boat for instance. I decided to use Processing, which is very similar to the Arduino language, to communicate with the Arduino via the USB cable.
The whole setup looks this:



On the left we see the Arduino Uno, on the right the opened remote control and to connect the two we see a breadboard in the middle. On the top we see the boat in question upside down so the rotors can turn while testing.

To make this project completely awesome I'll connect a webcam to my laptop to monitor the position and direction of the boat to make it autonomous.
This project is going pretty well so I hope to finish it in a couple of days.

So stay tuned!

Thursday, February 27, 2014

Muslim pilgrims per country

I seem to making a lot of maps lately, but it's gives these nice pictures that tell a good story.

Leiden is home to couple of fine museums one of which is Museum Volkenkunde (Museum of Ethnology). At the moment there is an exhibition on the Hajj, the pilgrimage to Mecca called 'Longing for Mecca'. It's on display until March 8th so you have to quick to go and see it.
My girlfriend, Elke Spiessens, did some research for this exhibitions especially tying to find the numbers behind the Hajj. Such as how do people travel to Mecca, or how many stones were used to throw at the Jamarat (stoning of the Devil) and also how many people travel from which country.
That last one struck me as an opportunity to make a nice visualization. These numbers are hard to find exactly and for a lot of countries they couldn't be found so there are some gray spots on the map.


In this map I included a bar plot so you can compare the different countries a bit easier and I thought filling in the numbers with arrows to the countries gives you a bit more feeling than just the colors. Small note, I didn''t have the number of pilgrims that comes from inside of Saudi Arabia. Also most numbers are estimates and based on quotas that are set. So every country can only send a certain amount of pilgrims, which is done for crowd control. The Hajj take place during 5 days in which over 3 million pilgrims visit Mecca and the surrounding sites.

Muslims per country

Looking back at this map I can strike you as odd. The top three countries that send the most pilgrims are Indonesia, Pakistan and India, where the first and third might not fit your typical middle eastern Muslim premise. 
If we look at the map found of Wikipedia showing the percentage of Muslims per country we see the following.

The percentage of Muslims per country

Which is I guess more what you'd expect. Countries in the Middle East and Northern Africa colored dark where a large majority of the people are Muslim (sometimes close to 99%). If you look carefully you can already see that Indonesia has a large Muslim majority  (about 86%). But if we look at the map showing the absolute number of Muslims per country we can understand the map with the number of pilgrims a lot better.

Absolute number of Muslims per country

This map also comes from Wikipedia and can immediately see what's happening. The top three countries Indonesia, Pakistan and India have a lot of citizens and so even if the percentages aren't as high as the Middle Eastern countries it adds up. So it turns out that almost two-thirds of the Muslims live in Asia-Pacific and I think that is a nice eye-opener.

How it's made

I just want to say a few words on how this map came together. As I mentioned in previous posts a great inspirational source is Visualize This by Nathan Yau and making this map was helped a lot by the examples found in that book. The data I had was run through a Python script to create the CSS styling needed to color in the SVG map. The bar plot was made in R, exported as a PDF, imported in Inkscape, styled to fit the map and then added. All the numbers and arrows were put in by hand in Inkscape.

Since this post is more focused on the story than on the technique I hope you find it informative. If you like to learn more about the Hajj and the people that take this pilgrimage go see that exposition before it closes.


Friday, February 21, 2014

How close are you to a school building?


In Leiden, The Netherlands there was quite a stir when a convicted pedophile was placed in a new house after being released. This left people in the city and especially the neighborhood angry and concerned. One of the points the mayor of Leiden made was that everybody needs to live somewhere. People who objected said out that there are schools nearby the house that was appointed and that of course made it a bad choice.

These arguments made me think. Is it possible to find a house that is not near a school?
My initial guess was that it was nearly impossible.

The data

So, to lighter subjects. Let's make a map! I first looked for a list of all the school in Leiden. That includes higher education. I must say that afterwards I realized I probably missed some school buildings. I didn't check if a certain primary or secondary school had more then one location. I also forgot some of the higher education facilities. And lastly I totally ignored kindergartens and such, which in my guess adds 30+ more locations.

The list I found for schools had addresses and postal codes included, as well as the type of the school. It took some time to get everything in a nice spreadsheet, but it was ok. I used Google's mapsengine to put all these locations on a map. There are some simple tutorials on the basics of Mapsengine, so within a few minutes I had a nice looking and useful map. I made a screenshot as you can see below or you can follow this link to play with interactively.

Locations of schools in Leiden

If you follow the link you'l be able to click on every location and see which school it is and it's properties.
You can also put your mouse over of one of the schoolmtypes in the list on the left and the school that belong to that type are highlighted. Pretty neat right. You can style this in a couple of ways and share it across the web.

Mapsengine is a really easy tool to use but in my opinion it's possibilities are restricted. For instance the border of the municipality had to be drawn in by hand. Since I only had to do this once this was no big deal. But Google Maps draws this shape if you search for Leiden, why isn't that feature included here?

Circles

When working on this is was thinking of a site that used Google Maps to show the impact of different atomic bombs on your favorite location. It draws circles around a position to show which areas are destroyed by the fire, which by the air blast, etc etc. Seems simple right, draw a circle around a point on the map. Sadly Mapsengine doesn't have this feature. What I wanted to do was draw a circle around every school with a radius of let's say 500 meters. I mean, that's close by right? Some browsing led me to a site called Freemapstools. You can upload a list of GPS coordinates plus the radii of the circles you want to draw and it makes a map with said circles. I did have to get all the GPS coordinates first. For that I used the website GPSCoordinaat, which only works for dutch addresses I'm afraid. I was just to lazy to find a different way to transform that data so I just filled in every address by hand. Sometimes doing a monotonous job is faster than trying to automate it.
Plugging all the coordinates in Freemapstools gave me the following map.

Circles of 500 m around school locations

Freemapstools allows you to export this map to a .kml file which you can import in Google Earth. (please comment if this doesn't work) That should look something like the image below.

Google Earth version

This allows to some zoom in nicely to check out those few areas that are not covered by a circles. Some of the empty spots are industrial areas or sport facilities and a large part is the city's center. Remember that I forgot a couple of locations and I didn't include kindergartens so a proper map would be probably be filled more than the above maps.

Conclusions

So what can we conclude from all this? 

1) Making a map is still a lot of work and not fully satisfactory
I really wanted to have both maps in one. The locations with the information on every school and the circles around them. And it would have been even better if those radii were adjustable so it would be more insightful. The radius of 500 meters was just a number I picked that sounded nice. A different radius like 200 meters or one kilometer would create a different story.

2) You're always close to a school building in Leiden
Leiden has a lot of people in a small area and a lot of schools, you can't really change that.





Wednesday, February 12, 2014

Map of Belgian municipalities and JSFiddle


In previous posts in showed the work I did on maps of The Netherlands. After that I thought it would be nice to see if I could do the same for Belgium. A benefit of looking at Belgian municipality borders don't change as much as the Dutch ones do.
As ever Wikipedia was a good source for a SVG version of the map I was looking for.

Map of Belgium with all the municipalities

There were only two problems. In the most recent maps of The Netherlands every municipality was a path and had it's name in the Id tag. You've guessed it, the Belgian map didn't have this. All the borders between two areas were a separate line. One municipality could be defined by maybe 5 lines but not one single area. So I needed some way turning all these lines into closed paths and putting the right name in the right path.

Inkscape SVG editing

I must add that Belgium has a very complex political system and maybe that is why they have over 400 municipalities.
To go from the border lines to paths I used a simple bucket fill in Inkscape. I think you lose some resolution in this process but for what I intend to do it's just important to get all the areas in the right place.
I found out that Inkscape has an Object Properties window (shift+ctrl+o if you're wondering) were you can change i.e. the Id and Title properties of an object in your SVG file. I had to use several .jpg maps which had the names of the municipalities on them and overlaid them with the empty SVG map.
This process cost me a couple of hours but I'm pleased with the result.

JSFiddle


It's possible to put an SVG directly into a blog. If you go to the HMTL mode you can just paste the SVG code in your post and it should show in the final version. But if it's too big it doesn't seem to work. So to show the map I made I had to try something else that didn't involve making my own website. I chose JSFiddle, a nice website to allows to create an account and to try out HTML, Javascript, etc and to share it with other developers.
The map, as is, can be found here. In this version of the map every municipality is one path, where the Id name and the Title are filled in. So if you hover with your mouse over an area you can see it's name (Title).

Future Work


If you would want to color every path individually to make a choropleth you still need to do some work and I intend to do that.
One thing I would like to add to the map is the different regions that exist, the governments, the districts, etc.
I also need some data to visualize. The Belgian government has some sites where they share data, but for now I didn't find them that insightful.
When I've made some examples I hope to post them soon.