oscnet commited on
Commit
e0e224f
·
1 Parent(s): 5396637

重构: 使用 Stable Zero123 生成工程六视图

Browse files

- 替换 Zero123++ 为 Stable Zero123
- 实现自定义视角控制,生成标准工程六视图
- 视图布局: 主/右/后(第一行), 左/俯/底(第二行)
- 添加进度条显示生成进度
- 更新依赖: 添加 torchvision, einops, omegaconf
- 更新文档说明工程六视图的用途和参数

Files changed (2) hide show
  1. README.md +65 -24
  2. app.py +151 -70
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Zero123++ Multi-view Generator
3
  emoji: 🎨
4
  colorFrom: blue
5
  colorTo: purple
@@ -10,53 +10,94 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- # Zero123++ Multi-view Generator
14
 
15
- 将单张图片转换为 6 个不同角度的一致性多视图图像。
16
 
17
  ## 功能特点
18
 
19
- - 📷 单图生成多视图
20
- - 🎯 6 个固定角度视图
21
- - 🚀 基于 Zero123++ v1.1 模型
22
  - ✨ 简洁的 Gradio 界面
23
 
24
  ## 使用方法
25
 
26
- 1. 上传一张图片(建议正方形,>= 320x320)
27
- 2. 点击"生成多视图"按钮
28
- 3. 等待 30-60 秒
29
- 4. 下载生成的多视图图像
30
 
31
  ## 输出说明
32
 
33
- 生成的图像包含 6 个视图,排列为 2 行 3 列:
34
 
35
- **视角参数:**
36
- - 方位角: 30°, 90°, 150°, 210°, 270°, 330°
37
- - 仰角: 30°, -20°, 30°, -20°, 30°, -20°
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  ## 技术细节
40
 
