This project aims to determine how much individuals resemble their parents, specifically focusing on visual similarities between my and my mom. It uses various Python libraries to process images, detect faces, and compute similarity scores. Overview
The code performs the following operations:
- Image Processing: Converts images to a consistent format and crops faces from the images using OpenCV.
- Feature Extraction: Utilizes a pre-trained Vision Transformer (ViT) model from the transformers library to extract image features.
- Similarity Calculation: Computes the cosine similarity between feature vectors of the processed images to quantify resemblance.
- Image Comparison: Analyzes sets of images, comparing each image of the child ('cris') with those of the mother ('mom'), to find the pair with the highest and lowest similarity scores.
- torch: For deep learning operations and handling tensors.
- transformers: To load pre-trained models for feature extraction
- opencv-python (cv2): For image processing tasks such as reading images and face detection.
- PIL (Pillow): For image manipulation like cropping and converting image formats.
- fastai: For additional image processing utilities.
- numpy: For numerical operations.
crop_image(im_for_crop): Detects and crops the face from an image.
get_similarity(file_name1, file_name2): Calculates the similarity score between two images.
show_images(rows, cols, imgs, labels, size=(256, 256)): Displays a grid of images with labels, useful for visualizing the results.
-
Face Detection: Each image is processed to detect and crop the face, ensuring that comparisons focus on facial features.
-
Feature Extraction: Faces are analyzed using the ViT model to extract feature vectors.
-
Similarity Scoring: Calculates the similarity between each pair of images (child and mother) to find the most and least similar pairs.
-
Result Visualization: Displays the images with the highest and lowest similarity scores, along with a grid of selected images for quick comparison.
This tool provides a fun and insightful way to explore familial resemblance through image analysis, demonstrating the power of machine learning and image processing in personal contexts.