Just moozing

Before you can check your notes, you must make them…

Cash machine, python and opencv

leave a comment »

I have been looking at playing cash machine and I want to test different strategies – so I thought it made sense to make an automatic system for playing the game. It ended with a dive into python and openCV, a combination I have had on my to-look-at list for a long time.

Elements of the program

If you have a tendency of getting addicted to this sort of stuff, don't start...

If you have a tendency of getting addicted to this sort of stuff, don’t start…

As I see the design, we have four distinct parts:

  1. A live feed of what the browser shows when playing the game.
  2. Some image processing to convert the input to something machine understandable.
  3. Some logic to decide where and how to drop the current items.
  4. A way of generating the events that mimics the arrow keys, to actually execute the decision.

Part 1, I am planning on being some sort of gstreamer virtual v4l2 device, that opencv will read from as if it were an ordinary camera. I have done this before.

Part 2 is mostly implemented. As of this writing, the program is able to extract the current score and it detects the items already dropped. It may not recognize all of them, but I decided that the logic for something like that would reside in part 3.

Part 3 is the logic of how to play. There will be some kind of fixed logic for some tasks, but this part is interesting from a player’s point of view.

Part 4 would be covered by something like xdotool, but I know that there a dedicate python libraries for it also. Since it is only up, down, left and right, I don’t foresee this being a big issue.

The code is available on gihub.

Comment, lessons learned and such

  • Opencv is easy to work with. The documentation is ok, and there are a lots of examples to “seek inspiration from”.
  • The latest opencv accessible in python by “import cv2” uses numpy arrays for image representation. It works – so go with that one.
  • Numpy array uses array[y,x]. Yes, x and y are not as you would expect of image data, so check which part of the image you are actually working on 🙂
  • Also, I did the usual thing where I did the infamous off-by-one when working with arrays. Specifying the endpoint is always interpreted as not including the endpoint.
  • Unittesting works. Use it.
  • It wanted to test stuff related to mock objects, but since all I have done so far is extracting stuff from an image, I did not get to it. That being said, I think I did a fair job structuring my tests. I still have an issue of where to put test data – I cannot run all tests due to path issues. Suggestions or links related to this sort of directory handling in unittests are welcome.
  • I need to focus on other stuff now, so the rest (like the remaining 80%) is put on hold. It is just too bad that I don’t have all the time in world to do pet projects 😦
Advertisements

Written by moozing

May 7, 2013 at 09:00

Posted in Tech

Tagged with , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: