File size: 2,342 Bytes
9180c95
 
 
 
 
e45131f
 
 
9180c95
e45131f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9180c95
 
 
e45131f
 
 
9180c95
e45131f
 
 
 
 
9180c95
 
e45131f
9180c95
 
 
 
e45131f
 
9180c95
 
 
 
e45131f
 
 
 
 
 
9180c95
e45131f
9180c95
 
 
 
 
e45131f
 
 
 
 
9180c95
 
e45131f
 
 
 
 
 
 
 
9180c95
 
 
e45131f
 
9180c95
 
e45131f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# app.py
import gradio as gr
import torch
import numpy as np
from PIL import Image
from transformers import AutoModel
import warnings
warnings.filterwarnings('ignore')

# 全局变量存储模型实例
model = None

def initialize_model():
    global model
    try:
        if model is None:
            model = AutoModel.from_pretrained(
                "jadechoghari/vfusion3d", 
                trust_remote_code=True,
                device_map="auto"  # 自动处理设备分配
            )
    except Exception as e:
        print(f"模型加载错误: {str(e)}")
        return None
    return model

def process_image(input_image):
    if input_image is None:
        return None, "请上传图片"
    
    try:
        # 初始化模型
        model = initialize_model()
        if model is None:
            return None, "模型加载失败"

        # 确保输入图像是PIL Image格式
        if not isinstance(input_image, Image.Image):
            input_image = Image.fromarray(np.uint8(input_image))
        
        # 图像预处理
        input_image = input_image.resize((256, 256))
        
        # 转换为tensor并归一化
        image_tensor = torch.from_numpy(np.array(input_image)).float() / 255.0
        image_tensor = image_tensor.permute(2, 0, 1).unsqueeze(0)
        
        # 模型推理
        with torch.no_grad():
            try:
                output = model(image_tensor)
                return output, "处理成功"
            except Exception as e:
                return None, f"模型推理错误: {str(e)}"
                
    except Exception as e:
        return None, f"处理错误: {str(e)}"

# 创建Gradio界面
demo = gr.Interface(
    fn=process_image,
    inputs=[
        gr.Image(
            type="pil",
            label="上传图片",
            tool="select"
        )
    ],
    outputs=[
        gr.Model3D(
            label="生成的3D模型",
            clear_color=[0.0, 0.0, 0.0, 0.0]
        ),
        gr.Textbox(
            label="处理状态",
            placeholder="等待处理..."
        )
    ],
    title="麒迹云台 - 2D转3D模型生成器",
    description="上传一张图片,AI将自动生成对应的3D模型。支持格式:jpg, png, jpeg",
    theme=gr.themes.Soft(),
    allow_flagging="never"
)

# 启动应用
demo.launch()