41
- - 模型: [sudo-ai/zero123plus-v1.1](https://huggingface.co/sudo-ai/zero123plus-v1.1)
42
- - 框架: Diffusers + Gradio
43
- - 硬件: 需要 GPU (推荐至少 T4 或更高)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  ## 引用
46
 
47
  如果使用了这个模型,请引用:
48
 
49
  ```bibtex
50
- @misc{shi2023zero123plus,
51
- title={Zero123++: a Single Image to Consistent Multi-view Diffusion Base Model},
52
- author={Ruoxi Shi and Hansheng Chen and Zhuoyang Zhang and Minghua Liu and Chao Xu and Xinyue Wei and Linghao Chen and Chong Zeng and Hao Su},
53
- year={2023},
54
- eprint={2310.15110},
55
- archivePrefix={arXiv},
56
- primaryClass={cs.CV}
57
  }
58
  ```
59
 
60
  ## 许可证
61
 
62
  MIT License
 
 
 
 
 
 
 
1
  ---
2
+ title: Zero123 工程六视图生成器
3
  emoji: 🎨
4
  colorFrom: blue
5
  colorTo: purple
 
10
  license: mit
11
  ---
12
 
13
+ # Zero123 工程六视图生成器
14
 
15
+ 将单张主视图转换为标准的工程六视图(正交投影)
16
 
17
  ## 功能特点
18
 
19
+ - 📷 单图生成工程六视图
20
+ - 🎯 标准正交投影视图(前/后/左/右/上/下)
21
+ - 🚀 基于 Stable Zero123 模型
22
  - ✨ 简洁的 Gradio 界面
23
 
24
  ## 使用方法
25
 
26
+ 1. 上传一张主视图图片(建议正方形,>= 256x256)
27
+ 2. 点击"生成工程六视图"按钮
28
+ 3. 等待 3-5 分钟(每个视图需要约 30-60 秒)
29
+ 4. 下载生成的六视图合成图
30
 
31
  ## 输出说明
32
 
33
+ 生成的图像包含 6 个标准工程视图,排列为 2 行 3 列:
34
 
35
+ ```
36
+ ┌─────────────┬─────────────┬─────────────┐
37
+ │ 主视图 │ 右视图 │ 后视图 │
38
+ │ (Front 0°) │ (Right 90°) │ (Back 180°) │
39
+ ├─────────────┼─────────────┼─────────────┤
40
+ │ 左视图 │ 俯视图 │ 底视图 │
41
+ │ (Left -90°) │ (Top) │ (Bottom) │
42
+ └─────────────┴─────────────┴─────────────┘
43
+ ```
44
+
45
+ ### 视角参数
46
+
47
+ | 视图 | 方位角 (Azimuth) | 仰角 (Elevation) |
48
+ |-----|-----------------|-----------------|
49
+ | 主视图 | 0° | 0° |
50
+ | 左视图 | -90° | 0° |
51
+ | 右视图 | 90° | 0° |
52
+ | 后视图 | 180° | 0° |
53
+ | 俯视图 | 0° | -90° |
54
+ | 底视图 | 0° | 90° |
55
 
56
  ## 技术细节
57
 
58
+ - **模型**: [Stable Zero123](https://huggingface.co/stabilityai/stable-zero123)
59
+ - **框架**: Diffusers + Gradio
60
+ - **硬件**: 需要 GPU (推荐至少 T4 或更高)
61
+ - **推理方式**: 每个视图独立生成,循环 6 次推理
62
+ - **总耗时**: 约 3-5 分钟(取决于 GPU 性能)
63
+
64
+ ## 与 Zero123++ 的区别
65
+
66
+ | 特性 | Zero123 (本项目) | Zero123++ |
67
+ |-----|-----------------|----------|
68
+ | 视角控制 | ✅ 自定义(工程六视图) | ❌ 固定视角 |
69
+ | 输出视图 | 前/后/左/右/上/下 | 环绕视图(30°-330°) |
70
+ | 推理次数 | 6次(每个视图1次) | 1次(同时生成6个) |
71
+ | 速度 | 较慢(3-5分钟) | 快(30-60秒) |
72
+ | 灵活性 | 高 | 低 |
73
+ | 适用场景 | 工程制图、3D建模 | 快速预览 |
74
+
75
+ ## 应用场景
76
+
77
+ - 🏗️ **工程制图**: 生成标准的六视图工程图
78
+ - 🎮 **3D 建模**: 为 3D 建模提供参考视图
79
+ - 📐 **产品设计**: 产品的多角度展示
80
+ - 🎨 **艺术创作**: 角色或物体的多视角参考
81
 
82
  ## 引用
83
 
84
  如果使用了这个模型,请引用:
85
 
86
  ```bibtex
87
+ @inproceedings{liu2023zero,
88
+ title={Zero-1-to-3: Zero-shot One Image to 3D Object},
89
+ author={Liu, Ruoshi and Duan, Rundi and Li, Hang and Xu, Zhuoyang and Xia, Ziwei and Qiu, Kejie and Shen, Yikang and Gong, Mingyuan and Liu, Zhigang and Chen, Yukang and Wang, Peng and Zhuge, Yunzhi},
90
+ booktitle={ICCV},
91
+ year={2023}
 
 
92
  }
93
  ```
94
 
95
  ## 许可证
96
 
97
  MIT License
98
+
99
+ ## 致谢
100
+
101
+ - [Zero123](https://zero123.cs.columbia.edu/) - 原始论文和模型
102
+ - [Stability AI](https://stability.ai/) - Stable Zero123 改进版本
103
+ - [Hugging Face](https://huggingface.co/) - 模型托管和部署平台
app.py CHANGED
@@ -1,69 +1,125 @@
1
  #!/usr/bin/env python3
2
  """
3
- Zero123++ Gradio Demo
4
  用于 Hugging Face Spaces 部署
5
  """
6
 
7
  import gradio as gr
8
  import torch
9
  from PIL import Image
10
- from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler
11
- import os
 
12
 
13
  # 全局变量存储 pipeline
14
  pipeline = None
15
 
 
 
 
 
 
 
 
 
 
 
16
  def load_model():
17
- """加载模型"""
18
  global pipeline
19
 
20
  if pipeline is not None:
21
  return
22
 
23
- print("正在加载模型...")
24
 
25
  # 检查 CUDA 可用性
26
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
27
  dtype = torch.float16 if torch.cuda.is_available() else torch.float32
28
 
29
- # 加载 pipeline
30
- pipeline = DiffusionPipeline.from_pretrained(
31
- "sudo-ai/zero123plus-v1.1",
32
- custom_pipeline="sudo-ai/zero123plus-pipeline",
33
- torch_dtype=dtype
34
- )
 
35
 
36
- # 设置调度器
37
- pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(
38
- pipeline.scheduler.config,
39
- timestep_spacing='trailing'
40
- )
41
 
42
- pipeline.to(device)
43
- print(f"✓ 模型加载完成 (设备: {device})")
44
 
45
- def process_image(input_image, remove_bg=False):
 
 
 
 
 
 
 
 
 
 
 
46
  """
47
- 处理输入图像,生成多视图
48
 
49
  输入:
50
  input_image: PIL Image
51
- remove_bg: 是否移除背景 (暂未实现)
 
 
52
 
53
  输出:
54
- result_image: PIL Image (多视图合成图)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  """
56
  if input_image is None:
57
  return None
58
 
59
- # 确保模型已加载
60
- load_model()
61
-
62
  try:
63
- # 预处理图像 - 转为正方形
64
- img = input_image
65
 
66
- # 如果不是正方形,裁剪为正方形
 
67
  if img.size[0] != img.size[1]:
68
  size = min(img.size)
69
  img = img.crop((
@@ -73,86 +129,108 @@ def process_image(input_image, remove_bg=False):
73
  (img.size[1] + size) // 2
74
  ))
75
 
76
- # 调整到推荐尺寸
77
- target_size = 320
78
- if img.size[0] != target_size:
79
- img = img.resize((target_size, target_size), Image.LANCZOS)
80
 
81
- # 运行推理
82
- print("正在生成多视图...")
83
- result = pipeline(img).images[0]
84
 
85
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  except Exception as e:
88
  print(f"错误: {e}")
 
 
89
  raise gr.Error(f"处理失败: {str(e)}")
90
 
91
  # 创建 Gradio 界面
92
  def create_demo():
93
- with gr.Blocks(title="Zero123++ Demo") as demo:
94
  gr.Markdown("""
95
- # Zero123++ 多视图生成
96
 
97
- 将单张图片转换为 6 个不同角度的视图
98
 
99
  **输入要求:**
100
  - 建议使用正方形图片
101
- - 推荐分辨率 >= 320x320
102
  - 脚本会自动裁剪和调整非正方形图片
103
 
104
  **输出说明:**
105
- - 生成 6 个视图 (2x 3列)
106
- - 方位角: 30°, 90°, 150°, 210°, 270°, 330°
107
- - 仰角: 30°, -20°, 30°, -20°, 30°, -20°
 
 
 
 
 
 
 
 
 
 
108
  """)
109
 
110
  with gr.Row():
111
  with gr.Column():
112
  input_image = gr.Image(
113
- label="输入图片",
114
  type="pil",
115
  height=400
116
  )
117
 
118
- # remove_bg = gr.Checkbox(
119
- # label="移除背景 (实验性)",
120
- # value=False
121
- # )
122
 
123
- generate_btn = gr.Button("生成多视图", variant="primary")
 
 
 
 
124
 
125
  with gr.Column():
126
  output_image = gr.Image(
127
- label="多视图输出",
128
  type="pil",
129
  height=400
130
  )
131
 
132
- gr.Examples(
133
- examples=[
134
- ["examples/example1.png"],
135
- ["examples/example2.png"],
136
- ],
137
- inputs=input_image,
138
- label="示例图片"
139
- )
140
-
141
  gr.Markdown("""
142
  ### 技术说明
143
- - 模型: [sudo-ai/zero123plus-v1.1](https://huggingface.co/sudo-ai/zero123plus-v1.1)
144
- - 首次运行需要加载模型,可能需要 20-30
145
- - 生成时间约 30-60 秒 (取决于硬件)
 
146
 
147
  ### 引用
148
  ```bibtex
149
- @misc{shi2023zero123plus,
150
- title={Zero123++: a Single Image to Consistent Multi-view Diffusion Base Model},
151
- author={Ruoxi Shi and Hansheng Chen and Zhuoyang Zhang and Minghua Liu and Chao Xu and Xinyue Wei and Linghao Chen and Chong Zeng and Hao Su},
152
- year={2023},
153
- eprint={2310.15110},
154
- archivePrefix={arXiv},
155
- primaryClass={cs.CV}
156
  }
157
  ```
158
  """)
@@ -168,11 +246,14 @@ def create_demo():
168
 
169
  if __name__ == "__main__":
170
  # 预加载模型
 
 
 
171
  load_model()
172
 
173
  # 启动 demo
174
  demo = create_demo()
175
- demo.queue()
176
  demo.launch(
177
  server_name="0.0.0.0",
178
  server_port=7860,
 
1
  #!/usr/bin/env python3
2
  """
3
+ Zero123 工程六视图生成器
4
  用于 Hugging Face Spaces 部署
5
  """
6
 
7
  import gradio as gr
8
  import torch
9
  from PIL import Image
10
+ import numpy as np
11
+ from diffusers import DiffusionPipeline, DDIMScheduler, EulerAncestralDiscreteScheduler
12
+ import math
13
 
14
  # 全局变量存储 pipeline
15
  pipeline = None
16
 
17
+ # 工程六视图的相机参数配置
18
+ ENGINEERING_VIEWS = {
19
+ "主视图 (Front)": {"azimuth": 0.0, "elevation": 0.0, "position": (0, 0)},
20
+ "右视图 (Right)": {"azimuth": 90.0, "elevation": 0.0, "position": (1, 0)},
21
+ "后视图 (Back)": {"azimuth": 180.0, "elevation": 0.0, "position": (2, 0)},
22
+ "左视图 (Left)": {"azimuth": -90.0, "elevation": 0.0, "position": (0, 1)},
23
+ "俯视图 (Top)": {"azimuth": 0.0, "elevation": -90.0, "position": (1, 1)},
24
+ "底视图 (Bottom)": {"azimuth": 0.0, "elevation": 90.0, "position": (2, 1)},
25
+ }
26
+
27
  def load_model():
28
+ """加载 Stable Zero123 模型"""
29
  global pipeline
30
 
31
  if pipeline is not None:
32
  return
33
 
34
+ print("正在加载 Stable Zero123 模型...")
35
 
36
  # 检查 CUDA 可用性
37
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
38
  dtype = torch.float16 if torch.cuda.is_available() else torch.float32
39
 
40
+ try:
41
+ # 使用 Stability AI 改进的 Zero123 版本
42
+ pipeline = DiffusionPipeline.from_pretrained(
43
+ "stabilityai/stable-zero123",
44
+ torch_dtype=dtype,
45
+ variant="fp16" if torch.cuda.is_available() else None
46
+ )
47
 
48
+ # 设置调度器
49
+ pipeline.scheduler = DDIMScheduler.from_config(
50
+ pipeline.scheduler.config
51
+ )
 
52
 
53
+ pipeline.to(device)
 
54
 
55
+ # 启用内存优化
56
+ if torch.cuda.is_available():
57
+ pipeline.enable_attention_slicing()
58
+ pipeline.enable_vae_slicing()
59
+
60
+ print(f"✓ 模型加载完成 (设备: {device})")
61
+ except Exception as e:
62
+ print(f"错误: 无法加载 Stable Zero123 - {e}")
63
+ print("尝试使用备用方法...")
64
+ raise
65
+
66
+ def generate_single_view(input_image, azimuth, elevation, num_inference_steps=75):
67
  """
68
+ 生成单个视角的图像
69
 
70
  输入:
71
  input_image: PIL Image
72
+ azimuth: 方位角()
73
+ elevation: 仰角(度)
74
+ num_inference_steps: 推理步数
75
 
76
  输出:
77
+ result_image: PIL Image
78
+ """
79
+ global pipeline
80
+
81
+ if pipeline is None:
82
+ load_model()
83
+
84
+ # 预处理输入图像
85
+ # Stable Zero123 期望 256x256 的图像
86
+ img = input_image.resize((256, 256), Image.LANCZOS)
87
+
88
+ # 运行推理
89
+ # Stable Zero123 的输入格式
90
+ result = pipeline(
91
+ img,
92
+ num_inference_steps=num_inference_steps,
93
+ guidance_scale=3.0,
94
+ # 相机参数 (polar angle, azimuth angle, zoom/distance)
95
+ # elevation 范围: 0-180 (0=顶部, 90=水平, 180=底部)
96
+ # azimuth 范围: 0-360
97
+ elevation_deg=90 - elevation, # 转换为 polar angle
98
+ azimuth_deg=azimuth % 360,
99
+ ).images[0]
100
+
101
+ return result
102
+
103
+ def process_image(input_image, progress=gr.Progress()):
104
+ """
105
+ 处理输入图像,生成工程六视图
106
+
107
+ 输入:
108
+ input_image: PIL Image
109
+ progress: Gradio Progress 跟踪器
110
+
111
+ 输出:
112
+ result_image: PIL Image (六视图合成图)
113
  """
114
  if input_image is None:
115
  return None
116
 
 
 
 
117
  try:
118
+ # 确保模型已加载
119
+ load_model()
120
 
121
+ # 预处理 - 转为正方形
122
+ img = input_image
123
  if img.size[0] != img.size[1]:
124
  size = min(img.size)
125
  img = img.crop((
 
129
  (img.size[1] + size) // 2
130
  ))
131
 
132
+ # 生成六个视图
133
+ views = {}
134
+ total_views = len(ENGINEERING_VIEWS)
 
135
 
136
+ for idx, (view_name, params) in enumerate(ENGINEERING_VIEWS.items()):
137
+ progress((idx + 1) / total_views, desc=f"生成 {view_name}...")
 
138
 
139
+ print(f"正在生成 {view_name} (方位角={params['azimuth']}°, 仰角={params['elevation']}°)")
140
+
141
+ view_image = generate_single_view(
142
+ img,
143
+ azimuth=params['azimuth'],
144
+ elevation=params['elevation'],
145
+ num_inference_steps=50 # 可以调整步数以平衡速度和质量
146
+ )
147
+
148
+ views[view_name] = view_image
149
+ print(f"✓ {view_name} 生成完成")
150
+
151
+ # 合成为 2x3 网格
152
+ view_size = 256 # 每个视图的尺寸
153
+ combined = Image.new('RGB', (view_size * 3, view_size * 2))
154
+
155
+ for view_name, params in ENGINEERING_VIEWS.items():
156
+ x, y = params['position']
157
+ combined.paste(views[view_name], (x * view_size, y * view_size))
158
+
159
+ print("✓ 所有视图生成完成并合成")
160
+ return combined
161
 
162
  except Exception as e:
163
  print(f"错误: {e}")
164
+ import traceback
165
+ traceback.print_exc()
166
  raise gr.Error(f"处理失败: {str(e)}")
167
 
168
  # 创建 Gradio 界面
169
  def create_demo():
170
+ with gr.Blocks(title="Zero123 工程六视图生成器") as demo:
171
  gr.Markdown("""
172
+ # Zero123 工程六视图生成器
173
 
174
+ 将单张主视图转换为标准的工程六视图(正交投影)
175
 
176
  **输入要求:**
177
  - 建议使用正方形图片
178
+ - 推荐分辨率 >= 256x256
179
  - 脚本会自动裁剪和调整非正方形图片
180
 
181
  **输出说明:**
182
+ 生成标准工程六视图,排列为 23 列:
183
+
184
+ | 主视图 (0°) | 右视图 (90°) | 后视图 (180°) |
185
+ |-----------|------------|-------------|
186
+ | 左视图 (-90°) | 俯视图 (上方) | 底视图 (下方) |
187
+
188
+ **视角参数:**
189
+ - 主视图: 方位角 0°, 仰角 0°
190
+ - 左视图: 方位角 -90°, 仰角 0°
191
+ - 右视图: 方位角 90°, 仰角 0°
192
+ - 后视图: 方位角 180°, 仰角 0°
193
+ - 俯视图: 方位角 0°, 仰角 -90°
194
+ - 底视图: 方位角 0°, 仰角 90°
195
  """)
196
 
197
  with gr.Row():
198
  with gr.Column():
199
  input_image = gr.Image(
200
+ label="输入主视图",
201
  type="pil",
202
  height=400
203
  )
204
 
205
+ generate_btn = gr.Button("生成工程六视图", variant="primary", size="lg")
 
 
 
206
 
207
+ gr.Markdown("""
208
+ **注意:**
209
+ - 每个视图需要单独推理,总共需要约 3-5 分钟
210
+ - 请耐心等待生成过程完成
211
+ """)
212
 
213
  with gr.Column():
214
  output_image = gr.Image(
215
+ label="工程六视图输出 (2x3 布局)",
216
  type="pil",
217
  height=400
218
  )
219
 
 
 
 
 
 
 
 
 
 
220
  gr.Markdown("""
221
  ### 技术说明
222
+ - 模型: [Stable Zero123](https://huggingface.co/stabilityai/stable-zero123)
223
+ - 基于论文: Zero-1-to-3 (ICCV 2023)
224
+ - 每个视图独立生成,保证视角一致性
225
+ - 生成时间: 约 30-60 秒/视图,总计 3-5 分钟
226
 
227
  ### 引用
228
  ```bibtex
229
+ @inproceedings{liu2023zero,
230
+ title={Zero-1-to-3: Zero-shot One Image to 3D Object},
231
+ author={Liu, Ruoshi and Duan, Rundi and others},
232
+ booktitle={ICCV},
233
+ year={2023}
 
 
234
  }
235
  ```
236
  """)
 
246
 
247
  if __name__ == "__main__":
248
  # 预加载模型
249
+ print("=" * 50)
250
+ print("Zero123 工程六视图生成器")
251
+ print("=" * 50)
252
  load_model()
253
 
254
  # 启动 demo
255
  demo = create_demo()
256
+ demo.queue(max_size=5)
257
  demo.launch(
258
  server_name="0.0.0.0",
259
  server_port=7860,