File size: 8,278 Bytes
83f52e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from functools import partial
import os

import torch
import numpy as np
import gradio as gr
import gdown

from load import load_model, load_json
from load import load_unit_motion_embs_splits, load_keyids_splits


EXAMPLES = [
    "A person is walking in a circle",
    "A person is jumping rope",
    "Someone is doing a backflip",
    "A person is doing a moonwalk",
    "A person walks forward and then turns back",
    "Picking up an object",
    "A person is swimming in the sea",
    "A human is squatting",
    "Someone is jumping with one foot",
    "A person is chopping vegetables",
    "Someone walks backward",
    "Somebody is ascending a staircase",
    "A person is sitting down",
    "A person is taking the stairs",
    "Someone is doing jumping jacks",
    "The person walked forward and is picking up his toolbox",
    "The person angrily punching the air."
]

# Show closest text in the training


# css to make videos look nice
CSS = """
video {
    position: relative;
    margin: 0;
    box-shadow: var(--block-shadow);
    border-width: var(--block-border-width);
    border-color: var(--block-border-color);
    border-radius: var(--block-radius);
    background: var(--block-background-fill);
    width: 100%;
    line-height: var(--line-sm);
}
"""


def humanml3d_keyid_to_babel_rendered_url(h3d_index, amass_to_babel, keyid):
    # Don't show the mirrored version of HumanMl3D
    if "M" in keyid:
        return None

    dico = h3d_index[keyid]
    path = dico["path"]

    # HumanAct12 motions are not rendered online
    # so we skip them for now
    if "humanact12" in path:
        return None

    # This motion is not rendered in BABEL
    # so we skip them for now
    if path not in amass_to_babel:
        return None

    babel_id = amass_to_babel[path].zfill(6)
    url = f"https://babel-renders.s3.eu-central-1.amazonaws.com/{babel_id}.mp4"

    # For the demo, we retrieve from the first annotation only
    ann = dico["annotations"][0]
    start = ann["start"]
    end = ann["end"]
    text = ann["text"]

    data = {
        "url": url,
        "start": start,
        "end": end,
        "text": text,
        "keyid": keyid,
        "babel_id": babel_id
    }

    return data


def retrieve(model, keyid_to_url, all_unit_motion_embs, all_keyids, text, splits=["test"], nmax=8):
    unit_motion_embs = torch.cat([all_unit_motion_embs[s] for s in splits])
    keyids = np.concatenate([all_keyids[s] for s in splits])

    scores = model.compute_scores(text, unit_embs=unit_motion_embs)

    sorted_idxs = np.argsort(-scores)
    best_keyids = keyids[sorted_idxs]
    best_scores = scores[sorted_idxs]

    datas = []
    for keyid, score in zip(best_keyids, best_scores):
        if len(datas) == nmax:
            break

        data = keyid_to_url(keyid)
        if data is None:
            continue
        data["score"] = round(float(score), 2)
        datas.append(data)
    return datas


# HTML component
def get_video_html(url, video_id, start=None, end=None, score=None, width=350, height=350):
    trim = ""
    if start is not None:
        if end is not None:
            trim = f"#t={start},{end}"
        else:
            trim = f"#t={start}"

    score_t = ""
    if score is not None:
        score_t = f'title="Score = {score}"'

    video_html = f'''
<video preload="auto" muted playsinline onpause="this.load()"
autoplay loop disablepictureinpicture id="{video_id}" width="{width}" height="{height}" {score_t}>
  <source src="{url}{trim}" type="video/mp4">
  Your browser does not support the video tag.
</video>
'''
    return video_html


def retrive_component(retrieve_function, text, splits, nvids, n_component=16):
    # cannot produce more than n_compoenent
    nvids = min(nvids, n_component)
    if not splits:
        return [None for _ in range(n_component)]

    splits_l = [x.lower() for x in splits]
    datas = retrieve_function(text, splits=splits_l, nmax=nvids)
    htmls = [
        get_video_html(
            url["url"], idx, start=url["start"],
            end=url["end"], score=url["score"]
        )
        for idx, url in enumerate(datas)
    ]
    # get n_component exactly if asked less
    # pad with dummy blocks
    htmls = htmls + [None for _ in range(max(0, n_component-nvids))]
    return htmls


