Humphreykowl commited on
Commit
891265c
·
verified ·
1 Parent(s): a507727

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -462
app.py CHANGED
@@ -1,97 +1,20 @@
1
- import gradio as gr
2
- import os
3
- import sys
4
- import logging
5
- from PIL import Image
6
- import numpy as np
7
- import time
8
- import random
9
- import warnings
10
- import json
11
-
12
- # 设置环境变量解决 OpenMP 问题
13
- os.environ['OMP_NUM_THREADS'] = '1'
14
- os.environ['MKL_NUM_THREADS'] = '1'
15
-
16
- # 抑制警告
17
- warnings.filterwarnings("ignore")
18
-
19
- # 设置日志
20
- logging.basicConfig(level=logging.INFO)
21
- logger = logging.getLogger(__name__)
22
-
23
- # 检查模型管理器是否存在
24
- try:
25
- from models.model_manager import ModelManager
26
- MODELS_AVAILABLE = True
27
- logger.info("模型管理器导入成功")
28
- except ImportError as e:
29
- logger.warning(f"模型管理器导入失败: {e}")
30
- logger.info("将使用简化版本运行")
31
- MODELS_AVAILABLE = False
32
- ModelManager = None
33
-
34
- class SimpleModelManager:
35
- """简化的模型管理器,用于演示模式"""
36
- def __init__(self):
37
- self.device = "cpu"
38
- logger.info("使用简化模型管理器")
39
-
40
- def generate_caption(self, image):
41
- return "这是一件时尚的服装设计,具有现代感和优雅的风格。"
42
-
43
- def analyze_style(self, image):
44
- return {"时尚潮流": 0.8, "休闲风": 0.6, "优雅风": 0.4}
45
-
46
- def generate_image(self, prompt, **kwargs):
47
- # 创建占位图像
48
- width, height = kwargs.get('width', 512), kwargs.get('height', 512)
49
- color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
50
- img = Image.new('RGB', (width, height), color=color)
51
- return img
52
-
53
- def cleanup(self):
54
- pass
55
-
56
- # 全局状态管理
57
- class AppState:
58
- def __init__(self):
59
- self.current_image = None
60
- self.current_caption = ""
61
- self.current_analysis = {}
62
- self.current_suggestions = {}
63
- self.design_images = []
64
- self.selected_suggestion = ""
65
- self.selected_design = ""
66
-
67
- # 初始化
68
- if MODELS_AVAILABLE:
69
- try:
70
- model_manager = ModelManager()
71
- logger.info("使用完整模型管理器")
72
- except Exception as e:
73
- logger.error(f"初始化完整模型管理器失败: {e}")
74
- model_manager = SimpleModelManager()
75
- else:
76
- model_manager = SimpleModelManager()
77
-
78
- app_state = AppState()
79
 
80
  def upload_and_analyze(image_path, progress=gr.Progress()):
81
- """完整的图片上传和分析流程"""
82
  try:
 
 
 
83
  if image_path is None:
84
  return {}, {}, gr.Radio(choices=[]), gr.Gallery(value=[])
85
 
86
  progress(0.1, desc="加载图片...")
87
-
88
- # 打开并保存图片
89
  image = Image.open(image_path).convert('RGB')
90
  app_state.current_image = image
91
 
 
92
  progress(0.2, desc="生成图像描述...")
93
-
94
- # 步骤1: 使用模型生成图像描述
95
  try:
96
  caption = model_manager.generate_caption(image)
97
  app_state.current_caption = caption
@@ -101,29 +24,23 @@ def upload_and_analyze(image_path, progress=gr.Progress()):
101
  caption = "时尚服装设计"
102
  app_state.current_caption = caption
103
 
104
- progress(0.4, desc="分析服装风格...")
105
-
106
  # 步骤2: 使用CLIP模型分析风格
 
107
  try:
108
- if hasattr(model_manager, 'analyze_style'):
109
- style_scores = model_manager.analyze_style(image)
110
- else:
111
- # 回退到基于描述的分析
112
- style_scores = analyze_style_from_caption(caption)
113
  logger.info(f"风格分析结果: {style_scores}")
114
  except Exception as e:
115
  logger.error(f"风格分析失败: {e}")
116
- style_scores = {"时尚潮流": 0.8, "休闲风": 0.6}
117
-
118
- progress(0.6, desc="提取颜色信息...")
119
 
120
  # 步骤3: 提取颜色信息
 
121
  colors = extract_dominant_colors(image)
122
 
 
123
  progress(0.8, desc="生成完整分析...")
124
-
125
- # 步骤4: 整合分析结果
126
- analysis_result = {
127
  "图像描述": caption,
128
  "主要风格": max(style_scores.keys(), key=style_scores.get),
129
  "风格评分": style_scores,
@@ -132,239 +49,76 @@ def upload_and_analyze(image_path, progress=gr.Progress()):
132
  "分析时间": time.strftime("%Y-%m-%d %H:%M:%S")
133
  }
134
 
135
- # 步骤5: 基于模型分析结果生成个性化建议
136
- suggestions = generate_personalized_suggestions_v2(
137
  caption, style_scores, colors
138
  )
139
 
140
- # 保存状态
141
- app_state.current_analysis = analysis_result
142
- app_state.current_suggestions = suggestions
143
-
144
  # 创建建议选项
145
- choices = list(suggestions.keys())
146
 
147
  progress(1.0, desc="分析完成")
148
 
149
  return (
150
- analysis_result,
151
- suggestions,
152
  gr.Radio(choices=choices, value=choices[0] if choices else None),
153
  gr.Gallery(value=[]) # 清空之前的设计
154
  )
155
 
156
  except Exception as e:
157
- error_result = {"错误": f"分析失败: {str(e)}"}
158
  logger.error(f"上传分析失败: {e}")
159
- return error_result, {}, gr.Radio(choices=[]), gr.Gallery(value=[])
160
-
161
- def analyze_style_from_caption(caption):
162
- """基于描述分析风格(回退方案)"""
163
- caption_lower = caption.lower()
164
-
165
- style_keywords = {
166
- "商务正装": ["suit", "formal", "business", "office", "professional", "tie", "blazer", "西装", "正装", "商务"],
167
- "休闲风": ["casual", "relaxed", "comfortable", "everyday", "jeans", "t-shirt", "休闲", "日常"],
168
- "运动风": ["sport", "athletic", "gym", "fitness", "running", "training", "运动", "健身"],
169
- "时尚潮流": ["fashion", "trendy", "stylish", "modern", "chic", "designer", "时尚", "潮流"],
170
- "复古风": ["vintage", "retro", "classic", "traditional", "old-fashioned", "复古", "经典"],
171
- "街头风": ["street", "urban", "hip-hop", "cool", "edgy", "街头", "嘻哈"],
172
- "优雅风": ["elegant", "sophisticated", "graceful", "refined", "classy", "优雅", "高贵"]
173
- }
174
-
175
- style_scores = {}
176
- for style, keywords in style_keywords.items():
177
- score = sum(0.2 for keyword in keywords if keyword in caption_lower)
178
- if score > 0:
179
- style_scores[style] = min(score, 1.0)
180
-
181
- # 如果没有匹配,给默认评分
182
- if not style_scores:
183
- style_scores = {"休闲风": 0.7, "时尚潮流": 0.5}
184
-
185
- return style_scores
186
-
187
- def extract_dominant_colors(image):
188
- """提取图像主要颜色"""
189
- try:
190
- from sklearn.cluster import KMeans
191
-
192
- # 调整图像大小以提高处理速度
193
- image = image.resize((150, 150))
194
-
195
- # 转换为numpy数组
196
- img_array = np.array(image)
197
- pixels = img_array.reshape(-1, 3)
198
-
199
- # 使用KMeans聚类找到主要颜色
200
- kmeans = KMeans(n_clusters=3, random_state=42, n_init=10)
201
- kmeans.fit(pixels)
202
-
203
- # 将RGB值转换为颜色名称
204
- colors = []
205
- for color in kmeans.cluster_centers_:
206
- color_name = rgb_to_color_name(color)
207
- colors.append(color_name)
208
-
209
- return colors
210
- except Exception as e:
211
- logger.error(f"颜色提取失败: {e}")
212
- return ["主色调", "辅助色", "点缀色"]
213
-
214
- def rgb_to_color_name(rgb):
215
- """将RGB值转换为颜色名称"""
216
- try:
217
- r, g, b = rgb.astype(int)
218
-
219
- if r > 200 and g > 200 and b > 200:
220
- return "白色系"
221
- elif r < 50 and g < 50 and b < 50:
222
- return "黑色系"
223
- elif r > g and r > b:
224
- return "红色系" if r > 150 else "深红色系"
225
- elif g > r and g > b:
226
- return "绿色系" if g > 150 else "深绿色系"
227
- elif b > r and b > g:
228
- return "蓝色系" if b > 150 else "深蓝色系"
229
- elif r > 150 and g > 150:
230
- return "黄色系"
231
- elif r > 100 and b > 100:
232
- return "紫色系"
233
- elif g > 100 and b > 100:
234
- return "青色系"
235
- else:
236
- return "灰色系"
237
- except:
238
- return "混合色调"
239
-
240
- def generate_personalized_suggestions_v2(caption, style_scores, colors):
241
- """基于模型分析结果生成个性化建议"""
242
- try:
243
- # 获取主要风格
244
- main_style = max(style_scores.keys(), key=style_scores.get) if style_scores else "时尚潮流"
245
- main_color = colors[0] if colors else "经典色"
246
-
247
- # 基于主要风格和颜色生成建议
248
- suggestions = {}
249
-
250
- if "商务" in main_style:
251
- suggestions = {
252
- f"经典{main_style}": f"保持{main_style}特色,强调{main_color}的专业感",
253
- f"现代{main_style}": f"在{main_style}基础上融入现代设计元素",
254
- f"时尚{main_style}": f"{main_style}结合当下流行趋势",
255
- f"个性{main_style}": f"基于{main_color}打造独特的{main_style}风格"
256
- }
257
- elif "休闲" in main_style:
258
- suggestions = {
259
- "舒适休闲": f"强化舒适感,突出{main_color}的自然魅力",
260
- "时尚休闲": f"休闲风格融入时尚元素",
261
- "运动休闲": f"结合运动风格的休闲设计",
262
- "优雅休闲": f"提升休闲装的优雅度"
263
- }
264
- elif "运动" in main_style:
265
- suggestions = {
266
- "专业运动": f"强化功能性,突出{main_color}的活力",
267
- "时尚运动": f"运动风格加入潮流设计",
268
- "休闲运动": f"适合日常的运动休闲风",
269
- "户外运动": f"适应户外环境的运动设计"
270
- }
271
- else:
272
- # 其他风格的通用建议
273
- suggestions = {
274
- f"经典{main_style}": f"保持{main_style}的经典特色",
275
- f"现代{main_style}": f"{main_style}融入现代设计理念",
276
- f"创新{main_style}": f"在{main_style}基础上的创新尝试",
277
- f"个性{main_style}": f"基于{main_color}的个性化{main_style}"
278
- }
279
-
280
- # 添加基于颜色的额外建议
281
- color_suggestions = {
282
- f"单色系设计": f"围绕{main_color}打造单色系搭配",
283
- f"撞色搭配": f"以{main_color}为主的撞色设计"
284
- }
285
-
286
- # 合并建议(限制数量避免选项过多)
287
- final_suggestions = dict(list(suggestions.items())[:4] + list(color_suggestions.items())[:2])
288
-
289
- return final_suggestions
290
-
291
- except Exception as e:
292
- logger.error(f"生成建议失败: {e}")
293
- return {
294
- "经典设计": "传统经典的设计风格",
295
- "现代风格": "融入现代设计元素",
296
- "时尚潮流": "跟随当下时尚趋势",
297
- "个性化": "独特的个性化设计"
298
- }
299
 
300
  def generate_designs(selected_suggestion, progress=gr.Progress()):
301
- """根据选择的建议使用SD模型生成设计"""
302
  try:
303
- if not selected_suggestion:
304
  return gr.Gallery(value=[]), gr.Radio(choices=[])
305
 
306
  progress(0.1, desc="准备设计生成...")
307
-
308
- # 保存选择的建议
309
  app_state.selected_suggestion = selected_suggestion
310
 
311
  # 基于当前分析结果和选择的建议生成详细的提示词
312
- base_prompt = create_design_prompt(selected_suggestion, app_state.current_analysis)
 
 
 
313
 
314
  # 生成设计图像
315
  design_images = []
316
  design_choices = []
317
 
318
- # 检查模型是否可用
319
- if not hasattr(model_manager, 'sd_pipeline') or model_manager.sd_pipeline is None:
320
- logger.error("无法生成设计:Stable Diffusion 模型未加载")
321
- # 创建占位图像
322
- for i in range(3):
323
- color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200))
324
- img = Image.new('RGB', (512, 512), color=color)
325
- design_images.append(img)
326
- design_choices.append(f"{selected_suggestion} - 方案 {i+1} (模型未加载)")
327
-
328
- return (
329
- gr.Gallery(value=design_images),
330
- gr.Radio(choices=design_choices, value=design_choices[0] if design_choices else None)
331
- )
332
-
333
  for i in range(3): # 生成3个设计方案
334
  try:
335
  progress(0.2 + i*0.25, desc=f"生成设计方案 {i+1}/3...")
336
 
337
- # 为每个设计添加变化
338
- variant_prompt = f"{base_prompt}, design variation {i+1}, unique style, high quality fashion design"
 
 
 
 
 
339
 
340
- # 使用模型生成图像 - 添加错误处理
341
- try:
342
- image = model_manager.generate_image(
343
- prompt=variant_prompt,
344
- negative_prompt="blurry, low quality, distorted, text, watermark, ugly, deformed",
345
- num_inference_steps=25,
346
- width=512,
347
- height=512,
348
- guidance_scale=7.5
349
- )
350
- except Exception as gen_e:
351
- logger.error(f"生成设计 {i+1} 失败: {gen_e}")
352
- # 创建占位图像
353
- width, height = 512, 512
354
- color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200))
355
- image = Image.new('RGB', (width, height), color=color)
356
 
357
- if image:
358
- design_images.append(image)
359
- design_choices.append(f"{selected_suggestion} - 方案 {i+1}")
360
- logger.info(f"成功生成设计方案 {i+1}")
361
 
362
  except Exception as e:
363
  logger.error(f"生成设计 {i+1} 失败: {e}")
364
  # 创建占位图像
365
- width, height = 512, 512
366
- color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200))
367
- img = Image.new('RGB', (width, height), color=color)
368
  design_images.append(img)
369
  design_choices.append(f"{selected_suggestion} - 方案 {i+1} (占位)")
370
 
@@ -382,76 +136,34 @@ def generate_designs(selected_suggestion, progress=gr.Progress()):
382
  logger.error(f"设计生成错误: {e}")
383
  return gr.Gallery(value=[]), gr.Radio(choices=[])
384
 
385
- def create_design_prompt(suggestion, analysis):
386
- """基于建议和分析创建详细的设计提示词"""
387
- try:
388
- # 从分析中提取关键信息
389
- style_info = analysis.get("主要风格", "时尚")
390
- colors = analysis.get("主要颜色", ["现代色"])
391
- description = analysis.get("图像描述", "")
392
-
393
- # 基础提示词映射
394
- style_prompts = {
395
- "商务正装": "professional business attire, formal suit, clean lines, sophisticated",
396
- "休闲风": "casual clothing, comfortable style, everyday wear, relaxed fit",
397
- "运动风": "sportswear, athletic clothing, functional design, activewear",
398
- "时尚潮流": "trendy fashion, stylish clothing, contemporary design, fashionable",
399
- "复古风": "vintage style, retro fashion, classic design, nostalgic elements",
400
- "街头风": "streetwear, urban fashion, casual street style, modern youth",
401
- "优雅风": "elegant clothing, sophisticated style, refined fashion, graceful design"
402
- }
403
-
404
- # 获取风格对应的提示词
405
- style_prompt = style_prompts.get(style_info, "stylish clothing, modern design")
406
-
407
- # 颜色相关的提示词
408
- color_prompt = f"color palette inspired by {colors[0] if colors else 'modern colors'}"
409
-
410
- # 根据建议调整提示词
411
- if "经典" in suggestion:
412
- modifier = "timeless, classic design, traditional elements"
413
- elif "现代" in suggestion:
414
- modifier = "contemporary, modern aesthetic, innovative design"
415
- elif "时尚" in suggestion:
416
- modifier = "fashion-forward, trendy, runway-inspired"
417
- elif "个性" in suggestion:
418
- modifier = "unique, personalized, distinctive style"
419
- else:
420
- modifier = "high-quality, well-designed, stylish"
421
-
422
- # 组合完整的提示词
423
- full_prompt = f"{style_prompt}, {color_prompt}, {modifier}, high detail, professional fashion photography, studio lighting"
424
-
425
- logger.info(f"生成的设计提示词: {full_prompt}")
426
- return full_prompt
427
-
428
- except Exception as e:
429
- logger.error(f"创建提示词失败: {e}")
430
- return "stylish clothing design, high quality, professional fashion"
431
-
432
- def generate_3d_fitting(selected_design, progress=gr.Progress()):
433
- """生成3D试穿效果"""
434
  try:
435
- if not selected_design:
436
  return None
437
 
438
  progress(0.1, desc="准备3D试穿...")
439
 
440
- # 保存选择的设计
441
- app_state.selected_design = selected_design
 
442
 
443
- # 基于选择的设计和原始分析创��3D试穿提示词
444
- fitting_prompt = create_3d_fitting_prompt(selected_design, app_state.current_analysis)
 
 
 
445
 
446
  progress(0.3, desc="生成3D模型...")
447
 
448
- # 尝试使用ControlNet如果可用
449
  if hasattr(model_manager, 'controlnet_pipeline') and model_manager.controlnet_pipeline:
450
  try:
