from fastapi import FastAPI, HTTPException, UploadFile, File from fastapi.responses import JSONResponse, HTMLResponse, PlainTextResponse from pydantic import BaseModel from typing import List, Optional, Dict, Any import json from PIL import Image import io import time import uvicorn from transformers import AutoImageProcessor, AutoModelForObjectDetection, SwinModel, SwinConfig from transformers.pipelines import pipeline import torch import torch.nn as nn import torch.nn.functional as F import torchvision.transforms as v2 from huggingface_hub import PyTorchModelHubMixin import numpy as np import warnings import os import json from pathlib import Path # Suppress specific warnings for cleaner output warnings.filterwarnings("ignore", message=".*use_fast.*") warnings.filterwarnings("ignore", message=".*copying from a non-meta parameter.*") warnings.filterwarnings("ignore", message=".*slow image processor.*") warnings.filterwarnings("ignore", message=".*slow processor.*") app = FastAPI(title="HuggingFace Fashion Analyzer API", version="1.0.0") # Fashion Image Encoder class for yainage90 model class ImageEncoder(nn.Module, PyTorchModelHubMixin): def __init__(self, config=None): super(ImageEncoder, self).__init__() if config is None: # Create a default config if none provided config = SwinConfig() elif isinstance(config, dict): # Convert dict to SwinConfig if needed config = SwinConfig(**config) self.swin = SwinModel(config) self.embedding_layer = nn.Linear(config.hidden_size, 128) def forward(self, image_tensor): features = self.swin(image_tensor).pooler_output embeddings = self.embedding_layer(features) embeddings = F.normalize(embeddings, p=2, dim=1) return embeddings class HuggingFaceFashionAnalyzer: def __init__(self): """Initialize specialized fashion models from yainage90""" print("Loading yainage90 fashion models...") self.device = "cuda" if torch.cuda.is_available() else "cpu" print(f"Using device: {self.device}") # Set CPU optimization settings if self.device == "cpu": torch.set_num_threads(2) # Limit CPU threads to reduce load print("CPU optimization: Limited threads to 2 for better performance") # Initialize yainage90 fashion object detection model try: self.detection_ckpt = 'yainage90/fashion-object-detection' # Use fast processor to avoid warnings self.detection_processor = AutoImageProcessor.from_pretrained( self.detection_ckpt, use_fast=True ) # Load model with proper parameter assignment to avoid warnings with warnings.catch_warnings(): warnings.simplefilter("ignore") self.detection_model = AutoModelForObjectDetection.from_pretrained( self.detection_ckpt, torch_dtype=torch.float32 if self.device == "cpu" else torch.float16, low_cpu_mem_usage=True if self.device == "cpu" else False ).to(self.device) print("Fashion object detection model loaded successfully") except Exception as e: print(f"Error loading fashion detection model: {e}") self.detection_model = None self.detection_processor = None # Initialize yainage90 fashion feature extractor try: self.encoder_ckpt = "yainage90/fashion-image-feature-extractor" self.encoder_config = SwinConfig.from_pretrained(self.encoder_ckpt) # Use fast processor to avoid warnings self.encoder_image_processor = AutoImageProcessor.from_pretrained( self.encoder_ckpt, use_fast=True ) # Create the encoder with proper configuration - use from_pretrained directly with warnings.catch_warnings(): warnings.simplefilter("ignore") self.feature_encoder = ImageEncoder.from_pretrained(self.encoder_ckpt).to(self.device) # Set appropriate dtype after loading if self.device == "cpu": self.feature_encoder = self.feature_encoder.float() else: self.feature_encoder = self.feature_encoder.half() # Setup image transforms for feature extraction self.transform = v2.Compose([ v2.Resize((self.encoder_config.image_size, self.encoder_config.image_size)), v2.ToTensor(), v2.Normalize(mean=self.encoder_image_processor.image_mean, std=self.encoder_image_processor.image_std), ]) print("Fashion feature extractor model loaded successfully") except Exception as e: print(f"Error loading fashion feature extractor: {e}") self.feature_encoder = None self.transform = None # Initialize basic image captioning as fallback with CPU optimization try: # Configure model kwargs for CPU optimization model_kwargs = {} if self.device == "cpu": model_kwargs["low_cpu_mem_usage"] = True model_kwargs["torch_dtype"] = torch.float32 else: model_kwargs["torch_dtype"] = torch.float16 self.image_to_text = pipeline( "image-to-text", model="Salesforce/blip-image-captioning-base", device=0 if torch.cuda.is_available() else -1, model_kwargs=model_kwargs ) print("Basic image captioning model loaded successfully") except Exception as e: print(f"Error loading image captioning model: {e}") self.image_to_text = None # Fashion categories mapping self.fashion_categories = { 0: 'bag', 1: 'bottom', 2: 'dress', 3: 'hat', 4: 'shoes', 5: 'outer', 6: 'top' } # Set models to evaluation mode for inference optimization if self.detection_model: self.detection_model.eval() if self.feature_encoder: self.feature_encoder.eval() def process_image_from_bytes(self, image_bytes): """Process image bytes and return PIL Image""" image = Image.open(io.BytesIO(image_bytes)) # Convert to RGB if necessary if image.mode != 'RGB': image = image.convert('RGB') return image def analyze_clothing_from_bytes(self, image_bytes): """Advanced fashion analysis using yainage90 specialized models""" try: # Process image image = self.process_image_from_bytes(image_bytes) # Get fashion object detection results detection_results = self.detect_fashion_objects(image) # Extract fashion features fashion_features = self.extract_fashion_features(image) # Get basic image description as fallback basic_description = self.get_basic_description(image) # Create comprehensive fashion analysis analysis = self.create_advanced_fashion_analysis(detection_results, fashion_features, basic_description, image) return analysis except Exception as e: return f"Error analyzing image: {str(e)}" def analyze_clothing_structured_format(self, image_bytes): """Analyze clothing using the refined structured format""" try: # Process image image = self.process_image_from_bytes(image_bytes) # Get fashion object detection results detection_results = self.detect_fashion_objects(image) # Get basic image description as fallback basic_description = self.get_basic_description(image) # Create structured analysis in the exact requested format analysis = self.create_refined_structured_analysis(detection_results, basic_description) return analysis except Exception as e: return f"Error analyzing image: {str(e)}" def analyze_clothing_enhanced_prompt_format(self, image_bytes): """Analyze clothing using the enhanced prompt format optimized for feature extraction""" try: # Process image image = self.process_image_from_bytes(image_bytes) # Get fashion object detection results detection_results = self.detect_fashion_objects(image) # Get basic image description as fallback basic_description = self.get_basic_description(image) # Create enhanced analysis using the refined prompt structure analysis = self.create_enhanced_prompt_analysis(detection_results, basic_description) return analysis except Exception as e: return f"Error analyzing image: {str(e)}" def create_refined_structured_analysis(self, detection_results, basic_description): """Create analysis in the exact refined format requested""" # Process detection results detected_items = detection_results.get('detected_items', []) # Categorize detected items upper_items = [] lower_items = [] footwear_items = [] for item in detected_items: category = item['category'].lower() if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']: upper_items.append(item) elif category in ['bottom', 'pants', 'jeans', 'skirt']: lower_items.append(item) elif category in ['shoes']: footwear_items.append(item) analysis_parts = [] # UPPER GARMENT analysis_parts.append("UPPER GARMENT:") if upper_items or 'dress' in basic_description.lower() or any(word in basic_description.lower() for word in ['shirt', 'blouse', 'top', 'jacket']): if upper_items: primary_upper = upper_items[0] garment_type = primary_upper['category'].title() else: garment_type = self.extract_garment_type(basic_description) analysis_parts.append(f"Type: {garment_type}") analysis_parts.append(f"Color: {self.extract_colors(basic_description)}") analysis_parts.append(f"Material: {self.extract_material(basic_description)}") analysis_parts.append(f"Features: {self.extract_comprehensive_features(basic_description, garment_type)}") else: analysis_parts.append("Type: Not clearly visible in image") analysis_parts.append("Color: Unable to determine") analysis_parts.append("Material: Unable to determine") analysis_parts.append("Features: Unable to determine") analysis_parts.append("") # LOWER GARMENT analysis_parts.append("LOWER GARMENT:") if 'dress' in basic_description.lower() and not lower_items: analysis_parts.append("[Not applicable - dress serves as complete outfit]") elif lower_items or any(word in basic_description.lower() for word in ['pants', 'jeans', 'skirt', 'shorts']): if lower_items: primary_lower = lower_items[0] garment_type = primary_lower['category'].title() else: # Infer from description if 'jeans' in basic_description.lower(): garment_type = "Jeans" elif 'pants' in basic_description.lower(): garment_type = "Pants" elif 'skirt' in basic_description.lower(): garment_type = "Skirt" else: garment_type = "Lower garment" analysis_parts.append(f"Type: {garment_type}") analysis_parts.append(f"Color: {self.extract_colors(basic_description)}") analysis_parts.append(f"Material: {self.extract_material(basic_description)}") analysis_parts.append(f"Features: {self.extract_comprehensive_features(basic_description, garment_type)}") else: analysis_parts.append("Type: Not clearly visible in image") analysis_parts.append("Color: Unable to determine") analysis_parts.append("Material: Unable to determine") analysis_parts.append("Features: Unable to determine") analysis_parts.append("") # FOOTWEAR analysis_parts.append("FOOTWEAR:") if footwear_items or any(word in basic_description.lower() for word in ['shoes', 'boots', 'sneakers', 'heels']): if footwear_items: primary_footwear = footwear_items[0] footwear_type = primary_footwear['category'].title() else: # Infer from description if 'sneakers' in basic_description.lower(): footwear_type = "Sneakers" elif 'boots' in basic_description.lower(): footwear_type = "Boots" elif 'heels' in basic_description.lower(): footwear_type = "Heels" else: footwear_type = "Shoes" analysis_parts.append(f"Type: {footwear_type}") analysis_parts.append(f"Color: {self.extract_colors(basic_description)}") analysis_parts.append(f"Material: {self.extract_material(basic_description)}") analysis_parts.append(f"Features: {self.extract_comprehensive_features(basic_description, footwear_type)}") else: analysis_parts.append("Type: Not clearly visible in image") analysis_parts.append("Color: Unable to determine") analysis_parts.append("Material: Unable to determine") analysis_parts.append("Features: Unable to determine") analysis_parts.append("") # OUTFIT SUMMARY analysis_parts.append("OUTFIT SUMMARY:") outfit_summary = self.create_comprehensive_outfit_summary(detected_items, basic_description) analysis_parts.append(outfit_summary) return "\n".join(analysis_parts) def create_enhanced_prompt_analysis(self, detection_results, basic_description): """Create analysis using the enhanced prompt format optimized for feature extraction""" # Process detection results detected_items = detection_results.get('detected_items', []) # Categorize detected items upper_items = [] lower_items = [] footwear_items = [] for item in detected_items: category = item['category'].lower() if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']: upper_items.append(item) elif category in ['bottom', 'pants', 'jeans', 'skirt']: lower_items.append(item) elif category in ['shoes']: footwear_items.append(item) analysis_parts = [] # UPPER GARMENT analysis_parts.append("**UPPER GARMENT**") analysis_parts.append("") if upper_items or 'dress' in basic_description.lower() or any(word in basic_description.lower() for word in ['shirt', 'blouse', 'top', 'jacket']): if upper_items: primary_upper = upper_items[0] garment_type = primary_upper['category'].title() else: garment_type = self.extract_garment_type(basic_description) analysis_parts.append(f"**Type**: {garment_type}") analysis_parts.append(f"**Color**: {self.extract_enhanced_colors(basic_description)}") analysis_parts.append(f"**Material**: {self.extract_enhanced_material(basic_description, garment_type)}") analysis_parts.append(f"**Features**: {self.extract_enhanced_features(basic_description, garment_type)}") else: analysis_parts.append("**Type**: Not clearly visible in image") analysis_parts.append("**Color**: Unable to determine") analysis_parts.append("**Material**: Unable to determine") analysis_parts.append("**Features**: Unable to determine") analysis_parts.append("") analysis_parts.append("---") analysis_parts.append("") # LOWER GARMENT analysis_parts.append("**LOWER GARMENT**") analysis_parts.append("") if 'dress' in basic_description.lower() and not lower_items: analysis_parts.append("**Type**: Not applicable - dress serves as complete outfit") analysis_parts.append("**Color**: N/A") analysis_parts.append("**Material**: N/A") analysis_parts.append("**Features**: N/A") elif lower_items or any(word in basic_description.lower() for word in ['pants', 'jeans', 'skirt', 'shorts']): if lower_items: primary_lower = lower_items[0] garment_type = primary_lower['category'].title() else: # Infer from description if 'jeans' in basic_description.lower(): garment_type = "Jeans" elif 'pants' in basic_description.lower(): garment_type = "Pants" elif 'skirt' in basic_description.lower(): garment_type = "Skirt" else: garment_type = "Lower garment" analysis_parts.append(f"**Type**: {garment_type}") analysis_parts.append(f"**Color**: {self.extract_enhanced_colors(basic_description)}") analysis_parts.append(f"**Material**: {self.extract_enhanced_material(basic_description, garment_type)}") analysis_parts.append(f"**Features**: {self.extract_enhanced_features(basic_description, garment_type)}") else: analysis_parts.append("**Type**: Not clearly visible in image") analysis_parts.append("**Color**: Unable to determine") analysis_parts.append("**Material**: Unable to determine") analysis_parts.append("**Features**: Unable to determine") analysis_parts.append("") analysis_parts.append("---") analysis_parts.append("") # FOOTWEAR analysis_parts.append("**FOOTWEAR**") analysis_parts.append("") if footwear_items or any(word in basic_description.lower() for word in ['shoes', 'boots', 'sneakers', 'heels']): if footwear_items: primary_footwear = footwear_items[0] footwear_type = primary_footwear['category'].title() else: # Infer from description if 'sneakers' in basic_description.lower(): footwear_type = "Sneakers" elif 'boots' in basic_description.lower(): footwear_type = "Boots" elif 'heels' in basic_description.lower(): footwear_type = "Heels" else: footwear_type = "Shoes" analysis_parts.append(f"**Type**: {footwear_type}") analysis_parts.append(f"**Color**: {self.extract_enhanced_colors(basic_description)}") analysis_parts.append(f"**Material**: {self.extract_enhanced_material(basic_description, footwear_type)}") analysis_parts.append(f"**Features**: {self.extract_enhanced_features(basic_description, footwear_type)}") else: analysis_parts.append("**Type**: Not clearly visible in image") analysis_parts.append("**Color**: Unable to determine") analysis_parts.append("**Material**: Unable to determine") analysis_parts.append("**Features**: Unable to determine") analysis_parts.append("") analysis_parts.append("---") analysis_parts.append("") # OUTFIT SUMMARY analysis_parts.append("**OUTFIT SUMMARY**") analysis_parts.append("") outfit_summary = self.create_enhanced_outfit_summary(detected_items, basic_description) analysis_parts.append(outfit_summary) return "\n".join(analysis_parts) def extract_comprehensive_features(self, description, garment_type): """Extract comprehensive features based on garment type""" desc_lower = description.lower() garment_lower = garment_type.lower() features = [] # Pattern and design features pattern = self.extract_pattern(description) if pattern and pattern != "Solid or minimal pattern": features.append(pattern) # Garment-specific features if any(word in garment_lower for word in ['shirt', 'blouse', 'top']): # Neckline neckline = self.extract_neckline(description) if neckline != "Neckline present but style not specified": features.append(neckline) # Sleeves sleeves = self.extract_sleeves(description) if sleeves != "Sleeve style not specified in description": features.append(sleeves) # Collar if 'collar' in desc_lower: features.append("Collared design") # Buttons if 'button' in desc_lower: features.append("Button closure") elif 'dress' in garment_lower: # Length if 'midi' in desc_lower: features.append("Midi length") elif 'maxi' in desc_lower: features.append("Maxi length") elif 'mini' in desc_lower: features.append("Mini length") # Sleeves sleeves = self.extract_sleeves(description) if sleeves != "Sleeve style not specified in description": features.append(sleeves) # Neckline neckline = self.extract_neckline(description) if neckline != "Neckline present but style not specified": features.append(neckline) # Fit fit = self.extract_fit(description) if fit != "Fit style not specified in description": features.append(fit) elif any(word in garment_lower for word in ['pants', 'jeans', 'skirt']): # Fit fit = self.extract_fit(description) if fit != "Fit style not specified in description": features.append(fit) # Pockets if 'pocket' in desc_lower: features.append("Pockets") # Belt if 'belt' in desc_lower: features.append("Belt") # Length for skirts if 'skirt' in garment_lower: if 'mini' in desc_lower: features.append("Mini length") elif 'midi' in desc_lower: features.append("Midi length") elif 'maxi' in desc_lower: features.append("Maxi length") elif any(word in garment_lower for word in ['shoes', 'sneakers', 'boots', 'heels']): # Closure if 'lace' in desc_lower: features.append("Lace-up closure") elif 'buckle' in desc_lower: features.append("Buckle closure") elif 'slip' in desc_lower: features.append("Slip-on style") # Heel if 'heel' in desc_lower: features.append("Heeled") elif 'flat' in desc_lower: features.append("Flat sole") # Style details if 'stud' in desc_lower: features.append("Studded details") # General features if 'zip' in desc_lower: features.append("Zipper details") return ', '.join(features) if features else "Standard design features" def create_comprehensive_outfit_summary(self, detected_items, basic_description): """Create a comprehensive outfit summary paragraph""" # Extract key elements garment_types = [item['category'] for item in detected_items if item['confidence'] > 0.5] colors = self.extract_colors(basic_description) style = self.extract_style(basic_description) pattern = self.extract_pattern(basic_description) summary_sentences = [] # Opening statement about overall aesthetic if garment_types: if 'dress' in garment_types: summary_sentences.append("This outfit centers around an elegant dress that serves as a complete styling solution, demonstrating the power of a well-chosen single piece.") elif len(garment_types) > 1: summary_sentences.append("This outfit showcases thoughtful coordination between multiple garment pieces, creating a cohesive and intentional look.") else: summary_sentences.append("This outfit highlights a key fashion piece with strong individual character and styling potential.") else: summary_sentences.append("This outfit demonstrates contemporary fashion sensibility with carefully considered design elements.") # Color and pattern analysis if 'neutral' in colors.lower(): summary_sentences.append("The neutral color palette provides exceptional versatility and sophisticated appeal, making it suitable for various occasions and easy to accessorize.") elif 'warm' in colors.lower(): summary_sentences.append("The warm color tones create an approachable and energetic aesthetic that conveys confidence and positivity.") elif 'cool' in colors.lower(): summary_sentences.append("The cool color palette conveys professionalism and calming elegance, perfect for both casual and semi-formal settings.") if 'floral' in pattern.lower(): summary_sentences.append("The floral elements add feminine charm and seasonal freshness, bringing natural beauty and romantic appeal to the overall ensemble.") elif 'solid' in pattern.lower(): summary_sentences.append("The solid construction maximizes styling flexibility and serves as an excellent foundation for accessory experimentation.") elif 'striped' in pattern.lower(): summary_sentences.append("The striped pattern adds visual interest and classic appeal while maintaining timeless sophistication.") # Style and occasion assessment if 'casual' in style.lower(): summary_sentences.append("The casual styling makes this outfit perfect for everyday wear, weekend activities, and relaxed social occasions while maintaining a polished appearance.") elif 'formal' in style.lower(): summary_sentences.append("The formal elements ensure this outfit meets professional standards and elegant occasion requirements with confidence and grace.") else: summary_sentences.append("The versatile design allows this outfit to transition seamlessly between different settings, from casual daytime events to more polished evening occasions.") # Concluding statement about fashion-forward thinking summary_sentences.append("The pieces work harmoniously together, reflecting contemporary fashion awareness and demonstrating how thoughtful styling choices can create an effortlessly chic and memorable appearance.") return " ".join(summary_sentences) def parse_structured_analysis(self, detection_results, basic_description): """Parse detection results and description into structured JSON format""" # Process detection results detected_items = detection_results.get('detected_items', []) # Categorize detected items upper_items = [] lower_items = [] footwear_items = [] for item in detected_items: category = item['category'].lower() if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']: upper_items.append(item) elif category in ['bottom', 'pants', 'jeans', 'skirt']: lower_items.append(item) elif category in ['shoes']: footwear_items.append(item) # Extract upper garment details if upper_items or 'dress' in basic_description.lower(): if upper_items: garment_type = upper_items[0]['category'].title() else: garment_type = self.extract_garment_type(basic_description) upper_garment = { "type": garment_type, "color": self.extract_colors(basic_description), "material": self.extract_material(basic_description), "features": self.extract_comprehensive_features(basic_description, garment_type) } else: upper_garment = { "type": "Not clearly visible", "color": "Unable to determine", "material": "Unable to determine", "features": "Unable to determine" } # Extract lower garment details if 'dress' in basic_description.lower() and not lower_items: lower_garment = { "type": "Not applicable - dress serves as complete outfit", "color": "N/A", "material": "N/A", "features": "N/A" } elif lower_items: garment_type = lower_items[0]['category'].title() lower_garment = { "type": garment_type, "color": self.extract_colors(basic_description), "material": self.extract_material(basic_description), "features": self.extract_comprehensive_features(basic_description, garment_type) } else: lower_garment = { "type": "Not clearly visible", "color": "Unable to determine", "material": "Unable to determine", "features": "Unable to determine" } # Extract footwear details if footwear_items: footwear_type = footwear_items[0]['category'].title() footwear = { "type": footwear_type, "color": self.extract_colors(basic_description), "material": self.extract_material(basic_description), "features": self.extract_comprehensive_features(basic_description, footwear_type) } else: footwear = { "type": "Not clearly visible", "color": "Unable to determine", "material": "Unable to determine", "features": "Unable to determine" } # Create outfit summary outfit_summary_text = self.create_comprehensive_outfit_summary(detected_items, basic_description) # Parse outfit summary into components outfit_summary = { "aesthetic": self.extract_style(basic_description), "style_notes": self.extract_pattern(basic_description), "occasion_suitability": "Versatile for multiple occasions", "color_harmony": self.extract_colors(basic_description), "overall_assessment": outfit_summary_text } # Calculate confidence score confidence_scores = [item['confidence'] for item in detected_items if item['confidence'] > 0.3] confidence_score = sum(confidence_scores) / len(confidence_scores) if confidence_scores else 0.5 # Create structured response return StructuredAnalysisResponse( upper_garment=GarmentDetails(**upper_garment), lower_garment=GarmentDetails(**lower_garment), footwear=GarmentDetails(**footwear), outfit_summary=OutfitSummary(**outfit_summary), confidence_score=round(confidence_score, 3), detected_items=detected_items ) def detect_fashion_objects(self, image): """Detect fashion objects using yainage90 fashion detection model""" if self.detection_model is None or self.detection_processor is None: return {"error": "Fashion detection model not available"} try: # Use inference mode for better performance with torch.inference_mode(): inputs = self.detection_processor(images=[image], return_tensors="pt") # Move inputs to device efficiently inputs = {k: v.to(self.device) for k, v in inputs.items()} outputs = self.detection_model(**inputs) target_sizes = torch.tensor([[image.size[1], image.size[0]]]) results = self.detection_processor.post_process_object_detection( outputs, threshold=0.4, target_sizes=target_sizes )[0] detected_items = [] for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): score = score.item() label = label.item() box = [i.item() for i in box] category = self.detection_model.config.id2label[label] detected_items.append({ 'category': category, 'confidence': round(score, 3), 'bbox': box }) return {"detected_items": detected_items} except Exception as e: return {"error": f"Detection failed: {str(e)}"} def extract_fashion_features(self, image): """Extract fashion features using yainage90 feature extractor""" if self.feature_encoder is None or self.transform is None: return {"error": "Feature extractor not available"} try: # Transform image for feature extraction image_tensor = self.transform(image) # Use inference mode for better performance with torch.inference_mode(): embedding = self.feature_encoder(image_tensor.unsqueeze(0).to(self.device)) return { "feature_vector": embedding.cpu().numpy().tolist(), "feature_dimension": embedding.shape[1] } except Exception as e: return {"error": f"Feature extraction failed: {str(e)}"} def get_basic_description(self, image): """Get basic image description as fallback""" if self.image_to_text is None: return "Image analysis not available" try: result = self.image_to_text(image) # Convert result to list if it's a generator/iterator if result is not None and hasattr(result, '__iter__') and not isinstance(result, (str, bytes)): result_list = list(result) if result_list and len(result_list) > 0: first_result = result_list[0] if isinstance(first_result, dict): return first_result.get('generated_text', 'Unable to describe image') return "Unable to describe image" except Exception as e: return f"Description failed: {str(e)}" def create_advanced_fashion_analysis(self, detection_results, fashion_features, basic_description, image): """Create comprehensive fashion analysis using yainage90 model results""" # Process detection results detected_items = detection_results.get('detected_items', []) detection_summary = self.summarize_detections(detected_items) # Create structured clothing analysis structured_analysis = self.create_structured_clothing_analysis(detected_items, basic_description) # Create comprehensive analysis analysis_template = f"""šŸŽ½ ADVANCED FASHION ANALYSIS REPORT šŸ” AI-POWERED OBJECT DETECTION: {detection_summary} šŸ“‹ STRUCTURED CLOTHING ANALYSIS: {structured_analysis} šŸ“‹ DETAILED GARMENT ANALYSIS: {self.create_detailed_garment_analysis(detected_items, basic_description)} šŸŽØ STYLE & DESIGN ASSESSMENT: {self.create_style_assessment(detected_items, basic_description)} šŸ’” PROFESSIONAL STYLING RECOMMENDATIONS: {self.generate_styling_recommendations(detected_items)} šŸ”„ WARDROBE INTEGRATION ADVICE: {self.generate_wardrobe_advice(detected_items)} šŸ“Š FASHION SUMMARY: {self.create_fashion_summary(detected_items, basic_description)} šŸ¤– TECHNICAL DETAILS: • Feature Vector Dimension: {fashion_features.get('feature_dimension', 'N/A')} • Detection Confidence: {self.get_average_confidence(detected_items)} • Analysis Method: yainage90 Fashion AI Models""" return analysis_template def create_structured_clothing_analysis(self, detected_items, basic_description): """Create structured clothing analysis in the requested format""" # Categorize detected items upper_items = [] lower_items = [] footwear_items = [] for item in detected_items: category = item['category'].lower() if category in ['top', 'shirt', 'blouse', 'outer', 'jacket', 'blazer', 'dress']: upper_items.append(item) elif category in ['bottom', 'pants', 'jeans', 'skirt']: lower_items.append(item) elif category in ['shoes']: footwear_items.append(item) analysis_parts = [] # Upper Garment Analysis if upper_items or 'dress' in basic_description.lower() or any(word in basic_description.lower() for word in ['shirt', 'blouse', 'top', 'jacket']): analysis_parts.append("UPPER GARMENT:") if upper_items: primary_upper = upper_items[0] analysis_parts.append(f"Type: {primary_upper['category'].title()}") else: analysis_parts.append(f"Type: {self.extract_garment_type(basic_description)}") analysis_parts.append(f"Color: {self.extract_colors(basic_description)}") analysis_parts.append(f"Material: {self.extract_material(basic_description)}") analysis_parts.append(f"Features: {self.extract_features(basic_description)}") analysis_parts.append("") # Lower Garment Analysis if lower_items or any(word in basic_description.lower() for word in ['pants', 'jeans', 'skirt', 'shorts']): analysis_parts.append("LOWER GARMENT:") if lower_items: primary_lower = lower_items[0] analysis_parts.append(f"Type: {primary_lower['category'].title()}") else: # Infer from description if 'jeans' in basic_description.lower(): analysis_parts.append("Type: Jeans") elif 'pants' in basic_description.lower(): analysis_parts.append("Type: Pants") elif 'skirt' in basic_description.lower(): analysis_parts.append("Type: Skirt") else: analysis_parts.append("Type: Lower garment") analysis_parts.append(f"Color: {self.extract_colors(basic_description)}") analysis_parts.append(f"Material: {self.extract_material(basic_description)}") analysis_parts.append(f"Features: {self.extract_features(basic_description)}") analysis_parts.append("") # Footwear Analysis if footwear_items or any(word in basic_description.lower() for word in ['shoes', 'boots', 'sneakers', 'heels']): analysis_parts.append("FOOTWEAR:") if footwear_items: primary_footwear = footwear_items[0] analysis_parts.append(f"Type: {primary_footwear['category'].title()}") else: # Infer from description if 'sneakers' in basic_description.lower(): analysis_parts.append("Type: Sneakers") elif 'boots' in basic_description.lower(): analysis_parts.append("Type: Boots") elif 'heels' in basic_description.lower(): analysis_parts.append("Type: Heels") else: analysis_parts.append("Type: Shoes") analysis_parts.append(f"Color: {self.extract_colors(basic_description)}") analysis_parts.append(f"Material: {self.extract_material(basic_description)}") analysis_parts.append(f"Features: {self.extract_features(basic_description)}") analysis_parts.append("") # Outfit Summary analysis_parts.append("OUTFIT SUMMARY:") outfit_summary = self.create_outfit_summary(detected_items, basic_description) analysis_parts.append(outfit_summary) return "\n".join(analysis_parts) def create_outfit_summary(self, detected_items, basic_description): """Create a comprehensive outfit summary paragraph""" # Extract key elements garment_types = [item['category'] for item in detected_items if item['confidence'] > 0.5] colors = self.extract_colors(basic_description) style = self.extract_style(basic_description) pattern = self.extract_pattern(basic_description) summary_parts = [] # Start with overall style assessment if garment_types: if 'dress' in garment_types: summary_parts.append("This outfit centers around an elegant dress that serves as a complete styling solution.") elif 'top' in garment_types and 'bottom' in garment_types: summary_parts.append("This outfit features a well-coordinated combination of separates that create a cohesive look.") elif len(garment_types) > 1: summary_parts.append("This outfit demonstrates thoughtful layering and coordination between multiple garment pieces.") else: summary_parts.append("This outfit showcases a key fashion piece with strong individual character.") else: summary_parts.append("This outfit demonstrates contemporary fashion sensibility with versatile appeal.") # Add color and pattern insights if 'neutral' in colors.lower(): summary_parts.append("The neutral color palette provides excellent versatility and sophisticated appeal.") elif 'warm' in colors.lower(): summary_parts.append("The warm color tones create an approachable and energetic aesthetic.") elif 'cool' in colors.lower(): summary_parts.append("The cool color palette conveys professionalism and calming elegance.") if 'floral' in pattern.lower(): summary_parts.append("The floral elements add feminine charm and seasonal freshness to the overall look.") elif 'solid' in pattern.lower(): summary_parts.append("The solid construction maximizes styling flexibility and accessory compatibility.") # Add style context if 'casual' in style.lower(): summary_parts.append("The casual styling makes this outfit perfect for everyday wear and relaxed social occasions.") elif 'formal' in style.lower(): summary_parts.append("The formal elements ensure this outfit is suitable for professional and elegant occasions.") elif 'versatile' in style.lower(): summary_parts.append("The versatile design allows this outfit to transition seamlessly between different settings and occasions.") # Conclude with overall assessment summary_parts.append("The pieces complement each other harmoniously, creating a polished and intentional appearance that reflects contemporary fashion awareness.") return " ".join(summary_parts) def summarize_detections(self, detected_items): """Summarize detected fashion items""" if not detected_items: return "No specific fashion items detected. Using general image analysis." summary_lines = [] for item in detected_items: category = item['category'].upper() confidence = item['confidence'] summary_lines.append(f"• {category}: {confidence*100:.1f}% confidence") return "\n".join(summary_lines) def create_detailed_garment_analysis(self, detected_items, basic_description): """Create detailed analysis of detected garments""" if not detected_items: return f"Based on image analysis: {basic_description}\n" + self.extract_comprehensive_details(basic_description) analysis_parts = [] for item in detected_items: category = item['category'] confidence = item['confidence'] if confidence > 0.5: # High confidence detections analysis_parts.append(f"**{category.upper()}** (Confidence: {confidence*100:.1f}%)") analysis_parts.append(self.get_category_specific_analysis(category, basic_description)) analysis_parts.append("") # Add spacing return "\n".join(analysis_parts) def get_category_specific_analysis(self, category, description): """Get specific analysis based on detected category""" category_analyses = { 'top': self.analyze_top_garment(description), 'bottom': self.analyze_bottom_garment(description), 'dress': self.analyze_dress_garment(description), 'outer': self.analyze_outer_garment(description), 'shoes': self.analyze_shoes(description), 'bag': self.analyze_bag(description), 'hat': self.analyze_hat(description) } return category_analyses.get(category, f"General {category} analysis based on visual features.") def analyze_top_garment(self, description): """Analyze top garments (shirts, blouses, t-shirts)""" analysis = [] desc_lower = description.lower() # Determine specific type if 't-shirt' in desc_lower or 'tee' in desc_lower: analysis.append("• Type: T-shirt - casual wardrobe staple") analysis.append("• Styling: Perfect for layering, casual wear, and relaxed occasions") elif 'shirt' in desc_lower or 'blouse' in desc_lower: analysis.append("• Type: Shirt/Blouse - versatile professional piece") analysis.append("• Styling: Suitable for business casual, can be dressed up or down") else: analysis.append("• Type: Top garment - versatile upper body wear") # Color analysis colors = self.extract_colors(description) analysis.append(f"• Color Profile: {colors}") # Fit and style fit = self.extract_fit(description) analysis.append(f"• Fit & Silhouette: {fit}") # Styling suggestions analysis.append("• Styling Tips: Tuck into high-waisted bottoms, layer under jackets, or wear loose for casual looks") return "\n".join(analysis) def analyze_bottom_garment(self, description): """Analyze bottom garments (pants, jeans, skirts)""" analysis = [] desc_lower = description.lower() if 'jeans' in desc_lower: analysis.append("• Type: Jeans - denim casual essential") analysis.append("• Material: Denim fabric, durable and versatile") elif 'pants' in desc_lower or 'trousers' in desc_lower: analysis.append("• Type: Pants/Trousers - structured bottom wear") analysis.append("• Occasion: Professional, semi-formal, or smart casual") elif 'skirt' in desc_lower: analysis.append("• Type: Skirt - feminine silhouette piece") analysis.append("• Style: Adds elegance and movement to outfits") else: analysis.append("• Type: Bottom garment - lower body wear") fit = self.extract_fit(description) analysis.append(f"• Fit & Cut: {fit}") analysis.append("• Styling: Pair with fitted tops for balanced proportions") return "\n".join(analysis) def analyze_dress_garment(self, description): """Analyze dress garments""" analysis = [] analysis.append("• Type: Dress - complete outfit piece") analysis.append("• Advantage: Single garment solution for put-together looks") pattern = self.extract_pattern(description) analysis.append(f"• Pattern & Design: {pattern}") style = self.extract_style(description) analysis.append(f"• Style Category: {style}") occasion = self.extract_occasion(description) analysis.append(f"• Best For: {occasion}") analysis.append("• Styling: Add layers, accessories, or shoes to change the look's formality") return "\n".join(analysis) def analyze_outer_garment(self, description): """Analyze outer garments (jackets, coats, blazers)""" analysis = [] desc_lower = description.lower() if 'blazer' in desc_lower: analysis.append("• Type: Blazer - structured professional outerwear") analysis.append("• Function: Adds polish and authority to outfits") elif 'jacket' in desc_lower: analysis.append("• Type: Jacket - versatile layering piece") analysis.append("• Function: Provides warmth and style enhancement") elif 'coat' in desc_lower: analysis.append("• Type: Coat - substantial outerwear") analysis.append("• Function: Weather protection with style") else: analysis.append("• Type: Outer garment - layering piece") analysis.append("• Styling: Layer over various outfits to change formality level") analysis.append("• Versatility: Essential for transitional weather and professional looks") return "\n".join(analysis) def analyze_shoes(self, description): """Analyze footwear""" analysis = [] analysis.append("• Type: Footwear - outfit foundation piece") analysis.append("• Impact: Significantly influences overall look formality and style") analysis.append("• Styling: Choose based on occasion, comfort needs, and outfit balance") analysis.append("• Tip: Quality footwear elevates any outfit") return "\n".join(analysis) def analyze_bag(self, description): """Analyze bags and accessories""" analysis = [] analysis.append("• Type: Bag/Accessory - functional style element") analysis.append("• Purpose: Combines practicality with fashion statement") analysis.append("• Styling: Should complement outfit colors and formality level") analysis.append("• Tip: Quality bags are investment pieces that enhance multiple outfits") return "\n".join(analysis) def analyze_hat(self, description): """Analyze hats and headwear""" analysis = [] analysis.append("• Type: Hat/Headwear - statement accessory") analysis.append("• Function: Adds personality and can change outfit's entire vibe") analysis.append("• Styling: Consider face shape, hair style, and occasion") analysis.append("• Tip: Hats can make simple outfits more interesting and unique") return "\n".join(analysis) def create_style_assessment(self, detected_items, basic_description): """Create style assessment based on detected items""" if not detected_items: return self.extract_style(basic_description) style_elements = [] categories = [item['category'] for item in detected_items if item['confidence'] > 0.5] if 'dress' in categories: style_elements.append("• Feminine and elegant aesthetic") style_elements.append("• Single-piece sophistication") if 'blazer' in categories or 'outer' in categories: style_elements.append("• Professional and structured elements") style_elements.append("• Layered sophistication") if 'jeans' in basic_description.lower() or 'bottom' in categories: style_elements.append("• Casual and approachable foundation") style_elements.append("• Versatile everyday appeal") if not style_elements: style_elements.append("• Contemporary fashion sensibility") style_elements.append("• Versatile styling potential") return "\n".join(style_elements) def generate_styling_recommendations(self, detected_items): """Generate styling recommendations based on detected items""" if not detected_items: return "• Focus on fit, color coordination, and occasion appropriateness\n• Layer pieces to create visual interest\n• Accessorize to personalize the look" recommendations = [] categories = [item['category'] for item in detected_items if item['confidence'] > 0.5] if 'top' in categories: recommendations.append("• Tuck into high-waisted bottoms for a polished silhouette") recommendations.append("• Layer under jackets or cardigans for depth") if 'bottom' in categories: recommendations.append("• Pair with fitted tops to balance proportions") recommendations.append("• Choose shoes that complement the formality level") if 'dress' in categories: recommendations.append("• Add a belt to define the waist") recommendations.append("• Layer with jackets or cardigans for versatility") recommendations.append("• Change accessories to shift from day to night") if 'outer' in categories: recommendations.append("• Use as a statement piece over simple outfits") recommendations.append("• Ensure proper fit in shoulders and sleeves") if not recommendations: recommendations.append("• Focus on color harmony and proportion balance") recommendations.append("• Consider the occasion and dress code") return "\n".join(recommendations) def generate_wardrobe_advice(self, detected_items): """Generate wardrobe integration advice""" if not detected_items: return "• Invest in versatile, quality basics\n• Build around neutral colors\n• Choose pieces that work for multiple occasions" advice = [] categories = [item['category'] for item in detected_items if item['confidence'] > 0.5] if 'top' in categories: advice.append("• Essential wardrobe building block") advice.append("• Pairs well with multiple bottom styles") if 'bottom' in categories: advice.append("• Foundation piece for outfit construction") advice.append("• Invest in quality fit and classic styles") if 'dress' in categories: advice.append("• Versatile one-piece solution") advice.append("• Can be styled for multiple occasions") if 'outer' in categories: advice.append("• Transforms and elevates basic outfits") advice.append("• Essential for professional wardrobes") advice.append("• Consider cost-per-wear when building wardrobe") advice.append("• Focus on pieces that reflect your lifestyle needs") return "\n".join(advice) def create_fashion_summary(self, detected_items, basic_description): """Create comprehensive fashion summary""" if not detected_items: return f"This garment demonstrates contemporary design with versatile styling potential. {basic_description}" primary_items = [item for item in detected_items if item['confidence'] > 0.6] if primary_items: main_category = primary_items[0]['category'] confidence = primary_items[0]['confidence'] summary = f"This {main_category} demonstrates {confidence*100:.0f}% detection confidence with professional fashion AI analysis. " if main_category in ['dress']: summary += "Offers complete outfit solution with feminine appeal and versatile styling options." elif main_category in ['top', 'shirt', 'blouse']: summary += "Serves as essential wardrobe foundation with excellent layering and styling flexibility." elif main_category in ['bottom', 'pants', 'jeans']: summary += "Provides structural foundation for balanced outfit proportions and professional styling." elif main_category in ['outer', 'jacket', 'blazer']: summary += "Functions as transformative layering piece that elevates casual outfits to professional standards." else: summary += "Represents quality fashion piece with contemporary appeal and styling versatility." else: summary = "Fashion analysis indicates contemporary styling with good versatility potential." return summary def get_average_confidence(self, detected_items): """Calculate average detection confidence""" if not detected_items: return "N/A" confidences = [item['confidence'] for item in detected_items] avg_confidence = sum(confidences) / len(confidences) return f"{avg_confidence*100:.1f}%" def extract_comprehensive_details(self, description): """Extract comprehensive details from description""" details = [] details.append(f"• Garment Type: {self.extract_garment_type(description)}") details.append(f"• Color Analysis: {self.extract_colors(description)}") details.append(f"• Pattern & Design: {self.extract_pattern(description)}") details.append(f"• Style Category: {self.extract_style(description)}") details.append(f"• Occasion Suitability: {self.extract_occasion(description)}") return "\n".join(details) def clean_generated_text(self, text): """Clean and format generated text for better readability""" if not text: return "" # Remove common artifacts and improve formatting text = text.strip() # Remove repetitive phrases lines = text.split('\n') cleaned_lines = [] seen_lines = set() for line in lines: line = line.strip() if line and line not in seen_lines and len(line) > 10: cleaned_lines.append(line) seen_lines.add(line) return '\n'.join(cleaned_lines[:5]) # Limit to 5 unique lines def generate_advanced_insights(self, description): """Generate sophisticated fashion insights when AI generation fails""" garment_type = self.extract_garment_type(description).lower() colors = self.extract_colors(description).lower() pattern = self.extract_pattern(description).lower() style = self.extract_style(description).lower() insights = [] # Advanced garment-specific insights if 'dress' in garment_type: if 'floral' in pattern: insights.extend([ "This floral dress exemplifies the timeless appeal of botanical motifs in women's fashion.", "The floral pattern creates visual movement and adds romantic femininity to the silhouette.", "Ideal for transitional seasons and outdoor social events where natural beauty is celebrated." ]) elif 'black' in colors: insights.extend([ "The classic black dress represents the epitome of versatile elegance in fashion.", "This piece serves as a foundational wardrobe element with endless styling possibilities.", "Perfect for day-to-night transitions with simple accessory changes." ]) else: insights.extend([ "This dress demonstrates contemporary design principles with classic appeal.", "The silhouette offers both comfort and sophisticated style for modern lifestyles." ]) elif any(item in garment_type for item in ['shirt', 'blouse', 'top']): insights.extend([ "This top represents a versatile foundation piece essential for capsule wardrobes.", "The design allows for multiple styling interpretations from casual to professional.", "Perfect for layering strategies and seasonal wardrobe transitions." ]) elif any(item in garment_type for item in ['pants', 'jeans', 'trousers']): insights.extend([ "These bottoms provide structural foundation for balanced outfit proportions.", "The cut and fit demonstrate attention to contemporary silhouette preferences.", "Suitable for building cohesive looks across casual and semi-formal contexts." ]) # Add sophisticated pattern and color insights if 'floral' in pattern: insights.append("The botanical motif connects the wearer to nature-inspired fashion trends and seasonal styling.") elif 'solid' in pattern: insights.append("The solid construction maximizes styling versatility and accessory compatibility.") elif 'striped' in pattern: insights.append("The linear pattern creates visual interest while maintaining classic appeal.") # Add color psychology insights if 'black' in colors: insights.append("Black conveys sophistication, authority, and timeless elegance in fashion psychology.") elif 'white' in colors: insights.append("White represents purity, freshness, and minimalist aesthetic principles.") elif any(color in colors for color in ['blue', 'navy']): insights.append("Blue tones suggest reliability, professionalism, and calming visual impact.") elif any(color in colors for color in ['red', 'burgundy']): insights.append("Red spectrum colors project confidence, energy, and bold fashion statements.") return ' '.join(insights) if insights else "This garment demonstrates thoughtful design principles with contemporary market appeal and versatile styling potential." def generate_fallback_insights(self, description): """Generate fallback insights when AI text generation fails""" garment_type = self.extract_garment_type(description).lower() pattern = self.extract_pattern(description).lower() insights = [] if 'dress' in garment_type: if 'floral' in pattern: insights.extend([ "This floral dress embodies feminine elegance and seasonal charm.", "The floral pattern adds visual interest and romantic appeal.", "Perfect for spring/summer occasions and outdoor events." ]) else: insights.extend([ "This dress offers versatile styling options for various occasions.", "The silhouette provides both comfort and style." ]) elif any(item in garment_type for item in ['shirt', 'blouse']): insights.extend([ "This top serves as a versatile wardrobe foundation piece.", "Can be styled up or down depending on the occasion." ]) elif any(item in garment_type for item in ['pants', 'jeans']): insights.extend([ "These bottoms provide a solid foundation for outfit building.", "Suitable for both casual and semi-formal styling." ]) # Add pattern-specific insights if 'floral' in pattern: insights.append("The floral motif brings natural beauty and femininity to the design.") elif 'solid' in pattern: insights.append("The solid color provides versatility for accessorizing and layering.") return ' '.join(insights) if insights else "This garment demonstrates classic design principles with contemporary appeal." def extract_garment_type(self, description): """Extract garment type from description with enhanced detection""" garment_keywords = { 'dress': 'Dress', 'gown': 'Evening Gown', 'shirt': 'Shirt/Blouse', 'blouse': 'Blouse', 'top': 'Top', 't-shirt': 'T-Shirt', 'tank': 'Tank Top', 'camisole': 'Camisole', 'pants': 'Pants/Trousers', 'trousers': 'Trousers', 'jeans': 'Jeans', 'leggings': 'Leggings', 'jacket': 'Jacket', 'blazer': 'Blazer', 'coat': 'Coat', 'cardigan': 'Cardigan', 'sweater': 'Sweater', 'pullover': 'Pullover', 'hoodie': 'Hoodie', 'sweatshirt': 'Sweatshirt', 'skirt': 'Skirt', 'shorts': 'Shorts', 'jumpsuit': 'Jumpsuit', 'romper': 'Romper', 'vest': 'Vest', 'tunic': 'Tunic' } description_lower = description.lower() # Check for specific garment types first for keyword, garment_type in garment_keywords.items(): if keyword in description_lower: return garment_type # Fallback analysis based on context clues if any(word in description_lower for word in ['wearing', 'outfit', 'clothing']): return "Fashion Garment" return "Clothing Item" def extract_colors(self, description): """Extract colors from description with enhanced detection and color theory analysis""" color_keywords = { 'black': {'name': 'Black', 'category': 'neutral', 'season': 'all', 'formality': 'high'}, 'white': {'name': 'White', 'category': 'neutral', 'season': 'all', 'formality': 'high'}, 'blue': {'name': 'Blue', 'category': 'cool', 'season': 'all', 'formality': 'medium'}, 'navy': {'name': 'Navy Blue', 'category': 'neutral', 'season': 'all', 'formality': 'high'}, 'red': {'name': 'Red', 'category': 'warm', 'season': 'winter', 'formality': 'medium'}, 'green': {'name': 'Green', 'category': 'cool', 'season': 'spring', 'formality': 'medium'}, 'yellow': {'name': 'Yellow', 'category': 'warm', 'season': 'summer', 'formality': 'low'}, 'pink': {'name': 'Pink', 'category': 'warm', 'season': 'spring', 'formality': 'low'}, 'purple': {'name': 'Purple', 'category': 'cool', 'season': 'fall', 'formality': 'medium'}, 'brown': {'name': 'Brown', 'category': 'neutral', 'season': 'fall', 'formality': 'medium'}, 'gray': {'name': 'Gray', 'category': 'neutral', 'season': 'all', 'formality': 'high'}, 'grey': {'name': 'Gray', 'category': 'neutral', 'season': 'all', 'formality': 'high'}, 'orange': {'name': 'Orange', 'category': 'warm', 'season': 'fall', 'formality': 'low'}, 'beige': {'name': 'Beige', 'category': 'neutral', 'season': 'all', 'formality': 'medium'}, 'cream': {'name': 'Cream', 'category': 'neutral', 'season': 'spring', 'formality': 'medium'}, 'tan': {'name': 'Tan', 'category': 'neutral', 'season': 'summer', 'formality': 'medium'}, 'gold': {'name': 'Gold', 'category': 'warm', 'season': 'fall', 'formality': 'high'}, 'silver': {'name': 'Silver', 'category': 'cool', 'season': 'winter', 'formality': 'high'}, 'maroon': {'name': 'Maroon', 'category': 'warm', 'season': 'fall', 'formality': 'high'}, 'burgundy': {'name': 'Burgundy', 'category': 'warm', 'season': 'fall', 'formality': 'high'}, 'teal': {'name': 'Teal', 'category': 'cool', 'season': 'winter', 'formality': 'medium'}, 'turquoise': {'name': 'Turquoise', 'category': 'cool', 'season': 'summer', 'formality': 'low'}, 'coral': {'name': 'Coral', 'category': 'warm', 'season': 'summer', 'formality': 'low'}, 'mint': {'name': 'Mint', 'category': 'cool', 'season': 'spring', 'formality': 'low'}, 'lavender': {'name': 'Lavender', 'category': 'cool', 'season': 'spring', 'formality': 'low'} } description_lower = description.lower() found_colors = [] for keyword, color_info in color_keywords.items(): if keyword in description_lower: if color_info not in found_colors: # Avoid duplicates found_colors.append(color_info) if found_colors: primary_color = found_colors[0] color_analysis = [] # Primary color analysis color_analysis.append(f"Primary: {primary_color['name']}") # Color theory insights if primary_color['category'] == 'neutral': color_analysis.append("(Neutral - highly versatile)") elif primary_color['category'] == 'warm': color_analysis.append("(Warm tone - energetic and approachable)") else: color_analysis.append("(Cool tone - calming and professional)") # Seasonal and styling context if primary_color['season'] != 'all': color_analysis.append(f"Best for {primary_color['season']} styling") # Formality level if primary_color['formality'] == 'high': color_analysis.append("Suitable for formal occasions") elif primary_color['formality'] == 'medium': color_analysis.append("Versatile for casual to semi-formal") else: color_analysis.append("Perfect for casual, relaxed styling") # Additional colors if len(found_colors) > 1: other_colors = [c['name'] for c in found_colors[1:]] color_analysis.append(f"Secondary: {', '.join(other_colors)}") return ' • '.join(color_analysis) else: # Try to infer from context if any(word in description_lower for word in ['dark', 'deep']): return "Dark tones detected • Creates sophisticated, slimming effect • Suitable for formal occasions" elif any(word in description_lower for word in ['light', 'pale', 'bright']): return "Light/bright tones detected • Fresh, youthful appearance • Perfect for casual, daytime wear" else: return "Mixed color palette • Offers styling versatility • Consider color coordination principles" def extract_neckline(self, description): """Extract neckline information with intelligent inference""" description_lower = description.lower() # Direct detection neckline_keywords = { 'v-neck': 'V-neckline', 'scoop': 'Scoop neckline', 'crew': 'Crew neckline', 'round': 'Round neckline', 'collar': 'Collared', 'turtleneck': 'Turtleneck', 'off-shoulder': 'Off-shoulder', 'strapless': 'Strapless', 'halter': 'Halter neckline' } for keyword, neckline_type in neckline_keywords.items(): if keyword in description_lower: return neckline_type # Intelligent inference based on garment type if 'dress' in description_lower: if 'formal' in description_lower or 'evening' in description_lower: return "Likely elegant neckline (common for dresses)" else: return "Standard dress neckline (round or scoop typical)" elif any(word in description_lower for word in ['shirt', 'blouse']): return "Standard shirt collar or neckline" elif 't-shirt' in description_lower: return "Crew or round neckline (typical for t-shirts)" return "Neckline present but style not specified" def extract_sleeves(self, description): """Extract sleeve information with intelligent inference""" description_lower = description.lower() # Direct detection sleeve_keywords = { 'long sleeve': 'Long sleeves', 'short sleeve': 'Short sleeves', 'sleeveless': 'Sleeveless', 'cap sleeve': 'Cap sleeves', 'three-quarter': '3/4 sleeves', 'bell sleeve': 'Bell sleeves', 'puff sleeve': 'Puff sleeves' } for keyword, sleeve_type in sleeve_keywords.items(): if keyword in description_lower: return sleeve_type # Intelligent inference based on garment type if 'dress' in description_lower: if 'summer' in description_lower or 'floral' in description_lower: return "Likely short sleeves or sleeveless (common for summer/floral dresses)" elif 'winter' in description_lower or 'formal' in description_lower: return "Likely long sleeves (common for formal/winter dresses)" else: return "Sleeve style varies (short sleeves, sleeveless, or straps typical for dresses)" elif 't-shirt' in description_lower: return "Short sleeves (standard for t-shirts)" elif any(word in description_lower for word in ['jacket', 'blazer', 'coat']): return "Long sleeves (standard for outerwear)" elif any(word in description_lower for word in ['tank', 'camisole']): return "Sleeveless or thin straps" return "Sleeve style not specified in description" def extract_pattern(self, description): """Extract pattern information with enhanced detection""" pattern_keywords = { 'floral': 'Floral print', 'flower': 'Floral design', 'striped': 'Striped pattern', 'stripes': 'Striped pattern', 'plaid': 'Plaid pattern', 'checkered': 'Checkered pattern', 'polka dot': 'Polka dot pattern', 'dots': 'Dotted pattern', 'geometric': 'Geometric pattern', 'abstract': 'Abstract pattern', 'paisley': 'Paisley pattern', 'animal print': 'Animal print', 'leopard': 'Leopard print', 'zebra': 'Zebra print', 'solid': 'Solid color', 'plain': 'Plain/solid', 'printed': 'Printed design', 'embroidered': 'Embroidered details', 'lace': 'Lace pattern', 'textured': 'Textured fabric' } description_lower = description.lower() found_patterns = [] for keyword, pattern_name in pattern_keywords.items(): if keyword in description_lower: if pattern_name not in found_patterns: found_patterns.append(pattern_name) if found_patterns: return ', '.join(found_patterns) else: # Try to infer from context if any(word in description_lower for word in ['print', 'design', 'pattern']): return "Decorative pattern present" else: return "Solid or minimal pattern" def extract_fit(self, description): """Extract fit information with intelligent inference""" description_lower = description.lower() # Direct detection fit_keywords = { 'loose': 'Loose fit', 'tight': 'Tight/fitted', 'fitted': 'Fitted', 'oversized': 'Oversized', 'slim': 'Slim fit', 'relaxed': 'Relaxed fit', 'bodycon': 'Body-conscious fit', 'a-line': 'A-line silhouette', 'straight': 'Straight fit' } for keyword, fit_type in fit_keywords.items(): if keyword in description_lower: return fit_type # Intelligent inference based on garment type and style if 'dress' in description_lower: if 'floral' in description_lower: return "Likely flowing or A-line fit (common for floral dresses)" elif 'formal' in description_lower: return "Likely fitted or tailored silhouette" else: return "Dress silhouette (fit varies by style)" elif any(word in description_lower for word in ['jeans', 'pants']): return "Standard trouser fit (straight, slim, or relaxed)" elif any(word in description_lower for word in ['blazer', 'jacket']): return "Tailored fit (structured silhouette)" elif 't-shirt' in description_lower: return "Regular fit (standard t-shirt silhouette)" return "Fit style not specified in description" def extract_material(self, description): """Extract material information with intelligent inference""" description_lower = description.lower() # Direct detection material_keywords = { 'cotton': 'Cotton', 'denim': 'Denim', 'silk': 'Silk', 'wool': 'Wool', 'polyester': 'Polyester', 'leather': 'Leather', 'linen': 'Linen', 'chiffon': 'Chiffon', 'satin': 'Satin', 'velvet': 'Velvet', 'lace': 'Lace', 'knit': 'Knit fabric', 'jersey': 'Jersey', 'tweed': 'Tweed' } found_materials = [] for keyword, material_name in material_keywords.items(): if keyword in description_lower: found_materials.append(material_name) if found_materials: return ', '.join(found_materials) # Intelligent inference based on garment type if 'dress' in description_lower: if 'floral' in description_lower: return "Likely lightweight fabric (cotton, chiffon, or jersey common for floral dresses)" elif 'formal' in description_lower: return "Likely elegant fabric (silk, satin, or crepe)" else: return "Dress fabric (varies by style and season)" elif 'jeans' in description_lower: return "Denim (standard for jeans)" elif 't-shirt' in description_lower: return "Cotton or cotton blend (typical for t-shirts)" elif any(word in description_lower for word in ['blazer', 'suit']): return "Structured fabric (wool, polyester blend, or cotton)" elif 'sweater' in description_lower: return "Knit fabric (wool, cotton, or synthetic blend)" return "Fabric type not specified in description" def extract_features(self, description): """Extract features information""" feature_keywords = ['button', 'zip', 'pocket', 'belt', 'hood'] description_lower = description.lower() found_features = [feature for feature in feature_keywords if feature in description_lower] return ', '.join(found_features).title() if found_features else "Specific features not clearly visible" def extract_enhanced_colors(self, description): """Enhanced color extraction with tone analysis and fashion context""" description_lower = description.lower() # Enhanced color detection with tone analysis color_analysis = { 'black': {'name': 'Black', 'tone': 'neutral', 'context': 'timeless and versatile'}, 'white': {'name': 'White', 'tone': 'neutral', 'context': 'clean and minimalist'}, 'blue': {'name': 'Blue', 'tone': 'cool', 'context': 'calming and professional'}, 'navy': {'name': 'Navy blue', 'tone': 'neutral', 'context': 'sophisticated and classic'}, 'red': {'name': 'Red', 'tone': 'warm', 'context': 'bold and energetic'}, 'green': {'name': 'Green', 'tone': 'cool', 'context': 'natural and refreshing'}, 'yellow': {'name': 'Yellow', 'tone': 'warm', 'context': 'cheerful and optimistic'}, 'pink': {'name': 'Pink', 'tone': 'warm', 'context': 'feminine and soft'}, 'purple': {'name': 'Purple', 'tone': 'cool', 'context': 'creative and luxurious'}, 'orange': {'name': 'Orange', 'tone': 'warm', 'context': 'vibrant and playful'}, 'brown': {'name': 'Brown', 'tone': 'warm', 'context': 'earthy and grounded'}, 'gray': {'name': 'Gray', 'tone': 'neutral', 'context': 'sophisticated and balanced'}, 'grey': {'name': 'Grey', 'tone': 'neutral', 'context': 'sophisticated and balanced'}, 'beige': {'name': 'Beige', 'tone': 'neutral', 'context': 'warm neutral and versatile'}, 'cream': {'name': 'Cream', 'tone': 'warm', 'context': 'soft and elegant'}, 'tan': {'name': 'Tan', 'tone': 'warm', 'context': 'natural and casual'}, 'khaki': {'name': 'Khaki', 'tone': 'neutral', 'context': 'utilitarian and relaxed'} } detected_colors = [] tone_categories = [] for color_key, color_info in color_analysis.items(): if color_key in description_lower: detected_colors.append(color_info['name']) tone_categories.append(color_info['tone']) if detected_colors: primary_color = detected_colors[0] primary_tone = tone_categories[0] if len(detected_colors) > 1: color_description = f"{primary_color} with {', '.join(detected_colors[1:])} accents ({primary_tone} tone palette)" else: color_description = f"{primary_color} ({primary_tone} tone)" # Add seasonal and styling context if primary_tone == 'neutral': color_description += " - versatile for all seasons and easy to style" elif primary_tone == 'warm': color_description += " - energetic warm tone, ideal for autumn/winter" elif primary_tone == 'cool': color_description += " - calming cool tone, perfect for spring/summer" return color_description # Fallback color inference if any(word in description_lower for word in ['dark', 'deep']): return "Dark tones (sophisticated and formal)" elif any(word in description_lower for word in ['light', 'pale', 'soft']): return "Light tones (fresh and approachable)" elif any(word in description_lower for word in ['bright', 'vibrant']): return "Bright tones (energetic and eye-catching)" return "Color not clearly specified (likely neutral or muted tones)" def extract_enhanced_material(self, description, garment_type): """Enhanced material extraction with texture and quality insights""" description_lower = description.lower() garment_lower = garment_type.lower() # Enhanced material detection with context material_analysis = { 'cotton': {'name': 'Cotton', 'texture': 'soft and breathable', 'care': 'easy-care'}, 'denim': {'name': 'Denim', 'texture': 'structured and durable', 'care': 'low-maintenance'}, 'silk': {'name': 'Silk', 'texture': 'smooth and luxurious', 'care': 'delicate'}, 'wool': {'name': 'Wool', 'texture': 'warm and textured', 'care': 'special care'}, 'polyester': {'name': 'Polyester', 'texture': 'smooth and wrinkle-resistant', 'care': 'easy-care'}, 'leather': {'name': 'Leather', 'texture': 'smooth and structured', 'care': 'special care'}, 'linen': {'name': 'Linen', 'texture': 'crisp and breathable', 'care': 'wrinkle-prone'}, 'chiffon': {'name': 'Chiffon', 'texture': 'flowing and delicate', 'care': 'delicate'}, 'satin': {'name': 'Satin', 'texture': 'smooth and lustrous', 'care': 'delicate'}, 'velvet': {'name': 'Velvet', 'texture': 'plush and luxurious', 'care': 'special care'}, 'knit': {'name': 'Knit fabric', 'texture': 'stretchy and comfortable', 'care': 'easy-care'}, 'jersey': {'name': 'Jersey', 'texture': 'soft and stretchy', 'care': 'easy-care'}, 'tweed': {'name': 'Tweed', 'texture': 'textured and structured', 'care': 'dry clean'}, 'chambray': {'name': 'Chambray', 'texture': 'lightweight and soft', 'care': 'easy-care'}, 'flannel': {'name': 'Flannel', 'texture': 'soft and warm', 'care': 'easy-care'} } detected_materials = [] for material_key, material_info in material_analysis.items(): if material_key in description_lower: detected_materials.append(f"{material_info['name']} ({material_info['texture']})") if detected_materials: return ', '.join(detected_materials) # Intelligent material inference based on garment type if any(word in garment_lower for word in ['jeans', 'denim']): return "Denim (structured cotton weave, durable and versatile)" elif any(word in garment_lower for word in ['t-shirt', 'tee']): return "Cotton jersey (soft, breathable, and comfortable)" elif any(word in garment_lower for word in ['dress', 'blouse']): if 'formal' in description_lower or 'elegant' in description_lower: return "Silk or silk-blend (smooth drape, elegant finish)" else: return "Cotton or cotton blend (comfortable and versatile)" elif any(word in garment_lower for word in ['blazer', 'jacket']): return "Wool blend or structured fabric (tailored appearance, professional)" elif any(word in garment_lower for word in ['sweater', 'pullover']): return "Knit wool or cotton blend (warm, textured, cozy)" elif any(word in garment_lower for word in ['shoes', 'boots']): return "Leather or synthetic leather (durable, structured)" elif any(word in garment_lower for word in ['sneakers']): return "Canvas, mesh, or synthetic materials (lightweight, breathable)" return "Material not clearly visible (likely standard garment-appropriate fabric)" def extract_enhanced_features(self, description, garment_type): """Enhanced feature extraction with detailed fashion analysis""" desc_lower = description.lower() garment_lower = garment_type.lower() features = [] # Detailed feature detection with fashion context feature_mapping = { 'button': 'Button closure (classic and adjustable)', 'zip': 'Zipper details (modern and functional)', 'pocket': 'Functional pockets (practical and stylish)', 'belt': 'Belt or waist definition (silhouette-enhancing)', 'hood': 'Hood detail (casual and functional)', 'collar': 'Collar design (structured and polished)', 'sleeve': 'Distinctive sleeve styling', 'pattern': 'Patterned design (visual interest)', 'print': 'Printed motifs (decorative elements)', 'embroidered': 'Embroidered details (artisanal touch)', 'lace': 'Lace accents (feminine and delicate)', 'ruffle': 'Ruffle details (romantic and textured)', 'pleat': 'Pleated construction (structured movement)', 'gather': 'Gathered fabric (soft volume)', 'tuck': 'Tucked details (tailored precision)' } for keyword, feature_desc in feature_mapping.items(): if keyword in desc_lower: features.append(feature_desc) # Garment-specific feature analysis if any(word in garment_lower for word in ['shirt', 'blouse']): # Neckline analysis if 'v-neck' in desc_lower: features.append("V-neckline (elongating and flattering)") elif 'crew' in desc_lower or 'round' in desc_lower: features.append("Round neckline (classic and versatile)") elif 'scoop' in desc_lower: features.append("Scoop neckline (feminine and soft)") # Sleeve analysis if 'long sleeve' in desc_lower: features.append("Long sleeves (coverage and sophistication)") elif 'short sleeve' in desc_lower: features.append("Short sleeves (casual and comfortable)") elif 'sleeveless' in desc_lower: features.append("Sleeveless design (modern and airy)") # Fit analysis if 'fitted' in desc_lower or 'slim' in desc_lower: features.append("Fitted silhouette (body-conscious and modern)") elif 'loose' in desc_lower or 'relaxed' in desc_lower: features.append("Relaxed fit (comfortable and casual)") elif 'oversized' in desc_lower: features.append("Oversized fit (contemporary and comfortable)") elif any(word in garment_lower for word in ['pants', 'jeans', 'trousers']): # Fit analysis if 'skinny' in desc_lower: features.append("Skinny fit (body-hugging and modern)") elif 'slim' in desc_lower: features.append("Slim fit (tailored and flattering)") elif 'straight' in desc_lower: features.append("Straight leg (classic and versatile)") elif 'wide' in desc_lower or 'flare' in desc_lower: features.append("Wide leg (flowing and dramatic)") # Rise analysis if 'high-waisted' in desc_lower or 'high waist' in desc_lower: features.append("High-waisted (vintage-inspired and flattering)") elif 'low-rise' in desc_lower: features.append("Low-rise (casual and relaxed)") # Length analysis if 'cropped' in desc_lower or 'ankle' in desc_lower: features.append("Cropped length (modern and versatile)") elif any(word in garment_lower for word in ['dress']): # Length analysis if 'mini' in desc_lower: features.append("Mini length (youthful and bold)") elif 'midi' in desc_lower: features.append("Midi length (elegant and versatile)") elif 'maxi' in desc_lower: features.append("Maxi length (dramatic and flowing)") # Silhouette analysis if 'a-line' in desc_lower: features.append("A-line silhouette (universally flattering)") elif 'bodycon' in desc_lower or 'fitted' in desc_lower: features.append("Fitted silhouette (body-conscious and sleek)") elif 'shift' in desc_lower: features.append("Shift silhouette (modern and comfortable)") elif any(word in garment_lower for word in ['shoes', 'sneakers', 'boots']): # Closure analysis if 'lace' in desc_lower: features.append("Lace-up closure (adjustable and secure)") elif 'slip-on' in desc_lower: features.append("Slip-on style (convenient and streamlined)") elif 'buckle' in desc_lower: features.append("Buckle closure (decorative and functional)") # Heel analysis if 'heel' in desc_lower: if 'high' in desc_lower: features.append("High heel (elegant and formal)") elif 'low' in desc_lower: features.append("Low heel (comfortable and versatile)") else: features.append("Heeled design (elevated and polished)") elif 'flat' in desc_lower: features.append("Flat sole (comfortable and casual)") # Style details if 'platform' in desc_lower: features.append("Platform sole (height and comfort)") if 'pointed' in desc_lower: features.append("Pointed toe (sleek and sophisticated)") elif 'round' in desc_lower: features.append("Round toe (classic and comfortable)") # General styling features if 'layered' in desc_lower: features.append("Layered styling (dimensional and interesting)") if 'textured' in desc_lower: features.append("Textured fabric (tactile and visual interest)") if 'structured' in desc_lower: features.append("Structured construction (tailored and polished)") if 'flowing' in desc_lower: features.append("Flowing silhouette (graceful and feminine)") return ', '.join(features) if features else "Clean, minimalist design with standard construction details" def create_enhanced_outfit_summary(self, detected_items, basic_description): """Create enhanced outfit summary with sophisticated fashion analysis""" # Extract key elements for analysis garment_types = [item['category'] for item in detected_items if item['confidence'] > 0.5] colors = self.extract_enhanced_colors(basic_description) style_elements = self.extract_style(basic_description) pattern_info = self.extract_pattern(basic_description) summary_components = [] # Opening statement about overall aesthetic and fashion intent if 'dress' in garment_types: summary_components.append("This outfit showcases the power of a well-chosen dress as a complete styling solution, demonstrating how a single piece can create a sophisticated and intentional look.") elif len(garment_types) >= 2: summary_components.append("This outfit exemplifies thoughtful coordination between multiple garment pieces, creating a harmonious ensemble that reflects contemporary fashion sensibility.") else: summary_components.append("This outfit highlights the impact of a key fashion piece, demonstrating how individual garments can make a strong style statement.") # Color, texture, and silhouette interaction analysis if 'neutral' in colors.lower(): summary_components.append("The neutral color palette creates exceptional versatility and timeless appeal, allowing for easy accessorizing and seamless integration into various settings.") elif 'warm' in colors.lower(): summary_components.append("The warm color tones generate an approachable and energetic aesthetic that conveys confidence and optimism, perfect for making a positive impression.") elif 'cool' in colors.lower(): summary_components.append("The cool color palette establishes a calming and professional presence, ideal for both casual sophistication and semi-formal occasions.") else: summary_components.append("The color choices demonstrate thoughtful consideration for both visual impact and wearability across different contexts.") # Pattern and design element analysis if 'floral' in pattern_info.lower(): summary_components.append("The floral elements introduce natural beauty and romantic charm, adding feminine appeal while maintaining contemporary relevance.") elif 'striped' in pattern_info.lower(): summary_components.append("The striped pattern provides classic visual interest and timeless sophistication, creating structure and movement within the design.") elif 'solid' in pattern_info.lower() or 'minimal' in pattern_info.lower(): summary_components.append("The solid construction maximizes styling flexibility and serves as an excellent foundation for creative accessorizing and layering.") # Cohesion and balance assessment if len(garment_types) > 1: summary_components.append("The pieces work together harmoniously, demonstrating an understanding of proportion, balance, and color coordination that creates visual cohesion.") # Occasion suitability and versatility analysis if 'casual' in style_elements.lower(): summary_components.append("The casual styling approach makes this outfit perfect for everyday wear, weekend activities, and relaxed social gatherings while maintaining a polished appearance.") elif 'formal' in style_elements.lower() or 'business' in style_elements.lower(): summary_components.append("The formal elements ensure this outfit meets professional standards and elegant occasion requirements with confidence and sophistication.") elif 'versatile' in style_elements.lower(): summary_components.append("The versatile design allows this outfit to transition seamlessly between different settings, from casual daytime events to more polished evening occasions.") else: summary_components.append("The styling choices reflect adaptability and modern lifestyle needs, suitable for multiple occasions and settings.") # Fashion-forward observations and concluding statement if any(word in basic_description.lower() for word in ['layered', 'mixed', 'contrast']): summary_components.append("The layering techniques and contrast elements showcase contemporary fashion awareness, demonstrating how modern styling can create depth and visual interest while maintaining wearability.") elif any(word in basic_description.lower() for word in ['minimalist', 'clean', 'simple']): summary_components.append("The minimalist approach reflects current fashion trends toward refined simplicity, proving that thoughtful restraint can create maximum impact and enduring style.") else: summary_components.append("The overall composition reflects contemporary fashion intelligence, combining classic principles with modern sensibilities to create an effortlessly chic and memorable appearance.") # Combine components into flowing paragraph return " ".join(summary_components) def extract_style(self, description): """Extract style information with intelligent inference""" description_lower = description.lower() # Direct detection style_keywords = { 'casual': 'Casual', 'formal': 'Formal', 'business': 'Business/Professional', 'sporty': 'Sporty/Athletic', 'elegant': 'Elegant', 'vintage': 'Vintage', 'bohemian': 'Bohemian', 'minimalist': 'Minimalist', 'romantic': 'Romantic', 'edgy': 'Edgy', 'classic': 'Classic', 'trendy': 'Trendy/Contemporary' } found_styles = [] for keyword, style_name in style_keywords.items(): if keyword in description_lower: found_styles.append(style_name) if found_styles: return ', '.join(found_styles) # Intelligent inference based on garment type and patterns if 'dress' in description_lower: if 'floral' in description_lower: return "Feminine/Romantic (floral dresses often have romantic appeal)" elif 'black' in description_lower: return "Classic/Versatile (black dresses are wardrobe staples)" else: return "Feminine/Versatile (dresses suit various style preferences)" elif any(word in description_lower for word in ['blazer', 'suit']): return "Professional/Business (structured formal wear)" elif 'jeans' in description_lower: return "Casual/Everyday (denim is casual staple)" elif 't-shirt' in description_lower: return "Casual/Relaxed (t-shirts are casual basics)" elif any(word in description_lower for word in ['jacket', 'coat']): return "Outerwear/Functional (practical and stylish)" return "Versatile style (suitable for multiple aesthetics)" def extract_occasion(self, description): """Extract occasion information""" if any(word in description.lower() for word in ['formal', 'business', 'suit']): return "Formal occasions, business settings" elif any(word in description.lower() for word in ['casual', 'everyday']): return "Casual wear, everyday use" else: return "Versatile for multiple occasions" def generate_styling_tips(self, description): """Generate styling tips based on the garment""" garment_type = self.extract_garment_type(description).lower() pattern = self.extract_pattern(description).lower() tips = [] if 'dress' in garment_type: tips.extend([ "• Pair with a denim jacket for casual daywear", "• Add heels and accessories for evening events", "• Layer with a cardigan for office-appropriate styling" ]) elif 'shirt' in garment_type or 'blouse' in garment_type: tips.extend([ "• Tuck into high-waisted pants for a polished look", "• Wear open over a tank top for layered styling", "• Knot at the waist for a casual, trendy appearance" ]) elif 'pants' in garment_type or 'jeans' in garment_type: tips.extend([ "• Pair with a fitted top to balance proportions", "• Add a blazer for business casual styling", "• Combine with sneakers for comfortable everyday wear" ]) if 'floral' in pattern: tips.append("• Keep accessories minimal to let the pattern shine") elif 'solid' in pattern: tips.append("• Perfect base for statement accessories and bold jewelry") return '\n'.join(tips) if tips else "• Versatile piece suitable for various styling approaches" def get_versatility_assessment(self, description): """Assess the versatility of the garment""" garment_type = self.extract_garment_type(description).lower() colors = self.extract_colors(description).lower() pattern = self.extract_pattern(description).lower() versatility_score = 0 assessment_parts = [] # Assess based on garment type if any(item in garment_type for item in ['dress', 'shirt', 'blouse', 'pants', 'jeans']): versatility_score += 2 assessment_parts.append("highly versatile garment type") # Assess based on colors if any(color in colors for color in ['black', 'white', 'navy', 'gray']): versatility_score += 2 assessment_parts.append("neutral color palette") elif colors == "colors not clearly identified": versatility_score += 1 assessment_parts.append("adaptable color scheme") # Assess based on pattern if 'solid' in pattern: versatility_score += 2 assessment_parts.append("solid pattern for easy mixing") elif any(p in pattern for p in ['floral', 'striped']): versatility_score += 1 assessment_parts.append("distinctive pattern adds character") if versatility_score >= 4: return f"This is a {', '.join(assessment_parts)} making it an excellent wardrobe staple." elif versatility_score >= 2: return f"Features {', '.join(assessment_parts)} providing good styling flexibility." else: return "Offers unique styling opportunities for specific occasions." # Import DeepFashion2 utilities try: from deepfashion2_utils import DeepFashion2Config, get_deepfashion2_statistics from deepfashion2_evaluation import run_full_evaluation DEEPFASHION2_AVAILABLE = True except ImportError as e: print(f"DeepFashion2 utilities not available: {e}") DEEPFASHION2_AVAILABLE = False # Initialize analyzer analyzer = HuggingFaceFashionAnalyzer() # Initialize DeepFashion2 configuration if available deepfashion2_config = None if DEEPFASHION2_AVAILABLE: try: deepfashion2_config = DeepFashion2Config() print(f"DeepFashion2 integration initialized. Dataset root: {deepfashion2_config.dataset_root}") except Exception as e: print(f"Failed to initialize DeepFashion2 config: {e}") DEEPFASHION2_AVAILABLE = False # Request/Response models class GarmentDetails(BaseModel): type: str color: str material: str features: str class OutfitSummary(BaseModel): aesthetic: str style_notes: str occasion_suitability: str color_harmony: str overall_assessment: str class StructuredAnalysisResponse(BaseModel): upper_garment: GarmentDetails lower_garment: GarmentDetails footwear: GarmentDetails outfit_summary: OutfitSummary confidence_score: float detected_items: List[Dict[str, Any]] = [] class DetailedAnalysisResponse(BaseModel): structured_analysis: StructuredAnalysisResponse raw_analysis: str processing_time: float model_info: Dict[str, str] # API Endpoints @app.get("/", response_class=HTMLResponse) async def root(): """Main page with file upload interface""" return """ Fashion Analyzer

šŸŽ½ Fashion Analyzer - JSON API

Upload an image of clothing to get detailed fashion analysis in structured JSON format



View Refined Prompt Format
""" @app.post("/analyze-image", response_model=DetailedAnalysisResponse) async def analyze_image(file: UploadFile = File(...)): """Analyze uploaded image and return structured JSON response""" try: start_time = time.time() # Read image bytes image_bytes = await file.read() # Process image image = analyzer.process_image_from_bytes(image_bytes) # Get fashion object detection results detection_results = analyzer.detect_fashion_objects(image) # Get basic image description basic_description = analyzer.get_basic_description(image) # Extract structured information structured_analysis = analyzer.parse_structured_analysis(detection_results, basic_description) # Get raw analysis for comparison raw_analysis = analyzer.analyze_clothing_structured_format(image_bytes) processing_time = time.time() - start_time return DetailedAnalysisResponse( structured_analysis=structured_analysis, raw_analysis=raw_analysis, processing_time=processing_time, model_info={ "detection_model": analyzer.detection_ckpt if analyzer.detection_model else "Not available", "feature_model": analyzer.encoder_ckpt if analyzer.feature_encoder else "Not available", "device": analyzer.device } ) except Exception as e: raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}") @app.post("/analyze-structured", response_model=StructuredAnalysisResponse) async def analyze_structured(file: UploadFile = File(...)): """Analyze uploaded image with structured format and return JSON""" try: # Read image bytes image_bytes = await file.read() # Process image image = analyzer.process_image_from_bytes(image_bytes) # Get fashion object detection results detection_results = analyzer.detect_fashion_objects(image) # Get basic image description basic_description = analyzer.get_basic_description(image) # Extract structured information structured_analysis = analyzer.parse_structured_analysis(detection_results, basic_description) return structured_analysis except Exception as e: raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}") @app.post("/analyze-enhanced", response_class=PlainTextResponse) async def analyze_clothing_enhanced(file: UploadFile = File(...)): """Analyze clothing using enhanced prompt format optimized for feature extraction""" try: # Read image bytes image_bytes = await file.read() # Analyze clothing using enhanced prompt format analysis = analyzer.analyze_clothing_enhanced_prompt_format(image_bytes) return analysis except Exception as e: raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}") @app.post("/detect-objects", response_model=Dict[str, Any]) async def detect_objects(file: UploadFile = File(...)): """Detect fashion objects and return JSON structure""" try: # Read image bytes image_bytes = await file.read() # Process image image = analyzer.process_image_from_bytes(image_bytes) # Get fashion object detection results detection_results = analyzer.detect_fashion_objects(image) return detection_results except Exception as e: raise HTTPException(status_code=500, detail=f"Error detecting objects: {str(e)}") @app.post("/extract-features", response_model=Dict[str, Any]) async def extract_features(file: UploadFile = File(...)): """Extract fashion features and return JSON structure""" try: # Read image bytes image_bytes = await file.read() # Process image image = analyzer.process_image_from_bytes(image_bytes) # Extract fashion features feature_results = analyzer.extract_fashion_features(image) return feature_results except Exception as e: raise HTTPException(status_code=500, detail=f"Error extracting features: {str(e)}") @app.get("/refined-prompt", response_class=PlainTextResponse) async def get_refined_prompt(): """Get the refined prompt for clothing analysis""" refined_prompt = """ šŸ” **CLOTHING ANALYSIS INSTRUCTION PROMPT — OPTIMIZED FOR OUTFIT RECOMMENDATION AI** You are an expert fashion stylist and computer vision analyst. Your task is to **analyze a photo of a person** and produce a **detailed, structured breakdown** of their outfit. Focus on identifying visible garments and extracting features relevant to **style, fit, color theory, material, and occasion**. Use the following **strict format** and ensure each field is filled with accurate, professional fashion insights. When unsure, make reasonable inferences based on visual clues and fashion knowledge. --- ### 🧩 **RESPONSE STRUCTURE**: --- ### **1. UPPER GARMENT** * **Type**: *(e.g., t-shirt, shirt, blouse, jacket, coat, dress, blazer, hoodie)* * **Color**: *(Primary color + notable accents; include tone category — warm, cool, neutral)* * **Material**: *(Visually identifiable or inferred — cotton, denim, knit, silk, etc.)* * **Features**: *(e.g., sleeve length, neckline, collar, fit, embellishments, print/pattern, texture)* --- ### **2. LOWER GARMENT** * **Type**: *(e.g., jeans, trousers, skirt, shorts, joggers, culottes)* * **Color**: *(Primary + secondary tones; match to upper color context)* * **Material**: *(e.g., denim, polyester, linen; infer if not obvious)* * **Features**: *(e.g., cut, length, waist rise, fit, pleats, pockets, fasteners, patterns)* --- ### **3. FOOTWEAR** * **Type**: *(e.g., sneakers, boots, sandals, loafers, heels, formal shoes)* * **Color**: *(Main color + accent details; match to outfit tones if relevant)* * **Material**: *(e.g., leather, suede, canvas, mesh — inferred if unclear)* * **Features**: *(e.g., laces, buckles, platform, heel height, style, sole type)* --- ### **4. OUTFIT SUMMARY** Write a **3–5 sentence paragraph** that: * Evaluates the **overall style and fashion intent** of the outfit * Describes how **color, texture, and silhouette** interact * Comments on **cohesion and balance** between garments * Assesses **occasion suitability** (e.g., casual, business casual, streetwear, evening) * Provides **fashion-forward observations** (e.g., layering techniques, contrast, minimalism, etc.) --- ### āœ… **GUIDELINES FOR THE MODEL**: * Use **fashion-specific vocabulary** (e.g., oversized, tailored, cropped, monochromatic, muted tones) * Apply **color theory**: mention harmony/contrast, seasonal suitability, or tone warmth * Infer **formality level** (e.g., smart casual, athleisure, streetwear, semi-formal) * Mention **fit and silhouette** if visible (e.g., slim-fit, relaxed, boxy, flowy) * If a garment is partially occluded, describe what's visible and make plausible inferences * Use consistent structure and complete all sections unless a category is **not applicable** --- ### šŸ“ **EXAMPLE OUTPUT FORMAT**: --- #### **UPPER GARMENT** **Type**: Button-down chambray shirt **Color**: Light denim blue (cool tone) with faded areas on collar and seams **Material**: Chambray (lightweight woven cotton) **Features**: Long sleeves rolled to elbows, pointed collar, front patch pockets, relaxed fit, visible stitching --- #### **LOWER GARMENT** **Type**: Black slim-fit chinos **Color**: Solid black (neutral tone) **Material**: Stretch cotton twill **Features**: Flat front, minimal pockets, cropped ankle length, clean silhouette --- #### **FOOTWEAR** **Type**: White minimalist sneakers **Color**: White with subtle grey accents on heel tab **Material**: Leather upper with rubber sole **Features**: Lace-up closure, low-profile design, round toe --- #### **OUTFIT SUMMARY** This outfit channels effortless smart-casual style with its blend of soft textures and minimal structure. The chambray shirt introduces a laid-back, workwear-inspired vibe, complemented by the refined edge of slim black chinos. The white sneakers tie the look together with understated coolness, ensuring comfort without sacrificing polish. The color palette of light blue, black, and white reflects balance and neutrality, making the outfit adaptable for casual office settings, social outings, or relaxed dates. The overall silhouette is clean and streamlined, making it modern, versatile, and easy to accessorize. """ return refined_prompt @app.get("/health") async def health_check(): """Health check endpoint""" try: # Test HuggingFace models availability if hasattr(analyzer, 'image_to_text') and analyzer.image_to_text is not None: return {"status": "healthy", "models": "loaded", "device": analyzer.device} else: return {"status": "unhealthy", "models": "not_loaded"} except Exception as e: return {"status": "unhealthy", "error": str(e)} # DeepFashion2 API endpoints @app.get("/deepfashion2/status") async def deepfashion2_status(): """Get DeepFashion2 integration status""" if not DEEPFASHION2_AVAILABLE: return {"available": False, "message": "DeepFashion2 utilities not available"} if not deepfashion2_config: return {"available": False, "message": "DeepFashion2 configuration not initialized"} # Check if dataset exists dataset_path = Path(deepfashion2_config.dataset_root) dataset_exists = dataset_path.exists() return { "available": True, "dataset_exists": dataset_exists, "dataset_root": deepfashion2_config.dataset_root, "categories": deepfashion2_config.categories, "image_size": deepfashion2_config.image_size } @app.get("/deepfashion2/statistics") async def deepfashion2_statistics(): """Get DeepFashion2 dataset statistics""" if not DEEPFASHION2_AVAILABLE or not deepfashion2_config: raise HTTPException(status_code=503, detail="DeepFashion2 not available") try: stats = get_deepfashion2_statistics(deepfashion2_config) return stats except Exception as e: raise HTTPException(status_code=500, detail=f"Error getting statistics: {str(e)}") @app.post("/deepfashion2/evaluate") async def deepfashion2_evaluate(max_samples: int = 50): """Run evaluation using DeepFashion2 dataset""" if not DEEPFASHION2_AVAILABLE or not deepfashion2_config: raise HTTPException(status_code=503, detail="DeepFashion2 not available") try: # Run evaluation in background (for demo purposes, limit samples) report_path = run_full_evaluation(analyzer, deepfashion2_config, max_samples=max_samples) return { "status": "completed", "report_path": report_path, "max_samples": max_samples, "message": f"Evaluation completed with {max_samples} samples" } except Exception as e: raise HTTPException(status_code=500, detail=f"Evaluation failed: {str(e)}") @app.get("/deepfashion2/setup-instructions") async def deepfashion2_setup_instructions(): """Get setup instructions for DeepFashion2 dataset""" return { "title": "DeepFashion2 Dataset Setup Instructions", "steps": [ { "step": 1, "description": "Visit the official DeepFashion2 repository", "url": "https://github.com/switchablenorms/DeepFashion2" }, { "step": 2, "description": "Follow the dataset download instructions in the repository" }, { "step": 3, "description": "Create the dataset directory", "command": f"mkdir -p {deepfashion2_config.dataset_root if deepfashion2_config else './data/deepfashion2'}" }, { "step": 4, "description": "Extract the dataset with the following structure:", "structure": [ "deepfashion2/", "ā”œā”€ā”€ train/", "│ ā”œā”€ā”€ image/", "│ └── annos/", "ā”œā”€ā”€ validation/", "│ ā”œā”€ā”€ image/", "│ └── annos/", "└── test/", " ā”œā”€ā”€ image/", " └── annos/" ] }, { "step": 5, "description": "Verify the setup by checking the status endpoint", "endpoint": "/deepfashion2/status" } ], "notes": [ "The DeepFashion2 dataset is large (~15GB) and requires registration", "Make sure you have sufficient disk space", "The dataset contains 491K images across 13 clothing categories" ] } if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=7860)