Magic the Gathering Card Recognition

This weekend I took a bit of time to read up on OpenCV (Open Source Computer Vision Library), I wanted to capture images of Magic the Gathering cards, then identify them using a Python library called ImageHash.

Below is a demonstration of what I was able to accomplish in about 2 days of research and hacking:

I’ll try and break down the steps and image manipulation functions I used to achieve this.

1. Capturing Stream from Webcam

Capturing the video stream, then displaying it on screen, is quite simple.  What I did was create an endless loop where I capture a single frame, then display that frame. This loop will complete when the q key is pressed.

import cv2

cap = cv2.VideoCapture(0)

while True:
    ret, frame =

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('q'):


For demonstration I’m going to show you a snapshot of the stream through this process:

View post on

2. Convert Frame to Grayscale

It is common to convert your image to grayscale, this helps us threshold and identify our contours in later steps:

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

View post on

3. Convert Frame to Grayscale

A Threshold allows us to convert our images to absolute black, or absolute white. Here, I’m converting any pixel at color 130 or higher to color 255:

_, thresh = cv2.threshold(gray, 130, 255, cv2.THRESH_BINARY)

View post on

4. Find all Contours of Threshold Image

Using my threshold frame, we can find all contours:

_, contours, _ = cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

sorted_contours = sorted([ (cv2.contourArea(i), i) for i in contours ], key=lambda a:a[0], reverse=True)

for _, contour in sorted_contours:
    cv2.drawContours(frame, [contour], -1, (0, 255, 0), 2)

This ends up looking something like this:

View post on

5. Find card contour

Here is where I got lazy and decided to just hack it together.

If you notice in the above image, there is a contour for the entire frame and the next largest contour is our card’s edges. I decided for the time being to hard code our card contour as the 2nd largest contour:

_, card_contour = sorted_contours[1]
cv2.drawContours(frame, [card_contour], -1, (0, 255, 0), 2)

View post on

6. Drawing Card Corner Points

Next I pass our card_contour into opencv’s minAreaRect function, this gives us 4 points, one for each corner:

rect = cv2.minAreaRect(card_contour)
points = cv2.boxPoints(rect)
points = np.int0(points)

for point in points:, tuple(point), 10, (0,255,0), -1)

View post on

7. Identify Corners and Warp Image

This part was a little challenging, but I found an excellent explanation by Adrian over at

Basically, he takes advantage of numpy and how opencv provides box points in row / columns.

# create a min area rectangle from our contour
_rect = cv2.minAreaRect(card_contour)
box = cv2.boxPoints(_rect)
box = np.int0(box)

# create empty initialized rectangle
rect = np.zeros((4, 2), dtype = "float32")

# get top left and bottom right points
s = box.sum(axis = 1)
rect[0] = box[np.argmin(s)]
rect[2] = box[np.argmax(s)]

# get top right and bottom left points
diff = np.diff(box, axis = 1)
rect[1] = box[np.argmin(diff)]
rect[3] = box[np.argmax(diff)]

(tl, tr, br, bl) = rect

widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))

heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))

dst = np.array([
    [0, 0],
    [maxWidth - 1, 0],
    [maxWidth - 1, maxHeight - 1],
    [0, maxHeight - 1]], dtype = "float32")

M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(frame, M, (maxWidth, maxHeight))

The warped frame came out looking as follows:

View post on

7. Hash Image and Compare

First off, this solution will not scale! Be forewarned.

I grabbed 4 Magic cards from my collection, then grabbed an official image from the Gatherer website and stuck it in a directory.

After that, I hashed my warped image, then iterated over each official image comparing hashes:

from PIL import Image, ImageFilter
from glob import glob
import imagehash

# hash our warped image
hash = imagehash.average_hash(Image.fromarray(warped))