451
- # 使用ControlNet生成更精确的试穿效果
452
  image = model_manager.generate_controlnet_image(
453
- image=app_state.current_image, # 使用原始图像作为控制
454
  prompt=fitting_prompt,
 
455
  negative_prompt="blurry, distorted, low quality, unrealistic, extra limbs, deformed",
456
  num_inference_steps=30,
457
  guidance_scale=8.0
@@ -459,7 +171,7 @@ def generate_3d_fitting(selected_design, progress=gr.Progress()):
459
  progress(0.9, desc="完成3D渲染")
460
  return image
461
  except Exception as e:
462
- logger.warning(f"ControlNet生成失败,使用普通SD: {e}")
463
 
464
  # 回退到普通SD模型
465
  progress(0.5, desc="使用标准模型生成...")
@@ -469,145 +181,110 @@ def generate_3d_fitting(selected_design, progress=gr.Progress()):
469
  negative_prompt="blurry, distorted, low quality, unrealistic, extra limbs, deformed, bad anatomy",
470
  num_inference_steps=30,
471
  width=512,
472
- height=768, # 适合全身图像
473
  guidance_scale=7.5
474
  )
475
  except Exception as e:
476
  logger.error(f"3D试穿生成失败: {e}")
477
- # 创建占位图像
478
- image = Image.new('RGB', (512, 768), color=(180, 180, 180))
479
 
480
  progress(0.9, desc="完成3D渲染")
481
  return image
482
 
483
  except Exception as e:
484
  logger.error(f"3D试穿生成错误: {e}")
485
- # 创建占位图像
486
- img = Image.new('RGB', (512, 768), color=(180, 180, 180))
487
- return img
488
 
489
- def create_3d_fitting_prompt(design_info, analysis):
490
- """创建3D试穿的提示词"""
491
  try:
 
492
  style_info = analysis.get("主要风格", "时尚")
493
  colors = analysis.get("主要颜色", ["现代色"])
 
494
 
495
- # 3D试穿基础提示词
496
- base_prompt = "3D virtual try-on, realistic human model wearing"
 
 
 
 
 
 
 
 
497
 
498
- # 根据设计信息调整
499
- if "方案" in design_info:
500
- design_type = design_info.split(" - ")[0] if " - " in design_info else design_info
501
- else:
502
- design_type = design_info
503
 
504
- # 添加风格和颜色信息
505
- style_desc = f"{style_info} style clothing"
506
- color_desc = f"in {colors[0] if colors else 'stylish colors'}"
 
507
 
508
- # 完整的3D试穿提示词
509
- full_prompt = f"{base_prompt} {style_desc} {color_desc}, full body shot, professional studio lighting, high quality 3D rendering, realistic fabric texture, well-fitted clothing"
 
 
 
 
 
510
 
511
- logger.info(f"3D试穿提示词: {full_prompt}")
 
 
 
 
 
 
 
 
 
 
 
 
 
512
  return full_prompt
513
 
514
  except Exception as e:
515
- logger.error(f"创建3D提示词失败: {e}")
516
- return "3D virtual fashion try-on, realistic human model, full body, studio lighting"
517
 
518
- def create_gradio_interface():
519
- """创建Gradio用户界面"""
520
-
521
- with gr.Blocks(title="AI时尚设计师", theme="soft") as demo:
522
- gr.Markdown("# 🎨 AI时尚设计师")
523
- gr.Markdown("上传服装图片,AI将分析风格并生成个性化设计方案和3D试穿效果")
524
-
525
- with gr.Row():
526
- with gr.Column(scale=1):
527
- image_input = gr.Image(type="filepath", label="上传参考图片", height=400)
528
- analyze_btn = gr.Button("🔍 AI智能分析", variant="primary", size="lg")
529
-
530
- with gr.Column(scale=2):
531
- analysis_output = gr.JSON(label="🎯 AI分析结果")
532
-
533
- with gr.Tab("💡 设计建议"):
534
- suggestions_output = gr.JSON(label="个性化设计建议")
535
- suggestion_choice = gr.Radio(label="选择设计方向", interactive=True)
536
- generate_designs_btn = gr.Button("🎨 生成设计方案", variant="primary")
537
-
538
- with gr.Tab("👔 样衣设计"):
539
- designs_gallery = gr.Gallery(label="AI生成的设计方案", columns=3, height=400)
540
- design_choice = gr.Radio(label="选择设计方案", interactive=True)
541
- generate_3d_btn = gr.Button("🎭 生成3D试穿效果", variant="primary")
542
-
543
- with gr.Tab("👤 3D试穿效果"):
544
- fitting_result = gr.Image(label="3D试穿效果", height=600)
545
-
546
- # 系统状态和控制
547
- with gr.Row():
548
- with gr.Column():
549
- gr.Markdown("### 🔧 系统控制")
550
- cleanup_btn = gr.Button("🧹 清理内存", variant="secondary")
551
- gr.Markdown("> **提示**: 如果遇到内存问题,请点击清理内存按钮")
552
-
553
- # 事件绑定 - 完整的流水线连接
554
- analyze_btn.click(
555
- fn=upload_and_analyze,
556
- inputs=[image_input],
557
- outputs=[analysis_output, suggestions_output, suggestion_choice, designs_gallery],
558
- concurrency_limit=1
559
- )
560
 
