Skip to content

Educational computer vision project exploring real-time object detection. Demonstrates OpenCV pipelines, PyTorch fine-tuning, and CUDA-accelerated inference with YOLOv8 on a playing cards dataset.

Notifications You must be signed in to change notification settings

Raghuboi/card-detection

Repository files navigation

Playing Card Detection with YOLOv8

Educational computer vision notebook exploring real-time object detection for Hi-Lo blackjack counting practice. Demonstrates PyTorch + YOLOv8 training, OpenCV inference pipelines, and CUDA-accelerated ML fundamentals.

📓 Open card-detection.ipynb to get started

Results

  • mAP@0.5: 99.5% on validation set
  • Inference: 30+ FPS on RTX 4070
  • Dataset: 21,203 training images, 52 classes (13 ranks × 4 suits)
  • Model: YOLOv8n (3.2M params, 6MB)

What's Inside

card-detection.ipynb - Comprehensive notebook covering:

  1. Dataset Exploration - YOLO annotation format, class distribution, bbox analysis
  2. ML Fundamentals - Loss functions (CIoU + BCE + DFL), training loop concepts
  3. Model Training - YOLOv8n fine-tuning with data augmentation
  4. Inference Pipeline - Manual NMS implementation, OpenCV visualization
  5. Hi-Lo Counting - Blackjack counting strategy with state management

Tech Stack

  • PyTorch 2.5.1+ (CUDA 12.1)
  • YOLOv8 (Ultralytics)
  • OpenCV 4.8+
  • matplotlib, seaborn (visualization)
  • NumPy, pandas (data analysis)

Quick Start

Setup

pip install ultralytics opencv-python torch matplotlib seaborn pandas
jupyter notebook card-detection.ipynb

Requirements: Python 3.10+, CUDA 11.7+ (for GPU acceleration)

Project Structure

card-detection/
├── card-detection.ipynb              # Main educational notebook
├── datasets/
│   ├── train/                        # 21,203 training images
│   ├── valid/                        # 2,020 validation images
│   └── data.yaml                     # Dataset config (52 classes)
└── backend/runs/card_detection/
    └── weights/best.pt               # Fine-tuned model weights

Key Concepts Covered

Dataset & Annotations

  • YOLO annotation format (normalized bounding boxes)
  • Class distribution analysis and balancing
  • Bbox aspect ratio and area statistics
  • Data augmentation (mosaic, mixup, HSV jitter)

Training Pipeline

  • Transfer learning from COCO pretrained weights
  • Multi-component loss function: 7.5×CIoU + 0.5×BCE + 1.5×DFL
  • AdamW optimizer with learning rate scheduling
  • Training metrics visualization and interpretation

Inference & Post-Processing

  • Manual NMS (Non-Maximum Suppression) implementation
  • Confidence threshold tuning (0.5-0.7 recommended)
  • OpenCV visualization pipeline
  • Real-time performance optimization

Hi-Lo Card Counting

  • Blackjack counting strategy implementation
  • State management (running count, seen cards)
  • Duplicate detection prevention
Card Range Value Strategy
2-6 +1 Favors player
7-9 0 Neutral
10-A -1 Favors dealer

Performance

Metrics (after 5 epochs):

  • Precision: 99.36%
  • Recall: 99.51%
  • mAP@0.5: 99.5%
  • mAP@0.5:0.95: 70.77%

Inference Speed (RTX 4070):

  • 30ms/frame (33 FPS)
  • ~2GB GPU memory

Dataset

Roboflow Playing Cards Dataset

  • 21,203 training images
  • 2,020 validation images
  • 52 classes (13 ranks × 4 suits)
  • Well-balanced: ~400 instances per class

Learning Resources

Author

Raghunath Prabhakar GitHub LinkedIn

Acknowledgments


Educational project for hands-on computer vision learning MIT License

About

Educational computer vision project exploring real-time object detection. Demonstrates OpenCV pipelines, PyTorch fine-tuning, and CUDA-accelerated inference with YOLOv8 on a playing cards dataset.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages