File size: 10,843 Bytes
2680bd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from typing import Dict, List
import numpy as np
from tqdm import tqdm
import math, json
import textwrap
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import gridspec
from matplotlib.font_manager import FontProperties
import matplotlib.gridspec as gridspec
from pygments import highlight
from pygments.lexers import JsonLexer
from pygments.formatter import Formatter
from pygments.styles import get_style_by_name
from pygments.token import Token

# NOTE: bihand_mano2mesh (from .mano2mesh) is imported lazily where used (mano viz mode only),
# so skeleton visualization works without the license-gated MANO model files.
from .mesh_visualizer import Mesh_Visualize_Helper
from .skeleton_visualizer import Skeleton_Visualize_Helper
from ..utils import smart_wrap

class MatplotlibFormatter(Formatter):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.data = []

    def format(self, tokensource, outfile):
        self.data = []
        for ttype, value in tokensource:
            style = self.style.style_for_token(ttype)
            color = style['color']
            if color:
                color = f"#{color}"
            else:

                style_bg_is_dark = self.style.background_color < '#777'
                color = '#ffffff' if style_bg_is_dark else '#000000'

            self.data.append((value, color, ttype))


class JsonPlotter:
    @staticmethod
    def plot_json(
        data_dict: dict | list,
        ax: plt.Axes,
        width: int = 80,
        style_name: str = 'monokai',
        font_family: str = 'monospace',
        font_size: int = 10
    ):

        json_str = json.dumps(data_dict, indent=4, ensure_ascii=False)
        lexer = JsonLexer()
        style = get_style_by_name(style_name)
        formatter = MatplotlibFormatter(style=style)
        highlight(json_str, lexer, formatter)

        ax.set_facecolor(style.background_color)

        fig = ax.get_figure()
        font = FontProperties(family=font_family, size=font_size)


        sample_text_obj = ax.text(0, 0, 'M', fontproperties=font, visible=False)

        try:
            fig.canvas.draw()

            text_bbox = sample_text_obj.get_window_extent()
            ax_bbox = ax.get_window_extent()

            if ax_bbox.width == 0 or text_bbox.width == 0:
                raise ValueError("Canvas has zero width, cannot calculate font metrics.")

            char_width = text_bbox.width / ax_bbox.width
            line_height = text_bbox.height / ax_bbox.height * 1.6

            print(f"Char Width: {char_width}, Line Height: {line_height}")
        except Exception:
            char_width = 0.006 * (font_size / 10)
            line_height = 0.025 * (font_size / 10)
        finally:
            sample_text_obj.remove()


        lines_of_tokens = []
        current_line = []
        current_char_count = 0

        wrapper = textwrap.TextWrapper(
            width=width,
            break_long_words=True,
            break_on_hyphens=False
        )

        for text, color, ttype in formatter.data:
            if text == '\n':
                lines_of_tokens.append(current_line)
                current_line = []
                current_char_count = 0
                continue

            is_string = ttype in Token.Literal.String
            if is_string and (current_char_count + len(text)) > width:
                leading_spaces = current_char_count
                wrapper.initial_indent = " " * 0

                wrapper.subsequent_indent = " " * (leading_spaces + 4)

                content = text[1:-1]
                wrapped_lines = wrapper.wrap(content)

                current_line.append((text[0], color))

                for i, line_content in enumerate(wrapped_lines):
                    if i == 0:

                        current_line.append((line_content, color))
                    else:
                        lines_of_tokens.append(current_line)

                        current_line = [ (line_content.lstrip(), color) ]


                current_line.append((text[-1], color))
            else:
                current_line.append((text, color))
                current_char_count += len(text)

        if current_line:
            lines_of_tokens.append(current_line)

        x_pos, y_pos = 0.02, 0.98
        left_margin = x_pos

        for line_tokens in lines_of_tokens:
            x_pos = left_margin
            for text, color in line_tokens:
                if not text: continue
                ax.text(x_pos, y_pos, text, color=color, fontproperties=font, ha='left', va='top')
                x_pos += len(text) * char_width
            y_pos -= line_height

        ax.axis('off')
        ax.set_ylim(0, 1)
        ax.set_xlim(0, 1)