561
- generate_designs_btn.click(
562
- fn=generate_designs,
563
- inputs=[suggestion_choice],
564
- outputs=[designs_gallery, design_choice],
565
- concurrency_limit=1
566
- )
567
 
568
- generate_3d_btn.click(
569
- fn=generate_3d_fitting,
570
- inputs=[design_choice],
571
- outputs=[fitting_result],
572
- concurrency_limit=1
573
- )
574
 
575
- cleanup_btn.click(
576
- fn=model_manager.cleanup,
577
- inputs=[],
578
- outputs=[]
 
 
 
 
 
 
 
579
  )
580
 
581
- # 添加使用说明
582
- with gr.Row():
583
- gr.Markdown("""
584
- ### 📋 使用流程:
585
- 1. **上传图片** → 点击"AI智能分析"进行风格识别
586
- 2. **查看建议** → 在"设计建议"标签中选择心仪的设计方向
587
- 3. **生成设计** → 点击"生成设计方案"查看AI设计的服装
588
- 4. **3D试穿** → ���择喜欢的设计,点击"生成3D试穿效果"
589
-
590
- 💡 **提示**:
591
- - 每一步都会调用相应的AI模型,请耐心等待生成完成
592
- - 如果遇到模型加载问题,请检查网络连接或预下载模型
593
- """)
594
-
595
- return demo
596
 
597
- if __name__ == "__main__":
598
- # 检查环境
599
- logger.info(f"Python版本: {sys.version}")
600
- logger.info(f"当前工作目录: {os.getcwd()}")
601
- logger.info(f"模型管理器状态: {'完整版' if MODELS_AVAILABLE else '简化版'}")
602
-
603
- # 创建并启动界面
604
- demo = create_gradio_interface()
605
-
606
- # 启动应用
607
- demo.launch(
608
- server_name="0.0.0.0",
609
- server_port=7860,
610
- share=False,
611
- show_error=True,
612
- max_threads=2
613
- )
 
1
+ # ...(保留之前的导入和初始化代码)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  def upload_and_analyze(image_path, progress=gr.Progress()):
4
+ """完整的图片上传和分析流程 - 增强关联性"""
5
  try:
6
+ # 重置应用状态
7
+ app_state = AppState()
8
+
9
  if image_path is None:
10
  return {}, {}, gr.Radio(choices=[]), gr.Gallery(value=[])
11
 
12
  progress(0.1, desc="加载图片...")
 
 
13
  image = Image.open(image_path).convert('RGB')
14
  app_state.current_image = image
15
 
16
+ # 步骤1: 生成图像描述并保存
17
  progress(0.2, desc="生成图像描述...")
 
 
18
  try:
19
  caption = model_manager.generate_caption(image)
20
  app_state.current_caption = caption
 
24
  caption = "时尚服装设计"
25
  app_state.current_caption = caption
26
 
 
 
27
  # 步骤2: 使用CLIP模型分析风格
28
+ progress(0.4, desc="分析服装风格...")
29
  try:
30
+ style_scores = model_manager.analyze_style(image)
 
 
 
 
31
  logger.info(f"风格分析结果: {style_scores}")
32
  except Exception as e:
33
  logger.error(f"风格分析失败: {e}")
34
+ # 使用描述回退分析
35
+ style_scores = analyze_style_from_caption(caption)
 
36
 
37
  # 步骤3: 提取颜色信息
38
+ progress(0.6, desc="提取颜色信息...")
39
  colors = extract_dominant_colors(image)
40
 
41
+ # 步骤4: 整合分析结果 - 保存所有关键信息
42
  progress(0.8, desc="生成完整分析...")
43
+ app_state.current_analysis = {
 
 
44
  "图像描述": caption,
45
  "主要风格": max(style_scores.keys(), key=style_scores.get),
46
  "风格评分": style_scores,
 
49
  "分析时间": time.strftime("%Y-%m-%d %H:%M:%S")
50
  }
51
 
52
+ # 步骤5: 基于所有分析结果生成个性化建议
53
+ app_state.current_suggestions = generate_personalized_suggestions_v2(
54
  caption, style_scores, colors
55
  )
56
 
 
 
 
 
57
  # 创建建议选项
58
+ choices = list(app_state.current_suggestions.keys())
59
 
60
  progress(1.0, desc="分析完成")
61
 
62
  return (
63
+ app_state.current_analysis,
64
+ app_state.current_suggestions,
65
  gr.Radio(choices=choices, value=choices[0] if choices else None),
66
  gr.Gallery(value=[]) # 清空之前的设计
67
  )
68
 
69
  except Exception as e:
 
70
  logger.error(f"上传分析失败: {e}")
71
+ return {"错误": f"分析失败: {str(e)}"}, {}, gr.Radio(choices=[]), gr.Gallery(value=[])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  def generate_designs(selected_suggestion, progress=gr.Progress()):
74
+ """根据选择的建议使用SD模型生成设计 - 增强关联性"""
75
  try:
76
+ if not selected_suggestion or not app_state.current_analysis:
77
  return gr.Gallery(value=[]), gr.Radio(choices=[])
78
 
79
  progress(0.1, desc="准备设计生成...")
 
 
80
  app_state.selected_suggestion = selected_suggestion
81
 
82
  # 基于当前分析结果和选择的建议生成详细的提示词
83
+ base_prompt = create_design_prompt(
84
+ selected_suggestion,
85
+ app_state.current_analysis
86
+ )
87
 
88
  # 生成设计图像
89
  design_images = []
90
  design_choices = []
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  for i in range(3): # 生成3个设计方案
93
  try:
94
  progress(0.2 + i*0.25, desc=f"生成设计方案 {i+1}/3...")
95
 
96
+ # 为每个设计添加变化,同时包含原始描述和风格信息
97
+ variant_prompt = (
98
+ f"{base_prompt}, design variation {i+1}, "
99
+ f"inspired by: '{app_state.current_caption}', "
100
+ f"style: {app_state.current_analysis['主要风格']}, "
101
+ f"colors: {', '.join(app_state.current_analysis['主要颜色'][:2])}"
102
+ )
103
 
104
+ # 使用模型生成图像
105
+ image = model_manager.generate_image(
106
+ prompt=variant_prompt,
107
+ negative_prompt="blurry, low quality, distorted, text, watermark, ugly, deformed",
108
+ num_inference_steps=25,
109
+ width=512,
110
+ height=512,
111
+ guidance_scale=7.5
112
+ )
 
 
 
 
 
 
 
113
 
114
+ design_images.append(image)
115
+ design_choices.append(f"{selected_suggestion} - 方案 {i+1}")
116
+ logger.info(f"成功生成设计方案 {i+1}")
 
117
 
118
  except Exception as e:
119
  logger.error(f"生成设计 {i+1} 失败: {e}")
120
  # 创建占位图像
121
+ img = create_placeholder_image(512, 512)
 
 
122
  design_images.append(img)
123
  design_choices.append(f"{selected_suggestion} - 方案 {i+1} (占位)")
124
 
 
136
  logger.error(f"设计生成错误: {e}")
137
  return gr.Gallery(value=[]), gr.Radio(choices=[])
138
 
139
+ def generate_3d_fitting(selected_design_index, progress=gr.Progress()):
140
+ """生成3D试穿效果 - 增强关联性"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  try:
142
+ if not app_state.design_images or not app_state.current_image:
143
  return None
144
 
145
  progress(0.1, desc="准备3D试穿...")
146
 
147
+ # 获取选中的设计
148
+ selected_image = app_state.design_images[selected_design_index]
149
+ app_state.selected_design = selected_design_index
150
 
151
+ # 基于原始图像和选择的设计创建3D试穿提示词
152
+ fitting_prompt = create_3d_fitting_prompt(
153
+ selected_design_index,
154
+ app_state.current_analysis
155
+ )
156
 
157
  progress(0.3, desc="生成3D模型...")
158
 
159
+ # 尝试使用ControlNet
160
  if hasattr(model_manager, 'controlnet_pipeline') and model_manager.controlnet_pipeline:
161
  try:
162
+ # 使用原始图像作为控制图,选择的设计作为风格参考
163
  image = model_manager.generate_controlnet_image(
164
+ image=app_state.current_image, # 姿势控制
165
  prompt=fitting_prompt,
166
+ reference_image=selected_image, # 设计风格参考
167
  negative_prompt="blurry, distorted, low quality, unrealistic, extra limbs, deformed",
168
  num_inference_steps=30,
169
  guidance_scale=8.0
 
171
  progress(0.9, desc="完成3D渲染")
172
  return image
173
  except Exception as e:
174
+ logger.warning(f"ControlNet生成失败: {e}")
175
 
176
  # 回退到普通SD模型
177
  progress(0.5, desc="使用标准模型生成...")
 
181
  negative_prompt="blurry, distorted, low quality, unrealistic, extra limbs, deformed, bad anatomy",
182
  num_inference_steps=30,
183
  width=512,
184
+ height=768,
185
  guidance_scale=7.5
186
  )
187
  except Exception as e:
188
  logger.error(f"3D试穿生成失败: {e}")
189
+ image = create_placeholder_image(512, 768)
 
190
 
191
  progress(0.9, desc="完成3D渲染")
192
  return image
193
 
194
  except Exception as e:
195
  logger.error(f"3D试穿生成错误: {e}")
196
+ return create_placeholder_image(512, 768)
 
 
197
 
198
+ def create_design_prompt(suggestion, analysis):
199
+ """创建详细的设计提示词 - 整合所有分析结果"""
200
  try:
201
+ # 从分析中提取关键信息
202
  style_info = analysis.get("主要风格", "时尚")
203
  colors = analysis.get("主要颜色", ["现代色"])
204
+ caption = analysis.get("图像描述", "")
205
 
206
+ # 获取风格描述
207
+ style_prompts = {
208
+ "商务正装": "专业商务装,正式西装,简洁线条,精致剪裁",
209
+ "休闲风": "休闲服装,舒适风格,日常穿着,宽松合身",
210
+ "运动风": "运动服装,功能性设计,运动休闲",
211
+ "时尚潮流": "时尚潮流,现代设计,高级时装",
212
+ "复古风": "复古风格,怀旧元素,经典设计",
213
+ "街头风": "街头服饰,都市风格,现代青年文化",
214
+ "优雅风": "优雅服装,精致风格,高贵设计"
215
+ }
216
 
217
+ style_prompt = style_prompts.get(style_info, "时尚服装,现代设计")
 
 
 
 
218
 
219
+ # 颜色描述
220
+ color_prompt = f"主色调: {colors[0] if colors else '现代色调'}"
221
+ if len(colors) > 1:
222
+ color_prompt += f", 辅色: {colors[1]}"
223
 
224
+ # 根据建议调整提示词
225
+ suggestion_modifiers = {
226
+ "经典": "经典设计,永恒风格,传统元素",
227
+ "现代": "现代美学,创新设计,前沿时尚",
228
+ "时尚": "时尚前沿,T台风格,潮流趋势",
229
+ "个性": "独特设计,个性化元素,定制风格"
230
+ }
231
 
232
+ modifier = "高品质,精心设计,时尚风格"
233
+ for key, value in suggestion_modifiers.items():
234
+ if key in suggestion:
235
+ modifier = value
236
+ break
237
+
238
+ # 组合完整的提示词 - 整合所有分析元素
239
+ full_prompt = (
240
+ f"{style_prompt},{color_prompt},{modifier}。"
241
+ f"设计灵感来源:'{caption}'。"
242
+ f"高清细节,专业时尚摄影,工作室灯光"
243
+ )
244
+
245
+ logger.info(f"生成的设计提示词: {full_prompt}")
246
  return full_prompt
247
 
248
  except Exception as e:
249
+ logger.error(f"创建提示词失败: {e}")
250
+ return "时尚服装设计,高品质,专业时尚"
251
 
252
+ def create_3d_fitting_prompt(design_index, analysis):
253
+ """创建3D试穿的提示词 - 整合所有分析结果"""
254
+ try:
255
+ style_info = analysis.get("主要风格", "时尚")
256
+ colors = analysis.get("主要颜色", ["现代色"])
257
+ caption = analysis.get("图像描述", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
 
259
+ # 获取设计描述
260
+ design_desc = f"设计方案 {design_index+1}"
 
 
 
 
261
 
262
+ # 3D试穿基础提示词
263
+ base_prompt = "3D虚拟试穿,真实人体模特穿着"
 
 
 
 
264
 
265
+ # 添加风格和颜色信息
266
+ style_desc = f"{style_info}风格服装"
267
+ color_desc = f"主色调: {colors[0] if colors else '时尚色调'}"
268
+
269
+ # 完整的3D试穿提示词
270
+ full_prompt = (
271
+ f"{base_prompt} {style_desc},{color_desc}。"
272
+ f"全身展示,专业工作室灯光,高质量3D渲染,"
273
+ f"真实面料质感,合身剪裁。"
274
+ f"设计灵感:'{caption}'。"
275
+ f"具体设计:{design_desc}"
276
  )
277
 
278
+ logger.info(f"3D试穿提示词: {full_prompt}")
279
+ return full_prompt
280
+
281
+ except Exception as e:
282
+ logger.error(f"创建3D提示词失败: {e}")
283
+ return "3D虚拟时尚试穿,真实人体模特,全身展示,工作室灯光"
284
+
285
+ def create_placeholder_image(width, height):
286
+ """创建占位图像"""
287
+ color = (random.randint(120, 200), random.randint(120, 200), random.randint(120, 200))
288
+ return Image.new('RGB', (width, height), color=color)
 
 
 
 
289
 
290
+ # ...(保留后续的UI创建和启动代码)