File size: 2,457 Bytes
876b58d
9a9f462
 
 
 
 
 
 
 
 
876b58d
9a9f462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b222ac
9a9f462
 
 
 
 
 
9b222ac
 
b62fa8c
9a9f462
 
9b222ac
b62fa8c
 
 
9a9f462
 
 
b62fa8c
b1b52ab
9a9f462
876b58d
9a9f462
876b58d
9a9f462
 
 
 
 
 
9b222ac
 
876b58d
9b222ac
 
 
 
 
876b58d
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
# [Previous imports and utility functions remain exactly the same until image_to_3d]

def image_to_3d(
    image: Image.Image,
    seed: int,
    ss_guidance_strength: float,
    ss_sampling_steps: int,
    slat_guidance_strength: float,
    slat_sampling_steps: int,
    req: gr.Request,
) -> Tuple[dict, str, str]:
    """
    Convert an image to a 3D model.
    """
    user_dir = os.path.join(TMP_DIR, str(req.session_hash))
    outputs = pipeline.run(
        image,
        seed=seed,
        formats=["gaussian", "mesh"],
        preprocess_image=False,
        sparse_structure_sampler_params={
            "steps": ss_sampling_steps,
            "cfg_strength": ss_guidance_strength,
        },
        slat_sampler_params={
            "steps": slat_sampling_steps,
            "cfg_strength": slat_guidance_strength,
        },
    )
    
    video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
    video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
    video = [np.concatenate([video[i], video_geo[i]], axis=1) for i in range(len(video))]
    trial_id = str(uuid.uuid4())
    video_path = os.path.join(user_dir, f"{trial_id}.mp4")
    imageio.mimsave(video_path, video, fps=15)
    
    # Generate full quality GLB
    glb = postprocessing_utils.to_glb(
        outputs['gaussian'][0], 
        outputs['mesh'][0],
        simplify=0.0,  # No simplification
        fill_holes=True,
        fill_holes_max_size=0.04,
        texture_size=2048,  # Maximum texture size
        verbose=False
    )
    full_glb_path = os.path.join(user_dir, f"{trial_id}_full.glb")
    glb.export(full_glb_path)
    
    state = pack_state(outputs['gaussian'][0], outputs['mesh'][0], trial_id)
    return state, video_path, full_glb_path

# [Rest of the code remains exactly the same, except for the event handler which needs to be updated]

    generate_btn.click(
        get_seed,
        inputs=[randomize_seed, seed],
        outputs=[seed],
    ).then(
        image_to_3d,
        inputs=[image_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps],
        outputs=[output_buf, video_output, download_full],
    ).then(
        lambda: [gr.Button(interactive=True), gr.Button(interactive=True), gr.Button(interactive=False)],
        outputs=[download_full, extract_glb_btn, download_reduced],
    )

# [Rest of the code remains exactly the same]