Vestiq / fast.py
Hashii1729's picture
Enhance HuggingFaceFashionAnalyzer: optimize model loading, suppress warnings, and improve CPU performance settings
c96ca80
raw
history blame
82.9 kB
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.responses import JSONResponse, HTMLResponse, PlainTextResponse
from pydantic import BaseModel
from typing import List, Optional
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
# 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 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 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 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_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."
# Initialize analyzer
analyzer = HuggingFaceFashionAnalyzer()
# Request/Response models
class AnalysisResponse(BaseModel):
analysis: str
# API Endpoints
@app.get("/", response_class=HTMLResponse)
async def root():
"""Main page with file upload interface"""
return """
<!DOCTYPE html>
<html>
<head>
<title>Fashion Analyzer</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 50px auto; padding: 20px; }
.upload-area { border: 2px dashed #ccc; padding: 50px; text-align: center; margin: 20px 0; }
.result { background: #f5f5f5; padding: 20px; margin: 20px 0; border-radius: 5px; }
</style>
</head>
<body>
<h1>🎽 Fashion Analyzer</h1>
<p>Upload an image of clothing to get detailed fashion analysis</p>
<div class="upload-area">
<input type="file" id="imageInput" accept="image/*" style="margin: 10px;">
<br>
<button onclick="analyzeImage()" style="padding: 10px 20px; margin: 10px;">Analyze Fashion (Detailed)</button>
<button onclick="analyzeStructured()" style="padding: 10px 20px; margin: 10px;">Analyze Fashion (Structured)</button>
<br>
<a href="/refined-prompt" target="_blank" style="color: #007bff; text-decoration: none;">View Refined Prompt Format</a>
</div>
<div id="result" class="result" style="display: none;">
<h3>Analysis Result:</h3>
<pre id="analysisText"></pre>
</div>
<script>
async function analyzeImage() {
const input = document.getElementById('imageInput');
const file = input.files[0];
if (!file) {
alert('Please select an image file');
return;
}
const formData = new FormData();
formData.append('file', file);
document.getElementById('analysisText').textContent = 'Analyzing... Please wait...';
document.getElementById('result').style.display = 'block';
try {
const response = await fetch('/analyze-image', {
method: 'POST',
body: formData
});
const result = await response.json();
document.getElementById('analysisText').textContent = result.analysis;
} catch (error) {
document.getElementById('analysisText').textContent = 'Error: ' + error.message;
}
}
async function analyzeStructured() {
const input = document.getElementById('imageInput');
const file = input.files[0];
if (!file) {
alert('Please select an image file');
return;
}
const formData = new FormData();
formData.append('file', file);
document.getElementById('analysisText').textContent = 'Analyzing with structured format... Please wait...';
document.getElementById('result').style.display = 'block';
try {
const response = await fetch('/analyze-structured', {
method: 'POST',
body: formData
});
const result = await response.json();
document.getElementById('analysisText').textContent = result.analysis;
} catch (error) {
document.getElementById('analysisText').textContent = 'Error: ' + error.message;
}
}
</script>
</body>
</html>
"""
@app.post("/analyze-image", response_model=AnalysisResponse)
async def analyze_image(file: UploadFile = File(...)):
"""Analyze uploaded image"""
try:
# Read image bytes
image_bytes = await file.read()
# Analyze the clothing
analysis = analyzer.analyze_clothing_from_bytes(image_bytes)
return AnalysisResponse(analysis=analysis)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
@app.post("/analyze-structured", response_model=AnalysisResponse)
async def analyze_structured(file: UploadFile = File(...)):
"""Analyze uploaded image with structured format"""
try:
# Read image bytes
image_bytes = await file.read()
# Get structured analysis
analysis = analyzer.analyze_clothing_structured_format(image_bytes)
return AnalysisResponse(analysis=analysis)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
@app.get("/refined-prompt", response_class=PlainTextResponse)
async def get_refined_prompt():
"""Get the refined prompt for clothing analysis"""
refined_prompt = """
REFINED CLOTHING ANALYSIS PROMPT
This task involves analyzing an image of a person and providing a detailed description of their clothing, focusing on the upper garment, lower garment, and footwear. Your response should follow this structured format:
ANALYSIS STRUCTURE:
1. UPPER GARMENT:
Type: [e.g., shirt, blouse, sweater, t-shirt, dress, jacket, blazer]
Color: [Primary color and any secondary colors with color theory insights]
Material: [Fabric type if visible, or intelligent inference based on garment type]
Features: [Distinguishing features like patterns, buttons, collars, sleeves, neckline]
2. LOWER GARMENT:
Type: [e.g., jeans, pants, skirt, shorts, trousers]
Color: [Primary color and any secondary colors]
Material: [Fabric type if visible, or intelligent inference]
Features: [Distinguishing features like pockets, belt, pleats, fit, length]
3. FOOTWEAR:
Type: [e.g., sneakers, heels, boots, flats, sandals]
Color: [Primary color and any secondary colors]
Material: [Material type if visible, or intelligent inference]
Features: [Distinguishing features like laces, buckles, studs, heel height, style]
4. OUTFIT SUMMARY:
Provide a comprehensive paragraph (3-5 sentences) that:
- Describes the overall aesthetic and style of the outfit
- Explains how the different pieces complement each other
- Assesses the occasion appropriateness and versatility
- Includes color harmony and pattern coordination analysis
- Offers styling insights and fashion-forward observations
ANALYSIS GUIDELINES:
• For each field, provide detailed descriptions that capture both visible elements and professional fashion insights
• Use fashion terminology appropriately (silhouette, drape, texture, etc.)
• Include color theory analysis (warm/cool tones, neutrals, seasonal appropriateness)
• Consider fit, proportion, and styling principles
• Assess formality level and occasion suitability
• Provide intelligent inferences when specific details aren't clearly visible
• The Outfit Summary should be creative, informative, and demonstrate fashion expertise
OUTPUT FORMAT:
Structure your response exactly as shown above, with clear section headers and consistent formatting. Each section should provide both factual observations and professional fashion analysis.
EXAMPLE OUTPUT FORMAT:
UPPER GARMENT:
Type: Floral midi dress
Color: Navy blue base with white and pink floral print (cool-toned palette, versatile for multiple seasons)
Material: Lightweight cotton or cotton blend (suitable for comfort and drape)
Features: Short sleeves, round neckline, fitted bodice with A-line skirt, all-over floral pattern
LOWER GARMENT:
[Not applicable - dress serves as complete outfit]
FOOTWEAR:
Type: White leather sneakers
Color: Clean white with minimal accent details
Material: Leather upper with rubber sole
Features: Lace-up closure, low-profile design, classic athletic styling
OUTFIT SUMMARY:
This outfit demonstrates a perfect balance between feminine charm and casual comfort through the floral midi dress paired with clean white sneakers. The navy blue base with delicate floral motifs creates visual interest while maintaining sophistication, making it suitable for daytime social events, casual dates, or weekend outings. The contrast between the dress's romantic aesthetic and the sneakers' sporty edge exemplifies modern mixed-styling approaches. The cool-toned color palette ensures versatility across seasons, while the comfortable silhouette and practical footwear choice reflect contemporary lifestyle needs. This combination successfully bridges the gap between dressed-up and dressed-down, creating an effortlessly chic appearance that's both approachable and stylish.
"""
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)}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7861)