Godfireeee/AI-Grok-Haircut-Suggestion-and-Face-Structure-Identifier
AI Haircut Stylist & Face Structure Identifier
An incredibly accurate, AI-powered stylist that uses Computer Vision to mathematically deduce your face shape and Large Language Models (LLMs) to recommend the most flattering haircuts.
๐ Concept & Features
- Mathematical Face Analysis: Uses OpenCV to extract contour geometry, calculating proportions (Length-to-Width ratio, Jawline Taper) to accurately classify your face shape (Oval, Round, Square, Diamond, Oblong, or Heart).
- Computer Vision Pipeline: Includes dynamic Canny Edge detection, Haar Cascades for eye tracking, and contour smoothing.
- Gender Detection: Integrated a lightweight Caffe Deep Neural Network (DNN) model to classify gender from the detected facial crop.
- AI Stylist via Groq API: Uses Llama 3 (via Groq for lightning-fast inference) as an expert master stylist. It receives your exact face shape and gender to output personalized, trending haircut recommendations that balance your facial proportions.
- Interactive UI: A highly polished, premium custom frontend using Vanilla JS with an intuitive Drag-and-Drop image upload.
๐ Deep Dive: Image Processing Pipeline
The app uses classic computer vision techniques instead of a heavy neural network for face shape classification. Here is the step-by-step pipeline:
- Preprocessing: The uploaded image is converted to Grayscale and passed through a
cv2.GaussianBlurto reduce noise and unnecessary details. - Dynamic Edge Detection: We compute the median pixel intensity of the image and calculate custom upper and lower thresholds based on the median to feed into
cv2.Canny. This ensures the edges are dynamically captured regardless of lighting conditions. - Morphological Dilation: Small gaps in the detected edges are closed using
cv2.dilatewith a rectangular kernel, ensuring facial outlines run continuously. - Contour Extraction & Smoothing:
cv2.findContoursidentifies the largest, most prominent shape structure. It is then smoothed mathematically usingcv2.approxPolyDPto form a geometric bounding representation of the jawline and face. - Geometry & Eye Tracking: The absolute face length ($H$) and width ($W$) are extracted via bounding boxes. We utilize Haar Cascades (
haarcascade_eye.xml) to calculate specific forehead/upper-face width. - Shape Classification Logic:
- Jawline Taper Metric: We measure the area of the smoothed contour divided by the rectangular bounding box area ($W \times H$).
- Length-to-Width Ratio: A high ratio ($>1.35$) helps differentiate Oblong, Oval, and Diamond shapes, whereas broad ratios assist in finding Square, Round, and Heart shapes based on the Jawline Taper Extent.
๐ ๏ธ Technology Stack
- Backend: Python, FastAPI, Uvicorn
- AI & Vision: OpenCV, Groq API (Llama 3 70b-versatile)
- Frontend: HTML5, Vanilla CSS, JavaScript
- Deployment: Localhost (Waitress/Uvicorn & HTTP Server)
โ๏ธ Installation & Usage
1. Prerequisites
- Python 3.10+
- A Groq API Key for the AI recommendations.
2. Setup the Backend
Navigate to the backend directory and install the requirements:
cd backend
pip install -r requirements.txtCreate a .env file in the root backend directory (or use the provided .env.example) and add your Groq API Key:
GROQ_API_KEY=gsk_your_api_key_hereStart the FastAPI server:
python -m uvicorn main:app --reload --host 127.0.0.1 --port 80003. Setup the Frontend
Open a new terminal, navigate to the frontend directory, and start a local static server:
cd frontend
python -m http.server 55004. Try it out!
Visit http://localhost:5500 in your web browser. Drag and drop a clear photo of your face, and the AI will analyze it to provide stylist recommendations!
