A computer vision project that extracts precise time (HH:MM:SS) from images of analog clocks using two approaches: Classical Computer Vision and Deep Learning (CNN).
This project implements two distinct pipelines for reading time from analog clock images:
Uses traditional image processing techniques including:
- Image preprocessing (CLAHE, Thresholding)
- Hough Circle Transform for clock face detection
- Contour detection and Probabilistic Hough Transform for hand segmentation
- Vector geometry calculations (dot/cross product) for angle computation
Treats the problem as image classification:
- Clock images categorized into 144 classes (12 hours x 12 five-minute intervals)
- Custom CNN architectures trained on a labeled dataset
- Two model variants: 38x38 and 64x64 input resolutions
- Detects time from analog clock images
- Identifies hour, minute, and second hands
- Handles various clock styles and designs
- Provides visualization of detected hands
- Supports both CV and CNN-based approaches
- Python 3.8 or higher
- pip package manager
git clone https://github.com/your-username/Ip-Lab.git
cd Ip-Lab# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activatepip install -r requirements.txt- opencv-python
- numpy
- tensorflow (>= 2.0)
- matplotlib
-
Place your clock image in the project directory (e.g.,
images/clock.jpg) -
Run the CV module:
python -m cv.main- Or use it programmatically:
from cv.main import solve, main
import cv2
# Using the solve function
img = cv2.imread("path/to/clock.jpg")
result_img, time_str = solve(img)
print(f"Detected Time: {time_str}")
# Or using the main function with a path
main("path/to/clock.jpg")Output:
- Console: Prints detected time (e.g.,
Detected Time: 10:23:45) - Window: Displays the image with detected hands annotated
-
Prepare your dataset:
- Organize images in
data/train/anddata/test/directories - Each class should be in its own subfolder (e.g.,
data/train/10-00/,data/train/10-05/)
- Organize images in
-
Run training:
python -m cnn.train- Training configuration can be modified in
cnn/train.py:epochs: Number of training epochs (default: 50)batch_size: Batch size for training (default: 32)data_dir: Path to training data (default:"data/train")
Output:
- Training progress and accuracy metrics
- Plots of training/validation accuracy
- Test set evaluation results
-
Preprocessing: Image is resized and converted to HSV color space, then enhanced with CLAHE and binary thresholding
-
Clock Detection: Hough Circle Transform detects the clock face; falls back to contour detection if circles aren't found
-
Hand Segmentation: Probabilistic Hough Transform detects lines, which are grouped by angle and filtered by length/thickness to identify hour, minute, and second hands
-
Time Calculation: Angles relative to 12 o'clock position are computed using vector geometry and converted to time
-
Data Preparation: Images are loaded, resized (38x38 or 64x64), converted to grayscale, and augmented
-
Model Architecture: Two similar CNN architectures with:
- Convolutional layers (32, 64, 128 filters)
- Batch normalization and dropout for regularization
- Dense output layer with 144 classes (softmax activation)
-
Training: Models trained with Adam optimizer and categorical cross-entropy loss