Monday, June 11, 2007

Pong Playing Computer playing against another computer using a webcam as an eye!


Download video (5.54 MB)

Several years ago, when I was a stupid little schoolboy, I created a really fun and addictive game called Ping Pong. It was based on the classic arcade game, PONG, and had some cool features like gradual speed increase, two game modes, and some neat sound effects.

I had almost forgotten about that game until I saw this auto wood-chopper project in which a computer plays a simple game on a PSP using only a webcam to view its screen. I thought the idea was pretty good. So, I decided to make my own computer play that old pong game of mine on another computer using a webcam for vision! I fired up my code editor, and after several hours of coding and experimentation, my image recognition code started looking good. It uses edge detection to determine the boundaries of the ball and the bat in the webcam’s field of view. The area of the ball is more than the bat, so that’s how it differentiates between the two. The other steps are more or less similar to the things I did for my object tracking app. Like the auto wood-chopper dude, I thought about using motors to press the keyboard keys on my laptop. However, I thought it would be overkill when I could write a simple TCP client/server program to establish communication between the two computers.

Be sure to watch the video above! I’d love to hear what you think. Oh and here’s a similar project in which a computer plays Minesweeper on another computer using a webcam – wow!

You can download my fun Pong game from here. (1.20 MB)

This was a very exciting project. In my opinion, one of the coolest webcam based projects I’ve done so far. I entered the world of webcams after working on CamCapture – a project based on a Coding4Fun article by Scott Hanselman. :) Webcams are fun but I sometimes wonder why they’re called Webcams. I never use them for online video conversations! In my opinion, they should be simply called USB cameras. :)

38 comments:

  1. Wow...you're amazing.

    ReplyDelete
  2. Simply cool .. :-)

    ReplyDelete
  3. Man this pisses me off, the damn computer plays pong better than me! ;)

    ReplyDelete
  4. i want to have time to do such things ...
    Very cool :>
    Next step : a robot & real ping pong table ?

    ReplyDelete
  5. Well done Ashish! that was well thought of and executed.

    ReplyDelete
  6. really cool! you are becoming an expert on cameras and image recognition! :)

    next step is face recognition for a robot, i think ;)

    ReplyDelete
  7. Thanks everyone. :D

    @nico: Real ping pong? That's a really cool idea..but I bet it'll be really difficult too! Well..I guess nothing is impossible. :)

    @niko - italy: Thanks for the comment. :) I've been thinking over face recognition for quite a long time..its a really cool thing. Some people have definitely achieved face detection..I have no idea how advanced face recognition is right now. I guess face detection should be the first step.

    ReplyDelete
  8. Soooo ehm who won?

    ReplyDelete
  9. I'm a little disappointed.

    With the information provided you could have your "bot" determine the angle and velocity of the ball in a fraction of a second, and have the paddle be exactly where the ball will hit every time. Rather than simply matching the y-coordinates.

    And better yet, it could adaptively use the full length of the paddle to put the ball as far away from the other player as possible.

    Nice idea, but you could do better.

    ReplyDelete
  10. @jared: I thought about determining the angle and velocity of the ball..but then I thought it would just make things more complicated. However, I think I'll give it a try. :)

    ReplyDelete
  11. That's pretty cool! So who won? And how long did it take? I assume neither opponent won if you didn't increase velocity.

    ReplyDelete
  12. Yeah, they keep playing for hours if I don't increase velocity. If I make the ball go really fast, the image recognition code ultimately fails to track the ball quickly enough. :)

    ReplyDelete
  13. Awesome!!!!!!

    ReplyDelete
  14. Thats pretty neat ! Keep up the interesting work.

    ReplyDelete
  15. Great Idea ! Now we are closer to Terminator Scenario!

    ReplyDelete
  16. In a hurry, but check this out: http://www.imageaddicts.net/uploads/collision.jpg

    ReplyDelete
  17. After work, I checked the file I uploaded. The resolution was low. Try this higher resolution grayscale version:

    http://www.imageaddicts.net/uploads/collisionBIG.jpg

    Eventually I will get around to fully implimenting this method in my flash pong game.

    For anyone out there looking for info on line intersection detection or how to test if two line segments intersect, I have plenty of code to share.

    ReplyDelete
  18. Nerd... Nerd... Nerd...

    ReplyDelete
  19. it is cool. very cool enough~

    ReplyDelete
  20. Not bad ;) I love pong!

    ReplyDelete
  21. Cool software Ashish.

    ReplyDelete
  22. What programming language did you use for the pong game?

    ReplyDelete
  23. You could've used Camstudio, instead of Screen Recorder Gold.

    ReplyDelete
  24. What's the background music?

    ReplyDelete
  25. can u help on how to do that? would u mind to send me steps to make that.... just like yours...im interested b4 in programming..and iv seen what youve got so i like it

    ReplyDelete
  26. you can contact me @gbtniel@yahoo.com

    ReplyDelete
  27. When both PCs become "aware", we'll have you to thank. Very cool. Nice tune selection.

    ReplyDelete
  28. This comment has been removed by a blog administrator.

    ReplyDelete
  29. Good Job! :)

    ReplyDelete
  30. This is a great site. Thank you for your information. I THANK YOU I SALUTE YOU IT,S A AMZING SITE.

    ReplyDelete
  31. Hey,

    Thanks for that important information, it really helpful..Interesting article

    ReplyDelete
  32. It was your great attempt yaar

    ReplyDelete
  33. It is extremely interesting for me to read this post. Thank you for it. I like such themes and everything that is connected to this matter. I definitely want to read a bit more soon.

    ReplyDelete