File size: 4,896 Bytes
8b9a89c
b22cadc
 
 
 
 
 
36c9090
66caa64
b22cadc
 
 
 
a2a921c
ed4a617
a2a921c
b22cadc
 
 
 
 
 
7a61832
 
b22cadc
 
 
 
fed6a50
a2a921c
b22cadc
a2a921c
 
0b41cb2
c051b4f
 
b22cadc
fed6a50
a2a921c
 
 
fed6a50
7a61832
fed6a50
7c25ff4
d2b53c9
fed6a50
 
66caa64
fed6a50
66caa64
 
a2a921c
 
 
e863be9
 
 
 
a2a921c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b845d1
66caa64
a2a921c
66caa64
 
a2a921c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66caa64
a2a921c
66caa64
 
a2a921c
 
 
 
 
 
 
9450742
e863be9
fed6a50
e863be9
b22cadc
e40fa87
 
 
 
 
 
 
fed6a50
7a61832
1b597ee
9450742
7a61832
 
7b845d1
 
e40fa87
b22cadc
e40fa87
b22cadc
c3e5515
 
e40fa87
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
import os
import gradio as gr
import numpy as np
import tempfile
import shutil
import hashlib
import subprocess
import time

TMP_DIR = os.path.join(tempfile.gettempdir(), 'gradio')
os.makedirs(TMP_DIR, exist_ok=True)

gs_train_iter = 1000
pose_lr = '1x'


def move_uploaded_files(files, temp_dir):
    saved_files = []
    for file in files:
        target_file_path = os.path.join(temp_dir, os.path.basename(file.name))
        saved_files.append(target_file_path)
        if os.path.exists(file.name):
            shutil.move(file.name, target_file_path)
    return saved_files


def process_images(files):
    n_view = len(files)
    
    files_str = ','.join([f.name for f in files])
    hashdir_name = hashlib.md5(files_str.encode('utf8')).hexdigest()
    hashdir_path = os.path.join(TMP_DIR, hashdir_name)
    hashdir_path_img = os.path.join(hashdir_path, 'images')
    os.makedirs(hashdir_path_img, exist_ok=True)
    saved_files = move_uploaded_files(files, hashdir_path_img)
    saved_files_str = ' '.join(saved_files)

    stdout, stderr, = [], []
    
    # ----- (1) Dust3r_coarse_geometric_initialization -----
    command = [
        "python", "-W", "ignore",
        "./coarse_init_infer.py",
        "--img_base_path", hashdir_path,
        "--n_views", str(n_view),
        "--focal_avg",
    ]
    start_time = time.time()
    result = subprocess.run(command, capture_output=True, text=True)
    end_time = time.time()
    stdout.append(f"Time taken for (1) Dust3r_coarse_geometric_initialization: {end_time-start_time} sec.")
    stdout.append(result.stdout)
    stderr.append(result.stderr)
    # ----- (2) Train: jointly optimize pose -----
    scene = 'scene'
    subdir = f'{n_view}_views_{gs_train_iter}Iter_{pose_lr}PoseLR'
    model_path = os.path.join(hashdir_path, 'infer', scene, subdir)
    
    # python -W ignore ./train_joint.py \
    #         -s ${SOURCE_PATH} \
    #         -m ${MODEL_PATH}  \
    #         --n_views ${N_VIEW}  \
    #         --scene ${SCENE} \
    #         --iter ${gs_train_iter} \
    #         --optim_pose \
    
    command = [
        "python", "-W", "ignore",
        "./train_joint.py",
        "-s", hashdir_path,
        "-m", model_path,
        "--n_views", str(n_view),
        "--scene", scene,
        "--iter", str(gs_train_iter),
        "--optim_pose",
    ]

    start_time = time.time()
    result = subprocess.run(command, capture_output=True, text=True)
    end_time = time.time()
    stdout.append(f"Time taken for (2) Train: jointly optimize pose: {end_time-start_time} sec.")
    stdout.append(result.stdout)
    stderr.append(result.stderr)

    # ----- (3) Render interpolated pose & output video -----
    # python -W ignore ./render_by_interp.py \
    #         -s ${SOURCE_PATH} \
    #         -m ${MODEL_PATH}  \
    #         --n_views ${N_VIEW}  \
    #         --scene ${SCENE} \
    #         --iter ${gs_train_iter} \
    #         --eval \
    #         --get_video
    
    command = [
        "python", "-W", "ignore",
        "./render_by_interp.py",
        "-s", hashdir_path,
        "-m", model_path,
        "--n_views", str(n_view),
        "--scene", scene,
        "--iter", str(gs_train_iter),
        "--eval",
        "--get_video",
    ]
    
    start_time = time.time()
    result = subprocess.run(command, capture_output=True, text=True)
    end_time = time.time()
    stdout.append(f"Time taken for (3) Render interpolated pose & output video: {end_time-start_time} sec.")
    stdout.append(result.stdout)
    stderr.append(result.stderr)

    stdout = '\n'.join(stdout)
    stderr = '\n'.join(stderr)
    
    result_tree = subprocess.run(["tree", hashdir_path], capture_output=True, text=True)
    ply_file = os.path.join(model_path, 'point_cloud', f'iteration_{gs_train_iter}', 'point_cloud.ply')
    mp4_file = os.path.join(model_path, f'{scene}_{n_view}_view.mp4')
    
    return mp4_file, ply_file, result_tree.stdout, stdout, stderr

def run():
    with gr.Blocks() as demo:
        gr.Markdown("Image and Video Processor")
        # Gradio interface
        inputs = gr.File(file_count="multiple", file_types=["image"], label="Upload Images")
        btn = gr.Button(value="Run")
        # outputs = [gr.Image(type="numpy", label="Processed Images", interactive=False), gr.Video(label="Output Video")]    
        # outputs = [gr.Textbox(label="Log"), gr.Image(type="filepath", label="Processed Images", interactive=False)]    
        outputs = [
            gr.Video(label="Output Video"),
            gr.Model3D(label="3D Model"),
            gr.Textbox(label="Output dirs"), 
            gr.Textbox(label="Log (stdout)"), 
            gr.Textbox(label="Log (stderr)"),
        ]    
        btn.click(fn=process_images, inputs=inputs, outputs=outputs)

    demo.launch(server_name="0.0.0.0", server_port=7860)


if __name__ == "__main__":
    run()