I was really into trading cards as a kid. I recently came across a lot of Magic The Gathering cards in a box and thought to myself - I wonder how many cards I have and how much they're worth?! Logging and looking these up manually would take a while so I decided to see if I could automate some of the process. Somehow, the process led to using a Raspberry Pi, building a platform out of Lego and leveraging AWS S3/Rekognition!
The Process- Take a picture of the title of the card using a Raspberry Pi, RPi camera, and Lego platform
- Upload the pictures to an AWS S3 bucket for storage and processing
- Use AWS Rekognition to extract text out of the picture and query it against a pricing API to get the market price of each card
I'm not good at wood working and I thought that it might be rough on the cards. Instead, I decided to use Lego so I bought a medium bin you can get at several retailers. I challenged myself to stick to this box only - no other support hence why this thing looks bare bones. This project won't show how to build it brick by brick but there should be enough pictures here to be able to replicate it or make it even better! The design was inspired by a cheap $7 card sorter I got years ago. The servo in the back is able to continuously spin and moves the tires forward in a simple cog-like setup. The wheel at the front hanging out from the dark green piece is for keeping the other cards from slipping out. There is just enough room to push one card out at a time. I also used a few cards taped together to keep enough weight on the cards to ensure that only one came out. Full disclosure - you'll notice in the first video that a picture was taken when the card wasn't in position. This happened from time to time but it was trivial to remove the blank pictures.
The camera sits on a stack of bricks positioned a few inches away from the platform, angled so that is aligns with the position of the card. The resolution has been modified with code to capture the top of the part of the card.
Raspberry Pi was the best choice for this project as I was going to need to run python for the peripherals. The other things we need are two servo motors and a camera. I have a 5V power supply connected to the breadboard - not mandatory but helpful.
The CodeThe code is written entirely in python 2.7. One script is for powering the servos and taking the picture; the other is for processing the pictures stored in S3 against Rekognition.
Once we have our cards loaded onto the Lego platform, we can simply do:
python mtg_servo.py <set_abbreviation>
This will start the servos and scan the cards. Once it's done, we can quit the script and load more. I was able to do about 20-25 cards a minute. The <set_abbreviation> was a three letter code for the card set. This helps us stay organized for both image processing and the pricing API e.g. all of the .jpg's for the "M13" set were written to the <current_directory/M13> path.
AWS S3 and RekognitionI tried doing OCR with tesseract and OpenCV. While both are amazing tools, Rekognition proved much easier to use. It allowed a lot of flexibility for positioning, lighting, distance, etc. You'll need an AWS account in order to do this which is free. Amazon is pretty generous with their AWS Free Tier - you can process 5,000 pictures per month under it. I uploaded the S3 files manually for the sake of time (not shown but here's a guide). The s3 bucket was setup exactly like the current directory - /set_name/file.jpg. The screenshots below show the demo version of Rekognition processing some of the pictures taken. You'll notice that it's incredibly accurate, despite any issues with the photo.
We can automate this process! Once all of the cards have been uploaded to the bucket, we can run the code below to output our Detected Text into a csv:
python Rekognize_S3.py <set_abbreviation>
Here were the results of the image processing. I have no doubt this would've been much higher if the pictures had been of better quality. The other two main issues I ran into were (1) the font - many of the fonts had characters that were deceptively close to where even I had a hard time deciphering them and (2) lighting. Out of the 920 cards I scanned:
- 619 were exact (67.3%)
- 201 were off by one letter (21.8%)
- 100 were off by more than one letter (10.9%)
Sweet! After that, I wrote a quick python script to hit TCGplayer's API for the market price of the cards*. In the end, I had about $275 worth of commons, uncommons, and rares! (I removed all of the cards I knew were already worth money)
*Edit 05/27/18: I've updated the Rekognition script to run the detected text against the TGCplayer's API in real-time (and writing to a file). Keep in mind that there is an application process for the API.
ClosingI hope this inspires you to bust out those old cards and do something with them! I plan on doing this again with sports cards and various other sets. Happy scanning!
Comments