File size: 9,756 Bytes
480e656
 
 
 
 
 
 
 
 
98dd4d1
 
b94a387
fe8ccea
d38ca73
27d9c8d
 
fe8ccea
98dd4d1
 
 
3854f27
98dd4d1
199cd31
480e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d5435
 
 
 
480e656
 
 
 
85d5435
5b1fe75
 
 
 
 
 
85d5435
 
 
 
 
 
 
480e656
 
 
 
151e5f8
480e656
 
 
 
 
aeb7259
 
 
480e656
 
 
aeb7259
 
 
85d5435
480e656
 
 
 
 
 
 
151e5f8
480e656
 
 
 
aeb7259
 
 
480e656
 
 
aeb7259
 
 
85d5435
aeb7259
480e656
 
 
 
 
 
151e5f8
480e656
 
 
 
aeb7259
 
 
480e656
 
 
aeb7259
 
 
85d5435
480e656
 
 
 
 
 
 
ad7c9e8
480e656
 
 
 
 
 
 
 
 
 
 
 
 
85d5435
480e656
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d5435
 
 
 
 
480e656
 
 
 
 
85d5435
 
 
 
 
 
 
 
 
 
346ee2d
480e656
 
 
 
 
 
ccb577d
480e656
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import spaces
import os
import subprocess
import tempfile
import uuid
import glob
import shutil
import time
import gradio as gr
import sys


# Set environment variables
os.environ["PIXEL3DMM_CODE_BASE"] = f"{os.getcwd()}"
os.environ["PIXEL3DMM_PREPROCESSED_DATA"] = f"{os.getcwd()}/proprocess_results"
os.environ["PIXEL3DMM_TRACKING_OUTPUT"] = f"{os.getcwd()}/tracking_results"

def sh(cmd): subprocess.check_call(cmd, shell=True)

# only do this once per VM restart
sh("pip install -e .")
sh("cd src/pixel3dmm/preprocessing/facer && pip install -e .")
sh("cd src/pixel3dmm/preprocessing/PIPNet/FaceBoxesV2/utils && sh make.sh")