def main():
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

    # LOADING
    model = load_model(device)
    splits = ["train", "val", "test"]
    all_unit_motion_embs = load_unit_motion_embs_splits(splits, device)
    all_keyids = load_keyids_splits(splits)

    h3d_index = load_json("amass-annotations/humanml3d.json")
    amass_to_babel = load_json("amass-annotations/amass_to_babel.json")

    keyid_to_url = partial(humanml3d_keyid_to_babel_rendered_url, h3d_index, amass_to_babel)
    retrieve_function = partial(retrieve, model, keyid_to_url, all_unit_motion_embs, all_keyids)

    # DEMO
    theme = gr.themes.Default(primary_hue="blue", secondary_hue="gray")
    retrive_and_show = partial(retrive_component, retrieve_function)

    default_text = "A person is "

    with gr.Blocks(css=CSS, theme=theme) as demo:
        title = "<h1 style='text-align: center'>TMR: Text-to-Motion Retrieval Using Contrastive 3D Human Motion Synthesis </h1>"
        gr.Markdown(title)

        authors = """
        <h2 style='text-align: center'>
        <a href="https://mathis.petrovich.fr" target="_blank"><nobr>Mathis Petrovich</nobr></a> &emsp;
        <a href="https://ps.is.mpg.de/~black" target="_blank"><nobr>Michael J. Black</nobr></a> &emsp;
	<a href="https://imagine.enpc.fr/~varolg" target="_blank"><nobr>G&uumll Varol</nobr></a>
	</h2>
        """
        gr.Markdown(authors)

        conf = """
        <h2 style='text-align: center'>
	<nobr>arXiv 2023</nobr>
	</h2>
        """
        gr.Markdown(conf)

        videos = []

        with gr.Row():
            with gr.Column(scale=3):
                with gr.Column(scale=2):
                    text = gr.Textbox(placeholder="Type in natural language, the motion to retrieve",
                                      show_label=True, label="Text prompt", value=default_text)
                with gr.Column(scale=1):
                    btn = gr.Button("Retrieve", variant='primary')
                    clear = gr.Button("Clear", variant='secondary')

                with gr.Row():
                    with gr.Column(scale=1):
                        splits = gr.Dropdown(["Train", "Val", "Test"],
                                             value=["Test"], multiselect=True, label="Splits",
                                             info="HumanML3D data used for the motion database")
                    with gr.Column(scale=1):
                        nvideo_slider = gr.Slider(minimum=4, maximum=16, step=4, value=8, label="Number of videos")
            with gr.Column(scale=2):
                examples = gr.Examples(examples=EXAMPLES, inputs=text, examples_per_page=15)

        i = -1
        # should indent
        for _ in range(4):
            with gr.Row():
                for _ in range(4):
                    i += 1
                    with gr.Column():
                        video = gr.HTML()
                        videos.append(video)

        def check_error(splits):
            if not splits:
                raise gr.Error("At least one split should be selected!")
            return splits

        btn.click(fn=retrive_and_show, inputs=[text, splits, nvideo_slider], outputs=videos).then(
            fn=check_error, inputs=splits
        )

        text.submit(fn=retrive_and_show, inputs=[text, splits, nvideo_slider], outputs=videos).then(
            fn=check_error, inputs=splits
        )

        def keep_test(splits):
            if len(splits) == 0:
                return ["Test"]
            return splits

        def clear_videos():
            return [None for x in range(16)] + [default_text]

        clear.click(fn=clear_videos, outputs=videos + [text])
    demo.launch()


def prepare():
    if not os.path.exists("data"):
        gdown.download_folder("https://drive.google.com/drive/folders/1MgPFgHZ28AMd01M1tJ7YW_1-ut3-4j08", use_cookies=False)


if __name__ == "__main__":
    prepare()
    main()

# new
# A person is walking slowly