class Visualize_Helper:
    @staticmethod
    def initialize():
        fig = plt.figure(figsize=(10, 10), layout='constrained')

        gs = gridspec.GridSpec(2, 2, figure=fig)
        skeleton_ax = fig.add_subplot(gs[0, 0], projection='3d')
        mano_ax = fig.add_subplot(gs[0, 1], projection='3d')
        annotation_ax = fig.add_subplot(gs[1, :])
        annotation_ax.axis("off")
        return fig, skeleton_ax, mano_ax, annotation_ax

    @staticmethod
    def format_plain_string(text:str, width:int=90):
        return '\n'.join(textwrap.wrap(text, width=width))


    @staticmethod
    def create_3d_animation(
        left_motion:np.ndarray, right_motion:np.ndarray,
        left_mano:Dict[str, np.ndarray]|None=None, right_mano:Dict[str, np.ndarray]|None=None,
        annotation:str | dict | list="",
        save_path:str|None=None, fps:int=30
    ):
        '''
        left_motion: (T, J, 3)
        right_motion: (T, J, 3)

        left_mano:
            shape: (T, 10)
            pose: (T, 48),
            trans: (T, 3)
        right_mano: the same as left_mano
        '''
        fig, skeleton_ax, mano_ax, annotation_ax = Visualize_Helper.initialize()

        print(f"Annotation Type: {type(annotation)}")

        if isinstance(annotation, dict) or isinstance(annotation, list):
            JsonPlotter.plot_json(annotation, annotation_ax)
        elif isinstance(annotation, str):
            print(f"annotation: {annotation.__repr__()}")
            annotation = '\n'.join(textwrap.wrap(annotation, width=90, replace_whitespace=False, tabsize=4))
            annotation_ax.text(
                x=0.1, y=0.95, s=annotation, ha='left', va='top', fontsize=14
            )
        frame_num = fig.text(
            x=0.5, y=0.95, s="", ha='center', va='top', fontsize=16
        )
        skeleton_helper = Skeleton_Visualize_Helper(skeleton_ax, left_motion, right_motion)
        skeleton_helper.initialize_ax()

        if left_mano is not None and right_mano is not None:
            from .mano2mesh import bihand_mano2mesh
            vertices, faces = bihand_mano2mesh(left_mano, right_mano)
            mesh_helper = Mesh_Visualize_Helper(mano_ax, vertices, faces)
            mesh_helper.initialize_ax()

        def update(frame):
            frame_num.set_text(f"Frame: {frame} / {left_motion.shape[0]}")
            skeleton_helper.draw(frame)
            if left_mano is not None and right_mano is not None:
                mesh_helper.draw_mesh(frame)

        ani = FuncAnimation(fig, update, frames=left_motion.shape[0])

        if save_path:
            pbar = tqdm(total=left_motion.shape[0], desc='Exporting animation')
            ani.save(save_path, writer='ffmpeg', fps=fps, progress_callback=lambda x, y: pbar.update(1))


        plt.close(fig)

class MultiMotionVisualizer:

    def initialize(number_of_motions:int):
        rows = int(math.floor(math.sqrt(number_of_motions)))
        columns = int(math.ceil(number_of_motions / rows))
        fig = plt.figure(figsize=(4 * columns, 4 * rows + 8))
        gs = gridspec.GridSpec(rows + 1, columns, figure=fig, height_ratios=[4] * rows + [8])
        motion_axes = []
        for i in range(number_of_motions):
            ax = fig.add_subplot(gs[i // columns, i % columns], projection='3d')
            motion_axes.append(ax)
        motion_axes = np.array(motion_axes)

        text_ax = fig.add_subplot(gs[rows, :])
        text_ax.axis("off")

        return fig, motion_axes, text_ax

    @staticmethod
    def create_3d_animation(
        motions:List[Dict[str, np.ndarray | str | dict]],
        text:str="",
        save_path:str=None, fps:int=30
    ):
        '''
        if motion['type'] == 'skeleton'
            motion = {
                'type': 'skeleton',
                'left_motion': (T, J, 3),
                'right_motion': (T, J, 3)
            }
        elif motion['type'] == 'mano'
            motion = {
                'type': 'mano',
                'left_motion': dict
                'right_motion': dict'
            }
        '''
        fig, axes, text_ax = MultiMotionVisualizer.initialize(len(motions))

        drawers = []
        frame_num = 0
        for motion_id, motion in enumerate(motions):
            if motion['type'] == 'skeleton':
                helper = Skeleton_Visualize_Helper(
                    axes[motion_id], motion['left_motion'], motion['right_motion'], motion.get('title', None)
                )
                frame_num = max(frame_num, motion['left_motion'].shape[0])
            elif motion['type'] == 'mano':
                from .mano2mesh import bihand_mano2mesh
                vertices, faces = bihand_mano2mesh(
                    motion['left_motion'], motion['right_motion']
                )
                helper = Mesh_Visualize_Helper(
                    axes[motion_id], vertices, faces, motion.get('title', None)
                )
                frame_num = max(vertices.shape[0], frame_num)
            helper.initialize_ax()
            drawers.append(helper)

        frame_num_text = fig.text(
            x=0.5, y=0.95, s="", ha='center', va='top', fontsize=16
        )

        # text = '\n'.join(textwrap.wrap(text, width=90))
        text = smart_wrap(text, width=90)

        text_ax.text(
            x=0.01, y=0.5, s=text, ha='left', va='center', fontsize=14
        )

        def update(frame):
            frame_num_text.set_text(f"Frame: {frame} / {frame_num}")
            for drawer in drawers:
                if isinstance(drawer, Skeleton_Visualize_Helper):
                    drawer.draw(frame)
                elif isinstance(drawer, Mesh_Visualize_Helper):
                    drawer.draw_mesh(frame)

        ani = FuncAnimation(fig, update, frames=frame_num)

        if save_path:
            name = save_path.split('/')[-1]
            pbar = tqdm(total=frame_num, desc=f'Exporting animation to {name}')
            ani.save(save_path, writer='ffmpeg', fps=fps, progress_callback=lambda x, y: pbar.update(1))

        plt.close(fig)