This repository contains algorithms for estimating time on analog clocks using two approaches: brute force feature extraction and geometric approach algorithms. Additionally, it includes scripts for object detection within a dataset to isolate clock images and resize them accordingly.
- The output and error margins obtained by using
clock_time_detection.py
for the specified data set are available in a csv file in the folder namedpre-calculated
.
- Python and Library Installation
- Ensure Python 3.x and pip are installed.
- Install required Python libraries:
pip install torch torchvision opencv-python pillow pandas csv
- Install YOLOv5 model:
pip install 'git+https://github.com/ultralytics/yolov5.git'
- Download the dataset from this link.
- Place the
label.csv
file under the analog_clock_detection folder - Separate the first column of the
label.csv
file into filenames. 0.jpg, 1.jpg, 2.jpg and so on. The already edited version for the given dataset is located in the pre_calculated folder. The script reads directly from this folder thelabel.csv
file with the filename column added. You can edit the file path in the script for your ownlabel.csv
file.
Running the Object Detection and Resize Script
- Place your dataset in a folder named
images
within the directory where the script is located. - Example file structure:
./analog_clock_detection/images/0.jpg, 1.jpg, 2.jpg, ...
- Run the
detect_clock_resize.py
script. - It processes images in the
images
folder (PNG, JPG, JPEG).
Output
- Processed images are saved in the
imaged_cropped_fixed
folder.
Notes
- If no objects are detected, the original image is resized and saved.
This code contains Python code to predict the time shown on analog clocks in input images using computer vision techniques.
The project utilizes OpenCV (Open Source Computer Vision Library) and pandas for handling image processing and data operations respectively. The main steps involve:
- Extracting SIFT (Scale-Invariant Feature Transform) features from a dataset of cropped analog clock images.
- Matching these features with those extracted from input images to identify the best match.
- Using homography to calculate perspective transformation and predict the time shown on the analog clock in the input image.
- Python 3.x
- OpenCV (cv2)
- NumPy
- pandas
-
Setup:
- Ensure Python and necessary libraries (OpenCV, NumPy, pandas) are installed.
-
Dataset Preparation:
- Save the images you want to estimate the time in a folder named
matches
that you created under theanalog_clock_detection
folder. - Ensure CSV file (
label.csv
) listing each image file name along with the corresponding time displayed on the clock.
- Save the images you want to estimate the time in a folder named
-
Running the Code:
- Run the Python script (
feature_ext_prediction.py
). - Create
matches/
folder underanalog_clock_detection
. Place the images you want to match into thematches/
folder. - The script will process each image in the
matches/
directory, attempting to predict the time shown on the analog clock in each image.
- Run the Python script (
-
Output:
- The predicted time for each input image is displayed in the terminal.
feature_ext_prediction.py
: Main script that performs time prediction based on feature matching and homography.label.csv
: CSV file containing the mapping of image file names to their respective times.images_cropped_fixed/
: Directory containing cropped analog clock images used for training.matches/
: Directory containing input images for which the time needs to be predicted.
- Ensure sufficient feature matches are found (
good_matches
) for accurate time prediction. - Adjust parameters such as the distance threshold in feature matching (
0.5 * n.distance
) and RANSAC threshold in homography calculation (5.0
) based on specific image characteristics.
This code contains Python code to detect and predict the time shown on analog clocks in input images using computer vision techniques.
The project utilizes OpenCV (Open Source Computer Vision Library) for circle and line detection:
- Circle Detection: Uses Hough Circle Transform to detect the circular shape of the clock face.
- Line Detection: Applies Canny edge detection followed by Hough Line Transform to detect hour and minute hands.
The angles between these hands are calculated to predict the time shown on the clock.
- Python 3.x
- OpenCV (cv2)
- NumPy
- csv (for CSV operations)
-
Setup:
- Ensure Python and necessary libraries (OpenCV, NumPy) are installed.
-
Running the Code:
- Run the Python script (
clock_time_detection.py
). - The script will sequentially process each image (
0.jpg
to102.jpg
by default) and detect the time shown on the clock.
- Run the Python script (
-
Output:
- The detected time for each image is printed in the terminal.
- Results are appended to
predictions.csv
, which contains the path of each image and its corresponding detected time. - Compare the times in the label.csv file with the output you receive.
clock_time_detection.py
: Main script that performs time detection based on circle and line detection.predictions.csv
: CSV file containing the mapping of image paths to their detected times.images_cropped_fixed/
: Directory containing cropped analog clock images used for detection.
- Ensure sufficient and clear images of analog clocks are provided for accurate detection.
- Fine-tune parameters such as circle detection thresholds (
param1
,param2
) and line detection parameters (minLineLength
,maxLineGap
) based on image characteristics and quality.