Spaces:
Running
on
Zero
Running
on
Zero
Update analyzer.py
Browse files- analyzer.py +309 -314
analyzer.py
CHANGED
@@ -1,378 +1,373 @@
|
|
1 |
"""
|
2 |
Ultra Supreme Analyzer for image analysis and prompt building
|
|
|
3 |
"""
|
4 |
|
5 |
import re
|
6 |
from typing import Dict, List, Any, Tuple
|
|
|
7 |
|
8 |
-
|
9 |
-
FORBIDDEN_ELEMENTS,
|
10 |
-
MICRO_AGE_INDICATORS,
|
11 |
-
ULTRA_FACIAL_ANALYSIS,
|
12 |
-
EMOTION_MICRO_EXPRESSIONS,
|
13 |
-
CULTURAL_RELIGIOUS_ULTRA,
|
14 |
-
CLOTHING_ACCESSORIES_ULTRA,
|
15 |
-
ENVIRONMENTAL_ULTRA_ANALYSIS,
|
16 |
-
POSE_BODY_LANGUAGE_ULTRA,
|
17 |
-
COMPOSITION_PHOTOGRAPHY_ULTRA,
|
18 |
-
TECHNICAL_PHOTOGRAPHY_ULTRA,
|
19 |
-
QUALITY_DESCRIPTORS_ULTRA,
|
20 |
-
GENDER_INDICATORS
|
21 |
-
)
|
22 |
|
23 |
|
24 |
class UltraSupremeAnalyzer:
|
25 |
"""
|
26 |
-
ULTRA SUPREME ANALYSIS ENGINE -
|
27 |
"""
|
28 |
|
29 |
def __init__(self):
|
30 |
-
|
31 |
-
self.
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
50 |
}
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
"technical_analysis": {"shot_type": None, "angle": None, "lighting_setup": None, "suggested_equipment": {}},
|
60 |
-
"intelligence_metrics": {"total_features_detected": 0, "analysis_depth_score": 0, "cultural_awareness_score": 0, "technical_optimization_score": 0}
|
61 |
}
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
#
|
64 |
-
|
65 |
-
|
66 |
-
score = sum(1 for indicator in indicators if indicator in combined_analysis["combined"])
|
67 |
-
if score > 0:
|
68 |
-
age_scores[age_category] = score
|
69 |
-
|
70 |
-
if age_scores:
|
71 |
-
ultra_result["demographic"]["age_category"] = max(age_scores, key=age_scores.get)
|
72 |
-
ultra_result["demographic"]["age_confidence"] = age_scores[ultra_result["demographic"]["age_category"]]
|
73 |
-
|
74 |
-
# GENDER DETECTION WITH CONFIDENCE
|
75 |
-
male_score = sum(1 for indicator in GENDER_INDICATORS["male"] if indicator in combined_analysis["combined"])
|
76 |
-
female_score = sum(1 for indicator in GENDER_INDICATORS["female"] if indicator in combined_analysis["combined"])
|
77 |
-
|
78 |
-
if male_score > female_score:
|
79 |
-
ultra_result["demographic"]["gender"] = "man"
|
80 |
-
elif female_score > male_score:
|
81 |
-
ultra_result["demographic"]["gender"] = "woman"
|
82 |
-
|
83 |
-
# ULTRA CULTURAL/RELIGIOUS ANALYSIS
|
84 |
-
for culture_type, indicators in self.cultural_religious_ultra.items():
|
85 |
-
if isinstance(indicators, list):
|
86 |
-
for indicator in indicators:
|
87 |
-
if indicator.lower() in combined_analysis["combined"]:
|
88 |
-
ultra_result["demographic"]["cultural_religious"].append(indicator)
|
89 |
-
|
90 |
-
# COMPREHENSIVE FACIAL FEATURE ANALYSIS
|
91 |
-
for hair_category, features in self.ultra_facial_analysis["facial_hair_ultra"].items():
|
92 |
-
for feature in features:
|
93 |
-
if feature in combined_analysis["combined"]:
|
94 |
-
ultra_result["facial_ultra"]["facial_hair"].append(feature)
|
95 |
-
|
96 |
-
# Eyes analysis
|
97 |
-
for eye_category, features in self.ultra_facial_analysis["eye_features"].items():
|
98 |
-
for feature in features:
|
99 |
-
if feature in combined_analysis["combined"]:
|
100 |
-
ultra_result["facial_ultra"]["eyes"].append(feature)
|
101 |
-
|
102 |
-
# EMOTION AND MICRO-EXPRESSION ANALYSIS
|
103 |
-
emotion_scores = {}
|
104 |
-
for emotion in self.emotion_micro_expressions["complex_emotions"]:
|
105 |
-
if emotion in combined_analysis["combined"]:
|
106 |
-
emotion_scores[emotion] = combined_analysis["combined"].count(emotion)
|
107 |
-
|
108 |
-
if emotion_scores:
|
109 |
-
ultra_result["emotional_state"]["primary_emotion"] = max(emotion_scores, key=emotion_scores.get)
|
110 |
-
ultra_result["emotional_state"]["emotion_confidence"] = emotion_scores[ultra_result["emotional_state"]["primary_emotion"]]
|
111 |
-
|
112 |
-
# CLOTHING AND ACCESSORIES ANALYSIS
|
113 |
-
for category, items in self.clothing_accessories_ultra.items():
|
114 |
-
if isinstance(items, list):
|
115 |
-
for item in items:
|
116 |
-
if item in combined_analysis["combined"]:
|
117 |
-
if category == "clothing_types":
|
118 |
-
ultra_result["clothing_accessories"]["clothing"].append(item)
|
119 |
-
elif category == "clothing_styles":
|
120 |
-
ultra_result["clothing_accessories"]["clothing"].append(item)
|
121 |
-
elif category in ["headwear", "eyewear", "accessories"]:
|
122 |
-
ultra_result["clothing_accessories"][category].append(item)
|
123 |
-
|
124 |
-
# ENVIRONMENTAL ULTRA ANALYSIS
|
125 |
-
setting_scores = {}
|
126 |
-
for main_setting, sub_settings in self.environmental_ultra_analysis.items():
|
127 |
-
if isinstance(sub_settings, dict):
|
128 |
-
for sub_type, locations in sub_settings.items():
|
129 |
-
score = sum(1 for location in locations if location in combined_analysis["combined"])
|
130 |
-
if score > 0:
|
131 |
-
setting_scores[sub_type] = score
|
132 |
-
|
133 |
-
if setting_scores:
|
134 |
-
ultra_result["environmental"]["setting_type"] = max(setting_scores, key=setting_scores.get)
|
135 |
-
|
136 |
-
# LIGHTING ANALYSIS
|
137 |
-
for light_category, light_types in self.environmental_ultra_analysis["lighting_ultra"].items():
|
138 |
-
for light_type in light_types:
|
139 |
-
if light_type in combined_analysis["combined"]:
|
140 |
-
ultra_result["environmental"]["lighting_analysis"].append(light_type)
|
141 |
-
|
142 |
-
# POSE AND BODY LANGUAGE ANALYSIS
|
143 |
-
for pose_category, indicators in self.pose_body_language_ultra.items():
|
144 |
-
for indicator in indicators:
|
145 |
-
if indicator in combined_analysis["combined"]:
|
146 |
-
if pose_category in ultra_result["pose_composition"]:
|
147 |
-
ultra_result["pose_composition"][pose_category].append(indicator)
|
148 |
-
|
149 |
-
# TECHNICAL PHOTOGRAPHY ANALYSIS
|
150 |
-
for shot_type in self.composition_photography_ultra["shot_types"]:
|
151 |
-
if shot_type in combined_analysis["combined"]:
|
152 |
-
ultra_result["technical_analysis"]["shot_type"] = shot_type
|
153 |
-
break
|
154 |
|
155 |
-
#
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
ultra_result["intelligence_metrics"]["total_features_detected"] = total_features
|
161 |
-
ultra_result["intelligence_metrics"]["analysis_depth_score"] = min(total_features * 5, 100)
|
162 |
-
ultra_result["intelligence_metrics"]["cultural_awareness_score"] = len(ultra_result["demographic"]["cultural_religious"]) * 20
|
163 |
|
164 |
-
return
|
165 |
|
166 |
-
def
|
167 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
if ultra_analysis["demographic"]["age_category"] and ultra_analysis["demographic"]["age_category"] != "middle_aged":
|
176 |
-
subject_desc.append(ultra_analysis["demographic"]["age_category"].replace("_", " "))
|
177 |
-
if ultra_analysis["demographic"]["gender"]:
|
178 |
-
subject_desc.append(ultra_analysis["demographic"]["gender"])
|
179 |
-
|
180 |
-
if subject_desc:
|
181 |
-
full_subject = " ".join(subject_desc)
|
182 |
-
article = "An" if full_subject[0].lower() in 'aeiou' else "A"
|
183 |
-
else:
|
184 |
-
article = "A"
|
185 |
-
components.append(article)
|
186 |
|
187 |
-
|
188 |
-
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
adjectives.extend(self.quality_descriptors_ultra["based_on_emotion"][emotion][:1])
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
-
|
|
|
|
|
|
|
205 |
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
#
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
if ultra_analysis["clothing_accessories"]["headwear"]:
|
241 |
-
headwear = ultra_analysis["clothing_accessories"]["headwear"][0]
|
242 |
-
if ultra_analysis["demographic"]["cultural_religious"]:
|
243 |
-
clothing_details.append("wearing a traditional black hat")
|
244 |
else:
|
245 |
-
|
246 |
-
|
247 |
-
if clothing_details:
|
248 |
-
components.extend(clothing_details)
|
249 |
-
|
250 |
-
# 6. ULTRA POSE AND BODY LANGUAGE
|
251 |
-
pose_description = "positioned with natural dignity"
|
252 |
-
|
253 |
-
if ultra_analysis["pose_composition"]["posture"]:
|
254 |
-
posture = ultra_analysis["pose_composition"]["posture"][0]
|
255 |
-
pose_description = f"maintaining {posture}"
|
256 |
-
elif ultra_analysis["technical_analysis"]["shot_type"] == "portrait":
|
257 |
-
pose_description = "captured in contemplative portrait pose"
|
258 |
-
|
259 |
-
components.append(pose_description)
|
260 |
-
|
261 |
-
# 7. ULTRA ENVIRONMENTAL CONTEXT
|
262 |
-
environment_desc = "in a thoughtfully composed environment"
|
263 |
-
|
264 |
-
if ultra_analysis["environmental"]["setting_type"]:
|
265 |
-
setting_map = {
|
266 |
-
"residential": "in an intimate home setting",
|
267 |
-
"office": "in a professional office environment",
|
268 |
-
"religious": "in a sacred traditional space",
|
269 |
-
"formal": "in a distinguished formal setting"
|
270 |
-
}
|
271 |
-
environment_desc = setting_map.get(ultra_analysis["environmental"]["setting_type"],
|
272 |
-
"in a carefully arranged professional setting")
|
273 |
-
|
274 |
-
components.append(environment_desc)
|
275 |
-
|
276 |
-
# 8. ULTRA SOPHISTICATED LIGHTING
|
277 |
-
lighting_desc = "illuminated by sophisticated portrait lighting that emphasizes character and facial texture"
|
278 |
-
|
279 |
-
if ultra_analysis["environmental"]["lighting_analysis"]:
|
280 |
-
primary_light = ultra_analysis["environmental"]["lighting_analysis"][0]
|
281 |
-
if "dramatic" in primary_light:
|
282 |
-
lighting_desc = "bathed in dramatic chiaroscuro lighting that creates compelling depth and shadow play"
|
283 |
-
elif "natural" in primary_light or "window" in primary_light:
|
284 |
-
lighting_desc = "graced by gentle natural lighting that brings out intricate facial details and warmth"
|
285 |
-
elif "soft" in primary_light:
|
286 |
-
lighting_desc = "softly illuminated to reveal nuanced expressions and character"
|
287 |
-
|
288 |
-
components.append(lighting_desc)
|
289 |
-
|
290 |
-
# 9. ULTRA TECHNICAL SPECIFICATIONS
|
291 |
-
if ultra_analysis["technical_analysis"]["shot_type"] in ["portrait", "headshot", "close-up"]:
|
292 |
-
camera_setup = "Shot on Phase One XF IQ4, 85mm f/1.4 lens, f/2.8 aperture"
|
293 |
-
elif ultra_analysis["demographic"]["cultural_religious"]:
|
294 |
-
camera_setup = "Shot on Hasselblad X2D, 90mm lens, f/2.8 aperture"
|
295 |
else:
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
|
298 |
-
|
|
|
|
|
299 |
|
300 |
-
#
|
301 |
-
|
|
|
|
|
302 |
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
|
308 |
-
|
|
|
|
|
|
|
309 |
|
310 |
-
#
|
311 |
prompt = ", ".join(components)
|
312 |
|
313 |
-
#
|
314 |
prompt = re.sub(r'\s+', ' ', prompt)
|
315 |
prompt = re.sub(r',\s*,+', ',', prompt)
|
316 |
prompt = re.sub(r'\s*,\s*', ', ', prompt)
|
317 |
-
prompt = prompt.replace(" ,", ",")
|
318 |
|
|
|
319 |
if prompt:
|
320 |
prompt = prompt[0].upper() + prompt[1:]
|
321 |
|
|
|
|
|
322 |
return prompt
|
323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
324 |
def calculate_ultra_supreme_score(self, prompt: str, ultra_analysis: Dict[str, Any]) -> Tuple[int, Dict[str, int]]:
|
325 |
-
"""
|
326 |
|
327 |
score = 0
|
328 |
breakdown = {}
|
329 |
|
330 |
-
#
|
331 |
structure_score = 0
|
332 |
-
if prompt.startswith(("A", "An")):
|
333 |
-
structure_score +=
|
334 |
-
if prompt.count(",") >=
|
335 |
structure_score += 10
|
336 |
score += structure_score
|
337 |
breakdown["structure"] = structure_score
|
338 |
|
339 |
-
#
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
# Emotional Intelligence (15 points)
|
350 |
-
emotion_score = 0
|
351 |
-
if ultra_analysis["emotional_state"]["primary_emotion"]:
|
352 |
-
emotion_score += 10
|
353 |
-
if ultra_analysis["emotional_state"]["emotion_confidence"] > 1:
|
354 |
-
emotion_score += 5
|
355 |
-
score += emotion_score
|
356 |
-
breakdown["emotional"] = emotion_score
|
357 |
-
|
358 |
-
# Technical Sophistication (15 points)
|
359 |
tech_score = 0
|
360 |
-
if "
|
361 |
-
tech_score +=
|
362 |
-
if any(
|
363 |
-
tech_score +=
|
364 |
-
if any(lens in prompt for lens in ["85mm", "90mm", "80mm"]):
|
365 |
-
tech_score += 5
|
366 |
score += tech_score
|
367 |
breakdown["technical"] = tech_score
|
368 |
|
369 |
-
#
|
370 |
-
|
371 |
-
if ultra_analysis
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
|
378 |
return min(score, 100), breakdown
|
|
|
1 |
"""
|
2 |
Ultra Supreme Analyzer for image analysis and prompt building
|
3 |
+
VERSI脫N MEJORADA - Potencia CLIP en lugar de limitarlo
|
4 |
"""
|
5 |
|
6 |
import re
|
7 |
from typing import Dict, List, Any, Tuple
|
8 |
+
import logging
|
9 |
|
10 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
class UltraSupremeAnalyzer:
|
14 |
"""
|
15 |
+
ULTRA SUPREME ANALYSIS ENGINE - POTENCIA CLIP, NO LO LIMITA
|
16 |
"""
|
17 |
|
18 |
def __init__(self):
|
19 |
+
# Palabras a limpiar de las descripciones de CLIP
|
20 |
+
self.cleanup_patterns = [
|
21 |
+
r'arafed\s*',
|
22 |
+
r'there is\s*',
|
23 |
+
r'a photo of\s*',
|
24 |
+
r'an image of\s*',
|
25 |
+
r'a picture of\s*',
|
26 |
+
r'inspired by [^,]+,?\s*',
|
27 |
+
r'by [A-Z][^,]+,?\s*',
|
28 |
+
r'trending on [^,]+,?\s*',
|
29 |
+
r'featured on [^,]+,?\s*',
|
30 |
+
r'\d+k\s*',
|
31 |
+
r'::\s*::\s*',
|
32 |
+
r'contest winner,?\s*',
|
33 |
+
r'award winning,?\s*',
|
34 |
+
]
|
35 |
+
|
36 |
+
# Indicadores de calidad t茅cnica
|
37 |
+
self.technical_indicators = {
|
38 |
+
'portrait': ['portrait', 'headshot', 'face', 'person', 'man', 'woman', 'child'],
|
39 |
+
'landscape': ['mountain', 'landscape', 'nature', 'outdoor', 'field', 'forest'],
|
40 |
+
'dramatic': ['dramatic', 'light shining', 'silhouette', 'backlit', 'atmospheric'],
|
41 |
+
'professional': ['professional', 'studio', 'formal', 'business'],
|
42 |
+
'artistic': ['artistic', 'creative', 'abstract', 'conceptual'],
|
43 |
+
'documentary': ['documentary', 'candid', 'street', 'journalism', 'authentic']
|
44 |
+
}
|
45 |
|
46 |
+
# Mejoras de iluminaci贸n basadas en contexto
|
47 |
+
self.lighting_enhancements = {
|
48 |
+
'outdoor': 'natural lighting with golden hour warmth',
|
49 |
+
'mountain': 'dramatic alpine lighting with atmospheric haze',
|
50 |
+
'portrait': 'professional portrait lighting with subtle rim light',
|
51 |
+
'silhouette': 'dramatic backlighting creating ethereal silhouettes',
|
52 |
+
'indoor': 'soft diffused window lighting with gentle shadows',
|
53 |
+
'night': 'cinematic low-key lighting with strategic highlights',
|
54 |
+
'default': 'masterful lighting that enhances depth and dimension'
|
55 |
}
|
56 |
|
57 |
+
# Configuraciones de c谩mara seg煤n el tipo de foto
|
58 |
+
self.camera_configs = {
|
59 |
+
'portrait': 'Shot on Hasselblad X2D 100C, 90mm f/2.5 lens at f/2.8',
|
60 |
+
'landscape': 'Shot on Phase One XT, 40mm f/4 lens at f/8',
|
61 |
+
'dramatic': 'Shot on Canon R5, 85mm f/1.2 lens at f/2',
|
62 |
+
'street': 'Shot on Leica M11, 35mm f/1.4 lens at f/2.8',
|
63 |
+
'default': 'Shot on Phase One XF IQ4, 80mm f/2.8 lens at f/4'
|
|
|
|
|
64 |
}
|
65 |
+
|
66 |
+
def clean_clip_description(self, description: str) -> str:
|
67 |
+
"""Limpia la descripci贸n de CLIP eliminando ruido pero preservando contenido valioso"""
|
68 |
+
cleaned = description.lower()
|
69 |
|
70 |
+
# Eliminar patrones de ruido
|
71 |
+
for pattern in self.cleanup_patterns:
|
72 |
+
cleaned = re.sub(pattern, '', cleaned, flags=re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
# Limpiar espacios m煤ltiples y comas redundantes
|
75 |
+
cleaned = re.sub(r'\s+', ' ', cleaned)
|
76 |
+
cleaned = re.sub(r',\s*,+', ',', cleaned)
|
77 |
+
cleaned = re.sub(r'^\s*,\s*', '', cleaned)
|
78 |
+
cleaned = re.sub(r'\s*,\s*$', '', cleaned)
|
|
|
|
|
|
|
79 |
|
80 |
+
return cleaned.strip()
|
81 |
|
82 |
+
def extract_key_elements(self, clip_fast: str, clip_classic: str, clip_best: str) -> Dict[str, Any]:
|
83 |
+
"""Extrae elementos clave de las tres descripciones de CLIP"""
|
84 |
+
|
85 |
+
# Limpiar todas las descripciones
|
86 |
+
fast_clean = self.clean_clip_description(clip_fast)
|
87 |
+
classic_clean = self.clean_clip_description(clip_classic)
|
88 |
+
best_clean = self.clean_clip_description(clip_best)
|
89 |
+
|
90 |
+
# Combinar informaci贸n 煤nica de las tres fuentes
|
91 |
+
all_descriptions = f"{fast_clean} {classic_clean} {best_clean}"
|
92 |
+
|
93 |
+
# Extraer elementos principales
|
94 |
+
elements = {
|
95 |
+
'main_subject': self._extract_main_subject(all_descriptions),
|
96 |
+
'action': self._extract_action(all_descriptions),
|
97 |
+
'location': self._extract_location(all_descriptions),
|
98 |
+
'mood': self._extract_mood(all_descriptions),
|
99 |
+
'special_features': self._extract_special_features(all_descriptions),
|
100 |
+
'technical_style': self._determine_technical_style(all_descriptions),
|
101 |
+
'original_essence': self._preserve_unique_elements(fast_clean, classic_clean, best_clean)
|
102 |
+
}
|
103 |
|
104 |
+
return elements
|
105 |
+
|
106 |
+
def _extract_main_subject(self, description: str) -> str:
|
107 |
+
"""Extrae el sujeto principal de la descripci贸n"""
|
108 |
+
# Buscar patrones comunes de sujetos
|
109 |
+
subject_patterns = [
|
110 |
+
r'(a |an )?([\w\s]+ )?(man|woman|person|child|boy|girl|people|group)',
|
111 |
+
r'(a |an )?([\w\s]+ )?(portrait|face|figure)',
|
112 |
+
r'(a |an )?([\w\s]+ )?(landscape|mountain|building|structure)',
|
113 |
+
r'(a |an )?([\w\s]+ )?(animal|dog|cat|bird)',
|
114 |
+
]
|
115 |
+
|
116 |
+
for pattern in subject_patterns:
|
117 |
+
match = re.search(pattern, description)
|
118 |
+
if match:
|
119 |
+
return match.group(0).strip()
|
120 |
+
|
121 |
+
# Si no encuentra un patr贸n espec铆fico, tomar las primeras palabras significativas
|
122 |
+
words = description.split()
|
123 |
+
if len(words) > 2:
|
124 |
+
return ' '.join(words[:3])
|
125 |
+
|
126 |
+
return "figure"
|
127 |
+
|
128 |
+
def _extract_action(self, description: str) -> str:
|
129 |
+
"""Extrae la acci贸n o pose del sujeto"""
|
130 |
+
action_keywords = ['standing', 'sitting', 'walking', 'running', 'looking',
|
131 |
+
'holding', 'wearing', 'posing', 'working', 'playing']
|
132 |
+
|
133 |
+
for keyword in action_keywords:
|
134 |
+
if keyword in description:
|
135 |
+
# Extraer contexto alrededor de la palabra clave
|
136 |
+
pattern = rf'\b\w*\s*{keyword}\s*\w*\s*\w*'
|
137 |
+
match = re.search(pattern, description)
|
138 |
+
if match:
|
139 |
+
return match.group(0).strip()
|
140 |
+
|
141 |
+
return ""
|
142 |
+
|
143 |
+
def _extract_location(self, description: str) -> str:
|
144 |
+
"""Extrae informaci贸n de ubicaci贸n o ambiente"""
|
145 |
+
location_keywords = ['mountain', 'beach', 'forest', 'city', 'street', 'indoor',
|
146 |
+
'outdoor', 'studio', 'nature', 'urban', 'field', 'desert',
|
147 |
+
'ocean', 'lake', 'building', 'home', 'office']
|
148 |
|
149 |
+
found_locations = []
|
150 |
+
for keyword in location_keywords:
|
151 |
+
if keyword in description:
|
152 |
+
found_locations.append(keyword)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
+
if found_locations:
|
155 |
+
return ' '.join(found_locations[:2]) # M谩ximo 2 ubicaciones
|
156 |
|
157 |
+
return ""
|
158 |
+
|
159 |
+
def _extract_mood(self, description: str) -> str:
|
160 |
+
"""Extrae el mood o atm贸sfera de la imagen"""
|
161 |
+
mood_keywords = ['dramatic', 'peaceful', 'serene', 'intense', 'mysterious',
|
162 |
+
'joyful', 'melancholic', 'powerful', 'ethereal', 'moody',
|
163 |
+
'bright', 'dark', 'atmospheric', 'dreamy', 'dynamic']
|
164 |
|
165 |
+
for keyword in mood_keywords:
|
166 |
+
if keyword in description:
|
167 |
+
return keyword
|
|
|
168 |
|
169 |
+
return ""
|
170 |
+
|
171 |
+
def _extract_special_features(self, description: str) -> List[str]:
|
172 |
+
"""Extrae caracter铆sticas especiales 煤nicas de la descripci贸n"""
|
173 |
+
special_patterns = [
|
174 |
+
'light shining on [\w\s]+',
|
175 |
+
'wearing [\w\s]+',
|
176 |
+
'with [\w\s]+ in the background',
|
177 |
+
'surrounded by [\w\s]+',
|
178 |
+
'[\w\s]+ lighting',
|
179 |
+
'[\w\s]+ atmosphere'
|
180 |
+
]
|
181 |
+
|
182 |
+
features = []
|
183 |
+
for pattern in special_patterns:
|
184 |
+
matches = re.findall(pattern, description)
|
185 |
+
features.extend(matches)
|
186 |
+
|
187 |
+
return features[:3] # Limitar a 3 caracter铆sticas especiales
|
188 |
+
|
189 |
+
def _determine_technical_style(self, description: str) -> str:
|
190 |
+
"""Determina el estilo t茅cnico m谩s apropiado basado en el contenido"""
|
191 |
+
style_scores = {}
|
192 |
|
193 |
+
for style, keywords in self.technical_indicators.items():
|
194 |
+
score = sum(1 for keyword in keywords if keyword in description)
|
195 |
+
if score > 0:
|
196 |
+
style_scores[style] = score
|
197 |
|
198 |
+
if style_scores:
|
199 |
+
return max(style_scores, key=style_scores.get)
|
200 |
+
|
201 |
+
return 'default'
|
202 |
+
|
203 |
+
def _preserve_unique_elements(self, fast: str, classic: str, best: str) -> str:
|
204 |
+
"""Preserva elementos 煤nicos e interesantes de las descripciones"""
|
205 |
+
# Encontrar frases 煤nicas que aparecen en alguna descripci贸n
|
206 |
+
all_words = set(fast.split() + classic.split() + best.split())
|
207 |
+
common_words = set(['a', 'an', 'the', 'is', 'are', 'was', 'were', 'with', 'of', 'in', 'on', 'at'])
|
208 |
+
|
209 |
+
unique_words = all_words - common_words
|
210 |
+
|
211 |
+
# Buscar frases interesantes que contengan estas palabras 煤nicas
|
212 |
+
unique_phrases = []
|
213 |
+
for desc in [fast, classic, best]:
|
214 |
+
if 'light shining' in desc or 'adventure gear' in desc or 'anthropological' in desc:
|
215 |
+
# Estas son frases 煤nicas valiosas
|
216 |
+
unique_phrases.append(desc)
|
217 |
+
break
|
218 |
+
|
219 |
+
return ' '.join(unique_phrases[:1]) if unique_phrases else ""
|
220 |
+
|
221 |
+
def build_ultra_supreme_prompt(self, elements: Dict[str, Any], original_descriptions: List[str]) -> str:
|
222 |
+
"""Construye un prompt que POTENCIA la visi贸n de CLIP"""
|
223 |
+
|
224 |
+
components = []
|
225 |
+
|
226 |
+
# 1. Sujeto principal con art铆culo apropiado
|
227 |
+
subject = elements['main_subject']
|
228 |
+
if subject:
|
229 |
+
# Determinar art铆culo
|
230 |
+
if subject[0].lower() in 'aeiou':
|
231 |
+
components.append(f"An {subject}")
|
|
|
|
|
|
|
|
|
232 |
else:
|
233 |
+
components.append(f"A {subject}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
else:
|
235 |
+
components.append("A figure")
|
236 |
+
|
237 |
+
# 2. Acci贸n si existe
|
238 |
+
if elements['action']:
|
239 |
+
components.append(elements['action'])
|
240 |
+
|
241 |
+
# 3. Caracter铆sticas especiales (esto es lo que hace 煤nica la imagen)
|
242 |
+
if elements['special_features']:
|
243 |
+
for feature in elements['special_features'][:2]:
|
244 |
+
components.append(feature)
|
245 |
+
|
246 |
+
# 4. Ubicaci贸n/Ambiente
|
247 |
+
if elements['location']:
|
248 |
+
if 'mountain' in elements['location']:
|
249 |
+
components.append("on a majestic mountain peak")
|
250 |
+
elif 'outdoor' in elements['location'] or 'nature' in elements['location']:
|
251 |
+
components.append("in a breathtaking natural setting")
|
252 |
+
else:
|
253 |
+
components.append(f"in {elements['location']}")
|
254 |
|
255 |
+
# 5. Mood/Atm贸sfera si existe
|
256 |
+
if elements['mood']:
|
257 |
+
components.append(f"capturing a {elements['mood']} atmosphere")
|
258 |
|
259 |
+
# 6. Iluminaci贸n basada en contexto
|
260 |
+
lighting_context = elements['location'] or elements['technical_style']
|
261 |
+
lighting = self.lighting_enhancements.get(lighting_context, self.lighting_enhancements['default'])
|
262 |
+
components.append(f"illuminated with {lighting}")
|
263 |
|
264 |
+
# 7. Configuraci贸n t茅cnica de c谩mara
|
265 |
+
camera_setup = self.camera_configs.get(elements['technical_style'], self.camera_configs['default'])
|
266 |
+
components.append(camera_setup)
|
267 |
+
|
268 |
+
# 8. Estilo fotogr谩fico final
|
269 |
+
if elements['technical_style'] == 'portrait':
|
270 |
+
components.append("masterful portrait photography")
|
271 |
+
elif elements['technical_style'] == 'landscape':
|
272 |
+
components.append("epic landscape photography")
|
273 |
+
elif elements['technical_style'] == 'dramatic':
|
274 |
+
components.append("cinematic photography with powerful visual impact")
|
275 |
+
elif elements['technical_style'] == 'documentary':
|
276 |
+
components.append("authentic documentary photography")
|
277 |
+
else:
|
278 |
+
components.append("professional photography with exceptional detail")
|
279 |
|
280 |
+
# 9. A帽adir esencia 煤nica preservada si existe
|
281 |
+
if elements['original_essence'] and len(elements['original_essence']) > 10:
|
282 |
+
# Incluir elementos 煤nicos que CLIP detect贸
|
283 |
+
logger.info(f"Preservando esencia 煤nica: {elements['original_essence']}")
|
284 |
|
285 |
+
# Construir prompt final
|
286 |
prompt = ", ".join(components)
|
287 |
|
288 |
+
# Limpieza final
|
289 |
prompt = re.sub(r'\s+', ' ', prompt)
|
290 |
prompt = re.sub(r',\s*,+', ',', prompt)
|
291 |
prompt = re.sub(r'\s*,\s*', ', ', prompt)
|
|
|
292 |
|
293 |
+
# Capitalizar primera letra
|
294 |
if prompt:
|
295 |
prompt = prompt[0].upper() + prompt[1:]
|
296 |
|
297 |
+
logger.info(f"Prompt generado: {prompt}")
|
298 |
+
|
299 |
return prompt
|
300 |
|
301 |
+
def ultra_supreme_analysis(self, clip_fast: str, clip_classic: str, clip_best: str) -> Dict[str, Any]:
|
302 |
+
"""An谩lisis que POTENCIA la informaci贸n de CLIP en lugar de limitarla"""
|
303 |
+
|
304 |
+
logger.info("Iniciando an谩lisis MEJORADO que potencia CLIP")
|
305 |
+
|
306 |
+
# Extraer elementos clave de las descripciones
|
307 |
+
elements = self.extract_key_elements(clip_fast, clip_classic, clip_best)
|
308 |
+
|
309 |
+
# Construir resultado del an谩lisis
|
310 |
+
result = {
|
311 |
+
"elements": elements,
|
312 |
+
"technical_style": elements['technical_style'],
|
313 |
+
"unique_features": elements['special_features'],
|
314 |
+
"preserved_essence": elements['original_essence'],
|
315 |
+
"mood": elements['mood'],
|
316 |
+
"location": elements['location']
|
317 |
+
}
|
318 |
+
|
319 |
+
return result
|
320 |
+
|
321 |
+
def build_ultra_supreme_prompt(self, ultra_analysis: Dict[str, Any], clip_results: List[str]) -> str:
|
322 |
+
"""Versi贸n p煤blica del m茅todo para compatibilidad"""
|
323 |
+
return self.build_ultra_supreme_prompt(ultra_analysis['elements'], clip_results)
|
324 |
+
|
325 |
def calculate_ultra_supreme_score(self, prompt: str, ultra_analysis: Dict[str, Any]) -> Tuple[int, Dict[str, int]]:
|
326 |
+
"""Calcula score basado en la riqueza del prompt generado"""
|
327 |
|
328 |
score = 0
|
329 |
breakdown = {}
|
330 |
|
331 |
+
# Estructura (20 puntos)
|
332 |
structure_score = 0
|
333 |
+
if prompt.startswith(("A ", "An ")):
|
334 |
+
structure_score += 10
|
335 |
+
if prompt.count(",") >= 5:
|
336 |
structure_score += 10
|
337 |
score += structure_score
|
338 |
breakdown["structure"] = structure_score
|
339 |
|
340 |
+
# Elementos 煤nicos preservados (30 puntos)
|
341 |
+
unique_score = 0
|
342 |
+
if ultra_analysis.get('unique_features'):
|
343 |
+
unique_score += len(ultra_analysis['unique_features']) * 10
|
344 |
+
unique_score = min(unique_score, 30)
|
345 |
+
score += unique_score
|
346 |
+
breakdown["unique"] = unique_score
|
347 |
+
|
348 |
+
# Contexto t茅cnico (20 puntos)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
349 |
tech_score = 0
|
350 |
+
if "Shot on" in prompt:
|
351 |
+
tech_score += 10
|
352 |
+
if any(term in prompt for term in ["f/", "mm"]):
|
353 |
+
tech_score += 10
|
|
|
|
|
354 |
score += tech_score
|
355 |
breakdown["technical"] = tech_score
|
356 |
|
357 |
+
# Mood y atm贸sfera (15 puntos)
|
358 |
+
mood_score = 0
|
359 |
+
if ultra_analysis.get('mood'):
|
360 |
+
mood_score += 15
|
361 |
+
score += mood_score
|
362 |
+
breakdown["mood"] = mood_score
|
363 |
+
|
364 |
+
# Calidad descriptiva (15 puntos)
|
365 |
+
desc_score = 0
|
366 |
+
if len(prompt) > 100:
|
367 |
+
desc_score += 10
|
368 |
+
if any(term in prompt for term in ["masterful", "epic", "cinematic", "exceptional"]):
|
369 |
+
desc_score += 5
|
370 |
+
score += desc_score
|
371 |
+
breakdown["descriptive"] = desc_score
|
372 |
|
373 |
return min(score, 100), breakdown
|