I found a baby mouse in my hallway today. I’m not sure how it made it that far from its mother; it was moving pretty slowly. I saw the trail it made through the drywall dust in the partially finished bedroom in our house, so I placed it back in the hole it came out of. I can only hope its mother finds it. As much as I would have loved to take care of it, I live with a wonderful little kitten named Gizmo. He didn’t pounce on the mouse when he saw it, but I don’t see them becoming fast friends…
v4l2 camera module now in Pygame SVN
My Google Summer of Code Project has finally grown up and flown out of my personal git repo to enter the Pygame SVN. All development will now occur there, though I will leave my git up for archival purposes.
The project is a module for Pygame to support v4l2 cameras. The impetus for this was to make the webcam on the OLPC XO usable without intimate knowledge of GStreamer, and to allow for some basic computer vision. The module does, however, support many camera pixelformats beyond those used on the XO, and is pretty flexible to adding new ones.
At the moment, there is no support for v4l cameras beyond detecting that they are attached to the computer, and there is no support at all for Windows or OS X. René Dudfield is starting work on Windows support using directshow. OS X support will come eventually.
The API documentation for the module is pretty thorough, but I haven’t uploaded any tutorials or example scripts yet. For now, you can look at some of the scripts I’ve posted on this blog.
RocketPong – A One Button Game
I couldn’t sleep, so I decided to write a quick entry for the Game Creation Society‘s One Button Game Compo.
It requires Pygame 1.8, which can be installed through a package manager on many distros, or can be downloaded from Pygame.
The game is a pong clone with gravity. The one button activates thrust on your paddle, which happens to look like a rocket. In theory, there is pixel perfect collision detection, so the angle of the ball against the curve of the rocket should matter. The opponent rocket is controlled by some really really horrible AI. The game would be infinitely more fun with two players, but I wasn’t sure if the one button rule meant per player or total…
Update: I uploaded a better version of it with more accurate collision detection and better AI. There is also a two player mode that is activated by hitting the right arrow key, which is the thrust key for the second player. This is the final version for the game competition, and very likely the last time I will touch the code.
Download:
rocketpong.tar.gz
rocketpong.zip
Things I’m Doing (or Queueing at least)
I am chronically unable to finish projects because I tend to pick up a new one more often than I complete an old one. Here is a list of things I’m currently working on, in no particular order.
- Turning Bridge into a real activity. This means using the datastore, dropping Elements in favor of directly interfacing PyBox2D, and making multiple levels of play.
- Writing a working Panorama activity. Ben Schwartz wrote some excellent autostitching using Numpy; it just needs some camera code to make capturing frames easier and to disable auto white balance and so on. I stuck Ben’s activity in a git repo, and I plan on making my changes soon.
- Commiting the rest of my GSoC project to Pygame SVN. I committed everything but the actual camera module so far. At the very least, I need to set up a build environment in Windows and make sure that the camera fails gracefully, since it only supports v4l2 at the moment. Before Pygame 1.9 is released though, I plan on writing VFW and maybe Quicktime support.
- Writing an Augmented Reality Game (ARG?). Augmented Reality is pretty much the holy grail of computing for me. I played around with ARToolkit a bit, but I really want to get into it when I have more time.
Of course, in addition to that, there is attempting to find a job, taking care of my kitten, finishing moving into my house, and loads of schoolwork. The schoolwork is mostly interesting though. It currently consists of writing object classification and image seam carving in Matlab, coming up with screenplay ideas, and playing with a Gumstix board. I’m also going to be starting some computer vision research with Professor Martial Hebert of the Vision and Mobile Robotics Laboratory at the Robotics Institute. More on that when it happens…
OLPC Physics Game Jam is Rollcats
The OLPC Physics Game Jam has effectively become a Rollcats Game Jam.
Regardless, there are some pretty exciting physics based games being created here, and a whole lot of accidently amazing demonstrations.
Daniel Drake and I are writing a bridge building game which may be downloaded as we work on it, from:
Living Pointillism packaged as an OLPC Activity
I packaged the script I wrote a few posts back as a .xo file that can be installed on an OLPC XO. This isn’t really because the script itself is that great, but because I wanted to test a way to deliver the Pygame stuff I’m working on for GSoC for the months until my code is merged, Pygame 1.9 is released, and the package is included on XO’s. This also explains why a 2KB Python script is now a 750KB .xo that takes up over 2MB when installed.
So, while the Activity itself just runs the Living Pointillism script, the activity directory (/home/olpc/Activities/Pointillism.activity/ on my XO running release ~690) contains the whole Pygame library with my modifications to date. So, you can just drop scripts (like these or these) that use pygame into that directory and run them from there, and they will use the local pygame instead of the one installed on the XO. This saves you from having to install all the stuff necessary to compile pygame on an XO.
Download: Living Pointillism
Pixel Perfect Collision between Real and Virtual Objects in Pygame
The Quick Version:
I wrote a few scripts to test the possibility of using pixel perfect collision detection in pygame to allow for interactions between real life and on screen objects. They require the installation of my branch of pygame, which includes support for v4l2 cameras. The download links for the scripts (including OLPC versions) and pygame source are at the bottom of the post.
The Verbose and Occasionally Tangential Version:
Joel Stanley of OLPC sent me a patch for my GSoC project a few days ago, along with a link to a picture of an exhibit at The Tech Museum of Innovation in San Jose, in which a person can manipulate virtual falling sand with his or her shadow. That is exactly the kind of killer tech demo I’ve been looking for for my project. The kind of thing that anyone could pick up in an instant and realize the beauty of human computer interaction. Of course, the museum setup had a controlled environment, a projector, a screen, and probably a whole lot of processing power. I have hundreds of thousands of kids around the world on 433mhz laptops.
At first, in response to Joel’s email, I had just planned to describe how to get the equivalent of the shadow. It obviously could not require a projector, a screen, a uniformly colored background (green screen), or even a consistant source of light to project a shadow. Instead, this requires an initial calibration step. When the scripts start, they wait for the user to hit a button. The user should then get out of view of the camera, so it only sees the background. It then waits a couple seconds and takes a picture of the background. The shadow is then created by thresholding frames currently being captured against the original background image. This actually works pretty well as long as the background isn’t moving. So, play it with the camera facing a wall if possible.
I had planned to just leave it at that, but then I figured since I already have that written, might as well just add a few lines of code to see if I could do pixel collisions between the shadow and objects on screen. Nothing complicated like sand, just a bubble on screen that the user pops. When this worked, I decided to extend it by having it place a new bubble on a random spot of the screen whenever one is popped. A few minutes later, my friend stopped by and asked what the hell I was doing jumping around in the middle of the room. I told her about the vision stuff, and then we both started jumping around in the middle of the room, popping fake bubbles. Who would have thought something that simple could be fun? That script is Pop Bubbles; you can download it at the bottom of the post and jump around your own room.
I thought I might be finished there, but it was still a far cry from the sand demo that Joel saw. I decided making the bubble move could be fun, so I added a fixed “velocity” of pixels that the bubble would move every frame if it wasn’t popped. It would be pretty silly if it kept going off the edge of the screen, so turned the edges into “walls” that would reverse the x or y velocity if the ball hit them. I then tried adding “gravity” by having the y velocity increase one step downward each frame, which turned the bubble into a ball. Then I added some inelasticity by decreasing the velocity a little upon impacting walls. Since the bubble was now pretty much a bouncy ball, I made it no longer pop upon hitting the shadow. Instead, it would bounce off the shadow in the opposite direction of where the shadow hit the ball, also adding some more velocity. By this point, I had a hideous doppelganger of physics that would make Newton wish he never saw an apple tree. This is what Bouncy Ball is, at the bottom of the post. Try it at your own risk. It is absurdly glitchy, and really only responds well to slow movements.
This still isn’t quite what the sand demo is, but I think it comes close enough to prove that it would be possible in Pygame. The biggest thing that is necessary is a real physics engine, which Zhang Fan is currently working on for Pygame as a GSoC project. Its likely that I will need to extend the bitmask module in pygame to make things like pinching an object possible. If anyone wants to improve this stuff, please do, there is a lot of room for it. I’d be happy to help out any way I can. I do hope to have something closer to the sand demo by the end of the summer.
As a note to OLPC users, I know its pretty inconvenient to have to build the library on the XO. I’m still working on packaging an .rpm and an .xo that contains all of my demo scripts. Also, for now, to get it running at a usable speed on the XO, it has to be at 320×240, which makes it pretty un-immersive, but I’m working on ways to scale it up without sacrificing much performance.
Download Python Scripts:
Bouncy Ball
Bouncy Ball (OLPC)
Pop Bubbles
Pop Bubbles (OLPC)
Download Pygame with camera module source:
Pygame 1.8.1 with camera module
Checkout Pygame with camera from git:
git clone git://git.n0r.org/git/pygame-nrp
Living Pointillism: A pygame webcam script
Living Pointillism is a quick little example script I wrote to showcase the camera module I’m writing for pygame through GSoC. It places a few dozen points (it automatically changes the number based on the framerate) for every frame of video, so the image is only clear if everything is perfectly still. This also results in some interesting visual effects when things are in motion. The screenshot above doesn’t quite capture the essence of it, but I don’t have the kind of bandwidth here to upload a video.
To use it, you need pygame with the camera module, which is currently only available from my repository: http://git.n0r.org/?p=pygame-nrp;a=summary
It runs fairly well on the OLPC XO too, and I’ll release an Activity for it along with an rpm of my branch of pygame soon.
Download: Living Pointillism
Update: This is now available packaged as an OLPC Activity.
Update 2: Pygame with the camera module should now be downloaded from the Pygame SVN.
HOWTO: Debug Colorspace Conversion
Pygame Webcam Support
Update: Grab the up to date code in Pygame >1.9.1 from the Pygame repository.
There was a three day weekend here, so I got some real coding done on my GSoC project.
You can pull the code I’m working on from my repository at http://git.n0r.org/?p=pygame-nrp;a=summary
It is currently barely alpha quality, but it supports v4l2 cameras that use MMAP and YUYV or RGB24 pixelformats. That means it’ll work on the OLPC XO, though you’ll need to install gcc, SDL-devel, and probably some other stuff to compile it.
You can open a camera with:
cam = camera.Camera(device, (width, height))
Device in most cases is going to be "/dev/video0"
. (width, height)
will probably be (640, 480)
.
You then need to start the camera with cam.start()
.
cam.get_image()
will return an RGB Surface.
cam.close()
closes the camera if necessary.