Praxis II — Automated Screw Sorting with Computer Vision & AI ScrewSorter is a computer-vision pipeline that automatically identifies screw head types and estimates screw lengths from images using Google Gemini AI and OpenCV. It was built for the Praxis II engineering design course and provides real-time screw analysis through a camera interface.
- Features
- How It Works
- Supported Screw Head Types
- Prerequisites
- Getting Started
- Configuration
- Project Structure
- Limitations & Future Work
- License
- 🔩 Three-class screw head classification — Flat Head, Oval Head, Round Washer Head using Google Gemini AI
- 🤖 AI-powered classification — Zero-shot screw head identification with Google's Gemini vision model
- 📏 Screw length estimator — Uses OpenCV contour analysis and QR code calibration to measure physical length in millimetres
- 📷 Real-time camera interface — Live video feed with capture functionality
- 🔒 Secure API key management — Environment variables with .env file support
- 💾 Result visualization — Saves annotated images with measurements and classifications
Camera feed ──► QR detection (scale calibration) ──► Screw contour analysis
│ │
▼ ▼
Physical measurements (mm) Head shape classification
│ │
└───────────────────► Gemini AI analysis ──► Results
- QR Code Detection — Detects a QR code in the image to establish a scale reference (calibrated to known size)
- Screw Detection — Uses OpenCV thresholding and contour analysis to identify screw shapes in the image
- Length Measurement — Calculates screw shaft length using the QR scale and fitted bounding rectangles
- Head Classification — Sends the image to Google Gemini AI with a prompt to identify the screw head type
- Result Display — Shows measurements, classification, and saves an annotated image
| Class | Description |
|---|---|
| Flat_Head | Countersunk head that sits flush with the surface |
| Oval_Head | Partially countersunk with a rounded, decorative top |
| Round_Washer | Dome-shaped head with a built-in washer bearing surface |
| Package | Purpose |
|---|---|
google-generativeai |
Gemini AI API client |
opencv-python |
Image processing and computer vision |
Pillow |
Image I/O and manipulation |
numpy |
Numerical operations |
matplotlib |
Result visualization |
python-dotenv |
Environment variable loading |
Install all dependencies in a virtual environment:
pip install google-generativeai opencv-python Pillow numpy matplotlib python-dotenv
-
Clone or download the repository
-
Set up a Python virtual environment:
python -m venv .venv .venv\Scripts\activate # On Windows pip install google-generativeai opencv-python Pillow numpy matplotlib python-dotenv
-
Get a Google Gemini API key:
- Visit Google AI Studio
- Create a new API key
-
Configure the API key:
- Create a
.envfile in the project root - Add your API key:
GEMINI_API_KEY=your_api_key_here
- Create a
-
Run the application:
python main.py
-
Usage:
- Place a screw next to a QR code sheet for scale reference
- Press SPACEBAR to capture and analyze
- Press Q to quit
- Results are displayed in the console and saved as
result.png
The following constants at the top of main.py can be tuned:
| Constant | Default | Description |
|---|---|---|
CAMERA_INDEX |
1 |
Camera device index (0 for default webcam) |
QR_SIZE_MM |
21.9 |
Physical size of QR code in mm for calibration |
SAVE_PATH |
result.png |
Path to save the annotated result image |
MODEL_ID |
gemini-2.5-flash |
Gemini model to use for classification |
ScrewSorter/
├── main.py # Main application script
├── .env # Environment variables (API key)
├── .gitignore # Git ignore rules
├── README.md # This file
├── LICENSE # License information
├── demo.py # Demo/test script
└── Older-Backup Files/ # Archived files
- Camera calibration — The QR size (
QR_SIZE_MM) needs to be measured accurately for precise measurements - Lighting conditions — Performance may vary with different lighting; optimal results with even, bright lighting
- Screw orientation — Best results when screws are positioned with heads clearly visible
- API rate limits — Gemini API has usage limits; includes retry logic for quota exceeded errors
- Real-time performance — Processing time depends on image complexity and API response time
This project was developed by the Praxis II team:
- Team Members: Aditya Jain, Eva Deochakke, Jocelyn Li, Navneet Saxena, Peyton Smith
This project is licensed under the terms of the LICENSE file included in this repository.