# Utility to stitch frames into a video
def make_video_from_frames(frames_dir, out_path, fps=15):
    if not os.path.isdir(frames_dir):
        return None
    files = glob.glob(os.path.join(frames_dir, "*.jpg")) + glob.glob(os.path.join(frames_dir, "*.png"))
    if not files:
        return None
    ext = files[0].split('.')[-1]
    pattern = os.path.join(frames_dir, f"%05d.{ext}")
    subprocess.run([
        "ffmpeg", "-y", "-i", pattern,
        "-r", str(fps), out_path
    ], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    return out_path

# Function to probe video for duration and frame rate
def get_video_info(video_path):
    """
    Probes the uploaded video and returns updated slider configs:
    - seconds slider: max = int(duration)
    - fps slider:    max = int(orig_fps)
    """
    if not video_path:
        # Return default slider updates when no video is uploaded
        return gr.update(maximum=10, value=3, step=1), gr.update(maximum=30, value=15, step=1)

    # Use ffprobe to get JSON metadata
    cmd = [
        "ffprobe", "-v", "quiet",
        "-print_format", "json",
        "-show_streams", video_path
    ]
    res = subprocess.run(cmd, capture_output=True, text=True)
    try:
        import json
        data = json.loads(res.stdout)
        stream = next(s for s in data.get('streams', []) if s.get('codec_type') == 'video')
        duration = float(stream.get('duration') or data.get('format', {}).get('duration', 0))
        fr = stream.get('r_frame_rate', '0/1')
        num, den = fr.split('/')
        orig_fps = float(num) / float(den) if float(den) else 30
    except Exception:
        duration, orig_fps = 10, 30

    # Configure sliders based on actual video properties
    seconds_cfg = gr.update(maximum=int(duration), value=min(int(duration), 3), step=1)
    fps_cfg     = gr.update(maximum=int(orig_fps), value=min(int(orig_fps), 15), step=1)
    return seconds_cfg, fps_cfg

# Step 1: Trim video based on user-defined duration and fps based on user-defined duration and fps
def step1_trim(video_path, seconds, fps, state):
    session_id = str(uuid.uuid4())
    base_dir = os.path.join(os.environ["PIXEL3DMM_PREPROCESSED_DATA"], session_id)
    state.update({"session_id": session_id, "base_dir": base_dir})

    tmp = tempfile.mkdtemp()
    trimmed = os.path.join(tmp, f"{session_id}.mp4")
    
    try:
        # capture both stdout & stderr
        p = subprocess.run([
        "ffmpeg", "-y", "-i", video_path,
        "-t", str(seconds),  # user-specified duration
        "-r", str(fps),      # user-specified fps
        trimmed
        ], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

        all_output = []
        
        for line in p.stdout:
            print(line, end="")   # real-time echo
            all_output.append(line)
        
    except subprocess.CalledProcessError as e:
        # e.stdout contains everything
        err = f"❌ Preprocess failed (exit {e.returncode}).\n\n{e.stdout}"
        return err, None, state


    state["trimmed_path"] = trimmed
    return f"βœ… Step 1: Trimmed to {seconds}s @{fps}fps", state

# Step 2: Preprocessing β†’ cropped video
@spaces.GPU()
def step2_preprocess(state):
    session_id = state["session_id"]
    base_dir = state["base_dir"]
    trimmed = state["trimmed_path"]

    try:
        # capture both stdout & stderr
        p =     subprocess.run([
        "python", "scripts/run_preprocessing.py",
        "--video_or_images_path", trimmed
    ], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        # e.stdout contains everything
        err = f"❌ Preprocess failed (exit {e.returncode}).\n\n{e.stdout}"
        return err, None, state

    crop_dir = os.path.join(base_dir, "cropped")
    out = os.path.join(os.path.dirname(trimmed), f"crop_{session_id}.mp4")
    video = make_video_from_frames(crop_dir, out)
    return "βœ… Step 2: Preprocessing complete", video, state

# Step 3: Normals inference β†’ normals video
@spaces.GPU()
def step3_normals(state):
    session_id = state["session_id"]
    base_dir = state["base_dir"]

    try:
        # capture both stdout & stderr
        p = subprocess.run([
        "python", "scripts/network_inference.py",
        "model.prediction_type=normals", f"video_name={session_id}"
    ], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        # e.stdout contains everything
        err = f"❌ Normal map failed (exit {e.returncode}).\n\n{e.stdout}"
        return err, None, state
        
    normals_dir = os.path.join(base_dir, "p3dmm", "normals")
    out = os.path.join(os.path.dirname(state["trimmed_path"]), f"normals_{session_id}.mp4")
    video = make_video_from_frames(normals_dir, out)
    return "βœ… Step 3: Normals inference complete", video, state

# Step 4: UV map inference β†’ uv map video
@spaces.GPU()
def step4_uv_map(state):
    session_id = state["session_id"]
    base_dir = state["base_dir"]

    try:
        # capture both stdout & stderr
        p = subprocess.run([
        "python", "scripts/network_inference.py",
        "model.prediction_type=uv_map", f"video_name={session_id}"
    ], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        # e.stdout contains everything
        err = f"❌ UV map failed (exit {e.returncode}).\n\n{e.stdout}"
        return err, None, state

    uv_dir = os.path.join(base_dir, "p3dmm", "uv_map")
    out = os.path.join(os.path.dirname(state["trimmed_path"]), f"uv_map_{session_id}.mp4")
    video = make_video_from_frames(uv_dir, out)
    return "βœ… Step 4: UV map inference complete", video, state

# Step 5: Tracking β†’ final tracking video
@spaces.GPU()
def step5_track(state):
    session_id = state["session_id"]
    script = os.path.join(os.environ["PIXEL3DMM_CODE_BASE"], "scripts", "track.py")
    cmd = [
        "python", script,
        f"video_name={session_id}"
    ]
    try:
        # capture both stdout & stderr
        p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=True)
    except subprocess.CalledProcessError as e:
        # e.stdout contains everything
        err = f"❌ Tracking failed (exit {e.returncode}).\n\n{e.stdout}"
        return err, None, state

    # if we get here, it succeeded:
    tracking_dir = os.path.join(os.environ["PIXEL3DMM_TRACKING_OUTPUT"], session_id, "frames")
    out = os.path.join(os.path.dirname(state["trimmed_path"]), f"result_{session_id}.mp4")
    video = make_video_from_frames(tracking_dir, out)
    return "βœ… Step 5: Tracking complete", video, state

# Build Gradio UI
demo = gr.Blocks()

with demo:
    gr.Markdown("## Video Processing Pipeline")
    with gr.Row():
        with gr.Column():
            video_in = gr.Video(label="Upload video", height=512)
            # Sliders for duration and fps
            seconds_slider = gr.Slider(label="Duration (seconds)", minimum=2, maximum=10, step=1, value=3)
            fps_slider     = gr.Slider(label="Frame Rate (fps)", minimum=15, maximum=30, step=1, value=15)
            status         = gr.Textbox(label="Status", lines=2, interactive=False)
            state          = gr.State({})
        with gr.Column():
            with gr.Row():
                crop_vid    = gr.Video(label="Preprocessed", height=256)
                normals_vid = gr.Video(label="Normals", height=256)
            with gr.Row():
                uv_vid      = gr.Video(label="UV Map", height=256)
                track_vid   = gr.Video(label="Tracking", height=256)
    run_btn_1 = gr.Button("Run Pipeline 1")
    run_btn_2 = gr.Button("Run Pipeline 2")
    run_btn_3 = gr.Button("Run Pipeline 3")
    run_btn_4 = gr.Button("Run Pipeline 4")
    run_btn_5 = gr.Button("Run Pipeline 5")

    # Update sliders after video upload
    video_in.change(fn=get_video_info, inputs=video_in, outputs=[seconds_slider, fps_slider])

    # Pipeline execution
    run_btn_1.click(fn=step1_trim, inputs=[video_in, seconds_slider, fps_slider, state], outputs=[status, state])
    run_btn_2.click(fn=step2_preprocess, inputs=[state], outputs=[status, crop_vid, state])
    run_btn_3.click(fn=step3_normals, inputs=[state], outputs=[status, normals_vid, state])
    run_btn_4.click(fn=step4_uv_map, inputs=[state], outputs=[status, uv_vid, state])
    run_btn_5.click(fn=step5_track, inputs=[state], outputs=[status, track_vid, state])
    
         # .then(fn=step2_preprocess, inputs=[state], outputs=[status, crop_vid, state])
         # .then(fn=step3_normals,    inputs=[state], outputs=[status, normals_vid, state])
         # .then(fn=step4_uv_map,     inputs=[state], outputs=[status, uv_vid, state])
         # .then(fn=step5_track,      inputs=[state], outputs=[status, track_vid, state])
    

# ------------------------------------------------------------------
# START THE GRADIO SERVER
# ------------------------------------------------------------------
demo.queue()

demo.launch(share=True, ssr_mode=False)