# loop over all official images
for orig in glob('orig/*.jpeg'):

    # grayscale, resize, and blur original image
    orig_image ='LA')
    orig_image.resize((maxWidth, maxHeight))

    # hash original and get hash
    orig_hash = imagehash.average_hash(orig_image)
    score = hash - orig_hash

    print('Comparing image to {}, score {}'.format(
        orig, score
print('-' * 50)

And that is it, the Goldenglow Moth card resulted in the following print:

Comparing image to orig/doom_blade.jpeg, score 25
Comparing image to orig/forst_breath.jpeg, score 26
Comparing image to orig/goldenglow_moth.jpeg, score 7
Comparing image to orig/kessig_wolf.jpeg, score 13

Remote Controlled Car using Raspberry Pi and Webcam



First thing I tackled was setting up the L293D H-Bridge on the Bread Board.

I found myself referencing the following Diagram a couple times.

Step one is connecting your chip down the center of your board:

From here I connected the 3 power pins to my board’s power rail using a few Jumpers:

A few more Jumpers connect each side of the chip to ground:

Finally I use a couple Wires to connect both sides of my power and ground rails:

Using a little bit of double sided tape I stick my board onto the Car Chassis:

I also wired up each DC motor, and the battery pack to the board:

Next I wired up my Raspberry Pi‘s GPIO pins, connecting them to the L293D.

Once I’ve verified the GPIOs were connected properly, I used a couple rubber bands to strap the Pi, Portable USB Charger (I used a Vans Shoe Charger) and Web Cam to the cassis:


On the Raspberry Pi I’m using Raspbian as the operating system, and installed a couple pieces of software:

* nginx
* Flask
* Rpi.GPIO

My nginx configuration is really basic and looks like this:

server {
 listen 80 default_server;
 listen [::]:80 default_server;

 root /var/www/html;

 location /stream {
   proxy_pass http://localhost:8080/?action=stream;
   proxy_set_header Content-Type "image/jpeg";

 location / {
   proxy_pass http://localhost:8000;


The Flask application is a bit janky, but gets the job done:

import RPi.GPIO as GPIO
from flask import Flask, render_template
from flask import request


GPIO.setup(14, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)

GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)

app = Flask(__name__)

def index():
return render_template(‘index.html’)

def left():
method = request.args.get(‘method’)
if method == ‘stop’:
sig = GPIO.LOW

GPIO.output(23, sig)
return "OK"

def forward():
method = request.args.get(‘method’)
if method == ‘stop’:
sig = GPIO.LOW

GPIO.output(15, sig)
GPIO.output(24, sig)
return "OK"

def backward():
method = request.args.get(‘method’)
if method == ‘stop’:
sig = GPIO.LOW

GPIO.output(14, sig)
GPIO.output(23, sig)
return "OK"

def right():
method = request.args.get(‘method’)
if method == ‘stop’:
sig = GPIO.LOW

GPIO.output(14, sig)
return "OK"

if __name__ == "__main__":’′, port=8000, debug=True)

While the template contains a bit of Javascript to handle button presses:




<link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

img {
margin-top: 25px;
margin-bottom: 25px;


<script src="" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>


// define our fired states to false
var forward_fired = false;
var backward_fired = false;
var left_fired = false;
var right_fired = false;

// keydown event will start motor
document.onkeydown = function() {

if(event.keyCode == 87) {

if(!forward_fired && !backward_fired) {
forward_fired = true;

button = document.getElementById(‘up’);
button.className = ‘btn btn-success btn-lg disabled’;

console.log(‘start forward’);

if(event.keyCode == 83) {
if(!backward_fired && !forward_fired) {
backward_fired = true;

button = document.getElementById(‘down’);
button.className = ‘btn btn-success btn-lg disabled’;

console.log(‘start backward’);

if(event.keyCode == 65) {
if(!left_fired && !right_fired && !backward_fired) {
left_fired = true;

button = document.getElementById(‘left’);
button.className = ‘btn btn-success btn-lg disabled’;

console.log(‘start left’);

if(event.keyCode == 68) {
if(!right_fired && !left_fired && !backward_fired) {
right_fired = true;

button = document.getElementById(‘right’);
button.className = ‘btn btn-success btn-lg disabled’;

console.log(‘start right’);


// keyup event will stop motor
document.onkeyup = function() {

if(event.keyCode == 32) {

if(event.keyCode == 87) {
if(forward_fired) {
forward_fired = false;

button = document.getElementById(‘up’);
button.className = ‘btn btn-default btn-lg disabled’;

console.log(‘stop forward’);

if(event.keyCode == 83) {
if(backward_fired) {
backward_fired = false;

button = document.getElementById(‘down’);
button.className = ‘btn btn-default btn-lg disabled’;

console.log(‘stop backward’);

if(event.keyCode == 65) {
if(left_fired) {
left_fired = false;

button = document.getElementById(‘left’);
button.className = ‘btn btn-default btn-lg disabled’;

console.log(‘stop left’);

if(event.keyCode == 68) {
if(right_fired) {
right_fired = false;

button = document.getElementById(‘right’);
button.className = ‘btn btn-default btn-lg disabled’;

console.log(‘stop right’);





<div class="container">
<div class="row">

<img src="/stream" class="img-thumbnail">


<!– control buttons –>
<div class="container">
<div class="row">


<button id="left" type="button" class="btn btn-default btn-lg disabled">
<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>

<button id="up" type="button" class="btn btn-default btn-lg disabled">
<span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>

<button id="down" type="button" class="btn btn-default btn-lg disabled">
<span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>

<button id="right" type="button" class="btn btn-default btn-lg disabled">
<span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span>





I haven’t yet configured the Flask and mjpg-streamer processes to startup automatically, so I connect via a shell and start each in a screen session.

Once that is done connect to the running nginx server using your browser, you should be presented with a user interface similar to this:

Use your keyboard to control the car, just like a video game the key W moves forward, S backwards, A left, and D is right.

Component List

Here is a list of each of the components (or comparable) I used during the setup:

Raspberry Pi 3 Model B Motherboard

Official Raspberry Pi 3 Case – Red/White

uxcell White 8.5 x 5.5cm 400 Tie Points 400 Holes Solderless Breadboard

Microsoft LifeCam HD-3000 Webcam – Black (T3H-00011), 720p HD 16:9 Video Chat, Skype Certified

INSMA Motor Smart Robot Car Chassis Kit Speed Encoder Battery Box For Arduino DIY

Adafruit Dual H-Bridge Motor Driver for DC or Steppers – 600mA – L293D [ADA807]

Kuman 120pcs Breadboard Jumper Wires for Arduino Raspberry Pi 3 40pin Male to Female, 40pin Male to Male, 40pin Female to Female Ribbon Cables Kit Multicolored Pack K45

Makerfocus 140pcs Breadboard Board Jumper Cable Wire Kit w Box

Happy Will 200 PCS Breadboard Jumper Wires/Jump Wire Mix Long and Short M/M for Circuit Board

Anker Astro E1 5200mAh Candy bar-Sized Ultra Compact Portable Charger (External Battery Power Bank) with High-Speed Charging PowerIQ Technology (Black)

Python says, Simon’s hipster brother

Many of you may remember playing with a Simon Electronic Memory Game when you were younger, you know something that looks like this:

At it’s core the game is rather simple, the device lights up random colors, and you need to repeat the pattern. Of course it gets harder the longer you play.

I thought it would be fun to build a Simon game using Raspberry Pi and a few electronic components:

I used the following components to assemble the project:

  • Raspberry Pi 3
  • 3x 330 Ohm resistor
  • 3x 1k Ohm resistor
  • White LED
  • Blue LED
  • Red LED
  • Breadboard
  • Assortment of wires

Here is a close up of the bread board and components:

The Raspberry Pi’s GPIO pins are then connected to the bread board,
and a small Python script powers the Simon game:


from RPi import GPIO

from sys import exit
from random import choice
from time import sleep

# define our pins for leds
white = 14
blue = 15
red = 18

# define our pins for buttonss
white_button = 21
blue_button = 20
red_button = 16

# disable warnings

# set the board to use broadcom pin numbering

# setup our LED pins as output
GPIO.setup(white, GPIO.OUT)
GPIO.setup(blue, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)

# setup our buttons as input
GPIO.setup(white_button, GPIO.IN)
GPIO.setup(blue_button, GPIO.IN)
GPIO.setup(red_button, GPIO.IN)

# create empty pattern list for simon says game
pattern = []

# create a list of our choices for simon says game
choices = [white, blue, red]

# starting difficulty based on blink durations
duration = 0.75

def add_color():
Append a random color to our pattern list

color = choice(choices)

def get_button():
Gets the next button press and returns

while True:
if GPIO.input(white_button):
return white

if GPIO.input(blue_button):
return blue

if GPIO.input(red_button):
return red

def blink(led, duration):
Blink a led for duration

GPIO.output(led, GPIO.HIGH)
GPIO.output(led, GPIO.LOW)

def blink_pattern(duration):
Blinks our pattern using duration as waits

for led in pattern:
blink(led, duration)

def check_pattern():
Checks our button presses against pattern

for led in pattern:
if led != get_button():
return False
sleep(0.3) # delay so button press doesn’t overlap
return True

def game_over():
Game over function

print ‘Pattern Length: {}’.format(len(pattern))
print ”’
_____ __ __ ______ ______ ________ _____
/ ____| /\ | \/ | ____| / __ \ \ / / ____| __ \
| | __ / \ | \ / | |__ | | | \ \ / /| |__ | |__) |
| | |_ | / /\ \ | |\/| | __| | | | |\ \/ / | __| | _ /
| |__| |/ ____ \| | | | |____ | |__| | \ / | |____| | \ \
\_____/_/ \_\_| |_|______| \____/ \/ |______|_| \_\


# blink all leds to show game over
for _ in range(3):
for c in choices:
blink(c, duration=0.1)


if __name__ == ‘__main__’:

# populate initial pattern

while True:

# blink back pattern

# check if our inputs were correct, else end game
if not check_pattern():

# add a new color to pattern

# decrease our duration to increase difficulty
if duration > 0.05:
duration -= 0.07


Happy Hacking!

Arduino values to Python over Serial

I’ve done a little bit of reading on the ReadAnalogVoltage of Arduino’s home page, and they give a straight forward way to read voltage from an analog pin.

I wanted to take this one step further and send the value over serial, then read it in Python using pySerial.

My setup is very straight forward, I have a Arduino UNO, a bread board, and a battery pack holding 4x AA batteries:


To start out I want to merely print the voltage value in Arduino Studio to the serial console, my code looks something like this:

[cpp title=”Arduino”]
void setup() {
// connect to serial

void loop() {

// read value from analog pin
int sensorValue = analogRead(A0);

// convert to voltage and print to serial connection
float voltage = sensorValue * ( 5.0 / 1023.0 );


Now that we’ve verified this works, lets make a couple modification to the Arduino code.

Since the value of the analogRead may be over 255 (more than can fit in a single byte), we will need to send two bytes, a high byte, and a low byte. This concept is called most significant byte, and least significant byte.

[cpp title=”Arduino”]
void setup() {
// connect to serial

void loop() {

// read value from analog pin
int sensorValue = analogRead(A0);

// get the high and low byte from value
byte high = highByte(sensorValue);
byte low = lowByte(sensorValue);

// write the high and low byte to serial


Then on the Python side we can use pySerial to read two bytes, and convert using the formula Arduino gave us.

[python title=”Python”]
import serial

# open our serial port at 9600 baud
dev = ‘/dev/cu.usbmodem1411’
with serial.Serial(dev, 9600, timeout=1) as ser:

while True:

# read 2 bytes from our serial connection
raw =

if raw:

# read the high and low byte
high, low = raw

# add up our bits from high and low byte
# to get the final value
val = ord(high) * 256 + ord(low)

# print our voltage reading
print round(val * ( 5.0 / 1023.0), 2)

One thing to take into consideration is, if we do not have voltage sent to the analog pin the result will be random and invalid. You will see this in the video before I connect the battery pack. Keep in mind my battery pack is producing about 5 volts:

Python and sentiment analysis

While looking for datasets to throw at sklearn, I came across UCI Sentiment Labelled Sentences Data Set.

UCI is providing us with positive / negative tagging on real world data, the data comes from three sources (Amazon, Yelp, and IMDB).

The only problem is the format is a little strange.. We have a .txt file for each source, this is a raw unstructured  formatting, plus not every line is tagged with sentiment.

To make the data easier to interact with, I generated a json file with only the results containing sentiment. Go ahead and download it.

$ zcat sentiment.json.gz | head -n 25
 "result": 0,
 "source": "amazon_cells_labelled.txt",
 "label": "negative",
 "text": "So there is no way for me to plug it in here in the US unless I go by a converter."
 "result": 1,
 "source": "amazon_cells_labelled.txt",
 "label": "positive",
 "text": "Good case, Excellent value."
 "result": 1,
 "source": "amazon_cells_labelled.txt",
 "label": "positive",
 "text": "Great for the jawbone."
 "result": 0,
 "source": "amazon_cells_labelled.txt",
 "label": "negative",
 "text": "Tied to charger for conversations lasting more than 45 minutes.MAJOR PROBLEMS!!"

Lets jump into an IPython interrupter and load the data:

In [1]: import json

In [2]: raw = open('sentiment.json').read()

Now that we have the data as a Python dictionary, create a DataFrame in the proper format:

In [1]: data = get_data_frame('sentiment.json')

In [2]: data.shape
Out[2]: (3000, 2)

Next lets split our full dataset into a training, and testing dataset:

In [1]: train, test = get_train_test_data(data, size=0.2)

In [2]: train.shape
Out[2]: (2400, 2)

In [3]: test.shape
Out[3]: (600, 2)

We are now set to run a bit of accuracy testing:

In [1]: test_predict(train, test)
Out[1]: {
  'test_score': 0.80000000000000004,
  'train_score': 0.98375000000000001

We can slice our full dataset a few more times, just to make sure our accuracy test is.. accurate:

In [1]: train, test = get_train_test_data(data, size=0.2)

In [2]: test_predict(train, test)
Out[2]: {
  'test_score': 0.79000000000000004,
  'train_score': 0.98416666666666663

In [3]: train, test = get_train_test_data(data, size=0.2)

In [4]: test_predict(train, test)
Out[4]: {
  'test_score': 0.80666666666666664,
  'train_score': 0.98291666666666666

In [5]: train, test = get_train_test_data(data, size=0.5)

In [6]: test_predict(train, test)
Out[6]: {
  'test_score': 0.79466666666666663,
  'train_score': 0.98999999999999999

All that is left is to feed in the entire dataset and predict on new sentences:

In [1]: predict(data, 'This was the worst experience.')
Out[1]: [
  (u'positive', 0.17704535094140364),
  (u'negative', 0.82295464905859583)

In [2]: predict(data, 'The staff here was fabulous')
Out[2]: [
  (u'negative', 0.20651083543376234),
  (u'positive', 0.79348916456623764)
In [1]: predict(data, 'I hate you')
Out[1]: [
  (u'positive', 0.22509671479185445),
  (u'negative', 0.77490328520814555)

In [2]: predict(data, 'I love you')
Out[2]: [
  (u'negative', 0.10593166714256422),
  (u'positive', 0.89406833285743614)

Lets put it all together by looking at the Python functions:


import re
import json
import random
import string

from pandas import DataFrame, concat

from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from sklearn.neighbors import NearestNeighbors
from sklearn.cross_validation import train_test_split
from sklearn.feature_extraction.text import TfidfTransformer, \

# update the pipeline to get best test results!
steps = [
(‘count_vectorizer’, CountVectorizer(
stop_words=’english’, ngram_range=(1, 2))),
(‘tfidf_transformer’, TfidfTransformer()),
(‘classifier’, MultinomialNB())

def id_generator(size=6):
Return random string.
chars = string.ascii_uppercase + string.digits
return ”.join(random.choice(chars) for _ in range(size))

def get_data_frame(filename):
Read tweets.json from directory and return DataFrame.

raw = dict(ids=[], text=[])

# open file and read as json
_data = open(filename)
data = json.loads(

# loop over all tweets in json file
for d in data:

# update raw list with tweet values.
dict(text=d[‘text’], classification=d[‘label’]))

return DataFrame(raw[‘text’], index=raw[‘ids’])

def merge(*args):
Merge two or more DataFrames.
return concat(args)

def get_train_test_data(data, size=0.2):
Split DataFrame and return a training and testing set.
train, test = train_test_split(data, test_size=size)
return train, test

def test_predict(train, test):
Run predictions on training and test data,
then return scores.

pipeline = Pipeline(steps), train.classification.values)

train_score = pipeline.score(
train.text.values, train.classification.values)
test_score = pipeline.score(
test.text.values, test.classification.values)

return dict(train_score=train_score, test_score=test_score)

def predict(data, text):

pipeline = Pipeline(steps), data.classification.values)

res = zip(pipeline.classes_, pipeline.predict_proba([text])[0])
return sorted(res, key=lambda x:x[1])