File size: 2,658 Bytes
84597aa
47bf5f3
d69cecb
 
 
c617047
 
 
 
 
 
 
 
 
 
 
d69cecb
47bf5f3
89c24f0
61c79bc
 
 
89c24f0
61c79bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb9535
61c79bc
cfb9535
61c79bc
 
 
 
d69cecb
61c79bc
 
89c24f0
c617047
 
 
 
 
f244b86
7067c12
c617047
47bf5f3
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
import torch
import sys
import numpy as np
import tempfile

try:
    torch.ones(1, 1).cuda().float()
    sys.path.insert(0,"/home/user/app/wrapper")
    sys.path.append("/home/user/app/Dynamic3DGaussians")
    import Dynamic3DGaussians.visualize
    Dynamic3DGaussians.visualize.FORCE_LOOP = True
    from Dynamic3DGaussians.visualize import visualize
    import open3d as o3d
    IS_CPU = False
except Exception as e:
    print(f"Error: {e}")
    IS_CPU = True

import gradio as gr
import cv2
from threading import Thread
import os

def video_thread(self):
    if self.counter != -1:
        file_path_video, file_path_pcd, file_path_png = self.callback_thread_params
        if self.videoWriter is None:
            self.counter = 200
            fourcc = cv2.VideoWriter_fourcc(*'mp4v')
            self.videoSize = (640, 360)
            self.videoWriter = cv2.VideoWriter(file_path_video, fourcc, 10, self.videoSize)
            pcd = self.lastPcd
            if not o3d.io.write_point_cloud(file_path_pcd, pcd):
                result = "ERROR #2"
        visualizer = self.delegate
        visualizer.capture_screen_image(file_path_png,do_render=True)
        img = cv2.imread(file_path_png)
        img = cv2.resize(img, self.videoSize)
        self.videoWriter.write(img)
        self.counter -= 1
        if self.counter == 0:
            self.counter = -1
            self.videoWriter.release()
            print("videoWriter released")

def do_visualize(what, file_path_video, file_path_pcd, file_path_png):
    o3d.callback_thread = video_thread
    o3d.callback_thread_params = (file_path_video, file_path_pcd, file_path_png)
    visualize(what, "pretrained")

def run_simulation(what):
    temp_name = next(tempfile._get_candidate_names())
    file_path_video = f"/tmp/{temp_name}.mp4"
    file_path_pcd = f"/tmp/{temp_name}.pcd"
    file_path_png = f"/tmp/{temp_name}.png"
    print("start thread")
    do_visualize(what, file_path_video, file_path_pcd, file_path_png)
    print("done")
    result = "Done"
    #TODO return path to FIFO-video file
    return file_path_video, file_path_pcd, file_path_png, result

def do_nothing():
    return "Not possible on CPU"

iface = None
if IS_CPU:
    iface = gr.Interface(fn=do_nothing, inputs=None, outputs=[gr.Textbox(label="Error", value="Not possible on CPU")], allow_flagging="never")
else:
    iface = gr.Interface(fn=run_simulation, inputs=gr.Dropdown(["basketball", "boxes", "football", "juggle", "softball", "tennis"], value="basketball"), outputs=[gr.Video(format="mp4"), gr.File(), gr.Image(), gr.Textbox(label="debug output")], allow_flagging="never")
iface.launch(server_name="0.0.0.0")