I think the reason I so quickly latched on to this idea was because Magic the Gathering and Dungeons & Dragons are two of my favorite games, plus I really dig the Ixala art work, I mean who hasn’t fantasized about riding a dinosaur to work.
Now I absolutely own tons of Magic the Gathering cards, and I could easily grab a handful and start coming up with an epic dungeon delve, but I thought, why not include a little bit of technology.
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.
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if cv2.waitKey(1) == ord('q'):
For demonstration I’m going to show you a snapshot of the stream through this process:
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: