Moss-Machines-Magic-the-Gathering-sorting

Moss Machine - Open Source Magic: The Gathering - MTG - Sorting & Recognition

Welcome to the Moss Machine project! This open-source initiative provides a comprehensive system for automating the sorting, recognition, and management of Trading Card Game (TCG) cards. The system supports 317 different card games including Magic: The Gathering, Pokemon, Yu-Gi-Oh!, and many others.


Overview

The Moss Machine combines perceptual hash-based computer vision, multi-threaded processing, and hardware integration to create a high-performance, scalable card sorting solution. Whether you’re a collector, player, or developer, this project offers tools and guidance to build your own automated card sorting system with speeds capable of processing cards in under 2 seconds per scan.


Features


System Architecture

Software Components

  1. optimized_scanner.py - Core recognition engine
    • OptimizedCardScanner class with multi-threaded game scanning
    • Perceptual hash computation and comparison
    • Database connection pooling (thread-local connections)
    • Adaptive threshold scanning
    • Serial communication with Arduino
    • Real-time webcam mode
    • Command-line interface with extensive filtering options
  2. gui_interface_enhanced.py - GUI control interface
    • Tabbed interface (Scanner, Arduino, Export)
    • Live camera feed with card detection visualization
    • Arduino parameter management and monitoring
    • CSV export with scan history
    • Game/Set/Rarity/Foil filtering
    • Auto-scan and manual scan modes
  3. Main.ino - Arduino firmware
    • 3-axis stepper motor control (X, Y, Z + E0, E1 dual drive)
    • VL6180X ToF sensor integration for distance measurement
    • Endstop monitoring (6 endstops: X/Y/Z min/max)
    • Dual vacuum system control
    • LCD status display (16x2 I2C)
    • Serial command parser with start/end markers
    • 34-tray coordinate system with automatic routing
    • Homing and calibration routines
    • State machine (Stopped/Started) for safe operation

Database Structure

Hardware Components


Quick Start

Software Setup

  1. Install Python dependencies:
    pip install opencv-python pillow imagehash pyserial numpy
    
  2. Database:
    • Place unified_card_database.db in the project directory
    • Or let the scanner auto-download from configured server URLs
  3. Run the scanner:
    # Command-line mode (single image)
    python optimized_scanner.py card.jpg --cache
    
    # Interactive mode with Arduino
    python optimized_scanner.py --interactive --serial-port COM3
    
    # Realtime webcam mode
    python optimized_scanner.py --realtime --cache
    
    # GUI mode
    python gui_interface_enhanced.py
    

Arduino Setup

  1. Install Arduino libraries:
    • LiquidCrystal_I2C
    • Adafruit_VL6180X
    • Wire (built-in)
    • Stepper (built-in)
  2. Upload Main.ino to Arduino Mega 2560

  3. Connect hardware:
    • RAMPS 1.4 to Mega
    • Stepper motors to X, Y, Z, E0, E1 outputs
    • Endstops to min/max pins
    • VL6180X sensor to I2C (SDA/SCL)
    • LCD to I2C
    • Vacuum relays to pins 9 & 10
    • LED to pin 8
  4. Configure serial port in Python scripts (default: COM3, 9600 baud)

Usage Examples

# List all supported games
python optimized_scanner.py --list-games

# List sets for Magic (game ID 167)
python optimized_scanner.py --list-sets 167

# Scan a card, filter to Magic only
python optimized_scanner.py card.jpg -g Magic --cache

# Scan with set filter (Alpha and Beta)
python optimized_scanner.py card.jpg -s LEA -s LEB

# Filter by foil type and rarity
python optimized_scanner.py card.jpg -f Foil -r M -r R

# Adaptive scan with relaxed thresholds
python optimized_scanner.py card.jpg -t 20 --min-confidence 70

Configuration

Scanner Parameters (optimized_scanner.py)

Arduino Parameters (Main.ino)

Tray Layout (Main.ino)


Pictures


Disclaimer


Build Your Own

Get Started


Disclaimer:
This system is intended for hobbyist and educational use. Always test with non-valuable cards first. Handle valuable or rare cards with care—never trust automated sorting in critical scenarios. The project is open source; use at your own risk.


Connect with Us & Follow

Discord Logo Reddit Logo GitHub Logo
Join our Discord:
https://discord.gg/2gNWpV6UjW
Reddit:
r/MossMachine
Repository:
GitHub

Support

Help keep my motivation

Other non-related support:


Acknowledgments & License

This project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0).
Please give credit to the original authors when creating derivatives.


Happy Sorting!