A machine learning project developed at UC San Diego as part of the ENLACE program for real-time geometric shape recognition using Support Vector Machine (SVM) classification and computer vision.
This system trains an SVM classifier to recognize geometric shapes (circles, squares, triangles, stars) from 16x16 pixel images and provides real-time classification through webcam capture. The project demonstrates practical machine learning applications in computer vision with optimized integer weights for potential hardware implementation.
- Multi-shape Support: Classifies circles, squares, triangles, and stars
- Real-time Classification: Webcam integration for live shape detection
- Optimized Implementation: Integer weight quantization for hardware efficiency
- High Accuracy: Achieves 98%+ classification accuracy
- Interactive Interface: Google Colab compatible with webcam capture
numpy
opencv-python (cv2)
scikit-learn
matplotlib
PIL (Pillow)
pathlib
- IPython.display
- google.colab (for drive mounting and file upload)
- base64 (for webcam functionality)
project/
├── shapes/ # Training data directory
│ ├── circle/ # Circle images (*.png)
│ ├── square/ # Square images (*.png)
│ ├── triangle/ # Triangle images (*.png)
│ └── star/ # Star images (*.png)
├── vec_train.npy # Training feature vectors
├── cat_train.npy # Training labels
├── vec_test.npy # Test feature vectors
├── cat_test.npy # Test labels
└── photo.jpg # Captured webcam image
# In Google Colab or local Jupyter environment
pip install opencv-python scikit-learn matplotlib pillow numpy
- Create a
shapes/
directory with subdirectories for each shape class - Place PNG images (16x16 recommended) in respective shape folders
- Or upload pre-processed
.npy
files directly
from google.colab import drive
drive.mount('/content/drive')
The system automatically:
- Loads images from the
shapes/
directory - Resizes images to 16x16 pixels
- Converts to grayscale
- Flattens to 256-dimensional feature vectors
# Select target shape
target_shape = 'square' # Options: 'circle', 'star', 'square', 'triangle'
# Train SVM classifier
from sklearn.svm import SVC
clf = SVC(kernel='linear')
clf.fit(vec_train, cat_train_bin)
# Capture image from webcam
filename = take_photo()
# Process and classify
result = classify_image(filename, target_shape)
- Input: RGB images of any size
- Preprocessing: Resize to 16x16, convert to grayscale, binary thresholding
- Features: 256-dimensional flattened pixel vectors
- Algorithm: Support Vector Machine (SVM) with linear kernel
- Binary Classification: One-vs-all approach for each shape
- Weight Quantization: Coefficients scaled and clipped to 4-bit integers (-8 to 7)
- Integer weights for hardware implementation
- Reduced precision maintains 98%+ accuracy
- Efficient dot product computation
target_shape = 'circle' # Detect circles
target_shape = 'star' # Detect stars
target_shape = 'square' # Detect squares
target_shape = 'triangle' # Detect triangles
# Linear kernel (recommended)
clf = SVC(kernel='linear')
# Alternative kernels
clf = SVC(kernel='rbf')
clf = SVC(random_state=0, tol=1e-5)
# Binary threshold (adjustable)
thres = 100
img_bin[img_gray > thres] = 255
img_bin[img_gray < thres] = 0
- FileNotFoundError: Ensure
shapes/
directory exists with proper structure - Webcam Access: Grant camera permissions in browser
- Import Errors: Install required packages via pip
- Low Accuracy: Check image quality and threshold settings
- Image Quality: Clear, well-lit shapes on contrasting backgrounds
This project demonstrates:
- Machine Learning Pipeline: Data loading, preprocessing, training, evaluation
- Computer Vision: Image processing and feature extraction
- Real-world Application: Webcam integration and live classification
- Optimization Techniques: Model quantization for deployment
- Performance Analysis: Accuracy metrics and validation