File size: 6,826 Bytes
434b0b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
# @Organization  : Tongyi Lab, Alibaba
# @Author        : Lingteng Qiu
# @Email         : 220019047@link.cuhk.edu.cn
# @Time          : 2025-10-20 10:00:00
# @Function      : LHM++ Inference Utils Tool

import base64
import glob
import json
import os
import tempfile
from typing import Dict, List, Optional

import gradio as gr
import numpy as np
import torch
from PIL import Image

SMPLX_EXPRESSION_DIM = 100
from contextlib import contextmanager


def get_smplx_params(
    data: Dict[str, torch.Tensor],
    device: torch.device,
) -> Dict[str, torch.Tensor]:
    """Extract and prepare SMPL-X parameters for model input.

    Filters relevant SMPL-X parameter keys from the input data dictionary
    and moves them to the specified device with an added batch dimension.

    Args:
        data: Dictionary containing SMPL-X parameters and other data.
        device: Target device (e.g., 'cuda' or 'cpu') for the parameters.

    Returns:
        Dictionary containing only SMPL-X parameters with shape (1, ...)
        on the specified device.
    """
    smplx_keys = [
        "root_pose",
        "body_pose",
        "jaw_pose",
        "leye_pose",
        "reye_pose",
        "lhand_pose",
        "rhand_pose",
        "expr",
        "trans",
        "betas",
    ]

    smplx_params = {k: data[k].unsqueeze(0).to(device) for k in smplx_keys if k in data}

    return smplx_params


def obtain_motion_sequence(motion_dir: str) -> List[Dict[str, torch.Tensor]]:
    """Load motion sequence data from SMPL-X and FLAME parameter files.

    Reads SMPL-X parameter JSON files from the specified directory and optionally
    merges them with corresponding FLAME parameters for facial expressions and poses.

    Args:
        motion_dir: Path to directory containing SMPL-X parameter JSON files.

    Returns:
        List of dictionaries containing SMPL-X parameters as torch tensors.
        Each dictionary includes body pose, facial expressions, and eye poses.
    """
    motion_files = sorted(glob.glob(os.path.join(motion_dir, "*.json")))
    smplx_list = []

    for motion_file in motion_files:
        # Load SMPL-X parameters
        with open(motion_file, "r") as f:
            smplx_params = json.load(f)

        # Try to load corresponding FLAME parameters
        flame_path = motion_file.replace("smplx_params", "flame_params")
        if os.path.exists(flame_path):
            with open(flame_path, "r") as f:
                flame_params = json.load(f)

            # Override SMPL-X parameters with FLAME data
            smplx_params["expr"] = torch.FloatTensor(flame_params["expcode"])
            smplx_params["jaw_pose"] = torch.FloatTensor(flame_params["posecode"][3:])
            smplx_params["leye_pose"] = torch.FloatTensor(flame_params["eyecode"][:3])
            smplx_params["reye_pose"] = torch.FloatTensor(flame_params["eyecode"][3:])
        else:
            # Use zero expressions if FLAME params not available
            # smplx_params["expr"] = torch.zeros(SMPLX_EXPRESSION_DIM)
            # Using SMPLX expression
            pass

        smplx_list.append(smplx_params)

    return smplx_list


def assert_input_image(input_image: Optional[np.ndarray]) -> None:
    """Validate input image is not None.

    Args:
        input_image: Input image array to validate.

    Raises:
        gr.Error: If input image is None.
    """
    if input_image is None:
        raise gr.Error("No image selected or uploaded!")


def prepare_working_dir() -> tempfile.TemporaryDirectory:
    """Create a temporary working directory.

    Returns:
        Temporary directory object.
    """
    return tempfile.TemporaryDirectory()


def init_preprocessor() -> None:
    """Initialize the global preprocessor for image preprocessing."""
    from core.utils.preprocess import Preprocessor

    global preprocessor
    preprocessor = Preprocessor()


def preprocess_fn(
    image_in: np.ndarray,
    remove_bg: bool,
    recenter: bool,
    working_dir: tempfile.TemporaryDirectory,
) -> str:
    """Preprocess input image with optional background removal and recentering.

    Args:
        image_in: Input image as numpy array.
        remove_bg: Whether to remove background.
        recenter: Whether to recenter the subject.
        working_dir: Temporary directory for storing intermediate files.

    Returns:
        Path to the preprocessed image.

    Raises:
        AssertionError: If preprocessing fails.
    """
    image_raw = os.path.join(working_dir.name, "raw.png")
    with Image.fromarray(image_in) as img:
        img.save(image_raw)

    image_out = os.path.join(working_dir.name, "rembg.png")
    success = preprocessor.preprocess(
        image_path=image_raw, save_path=image_out, rmbg=remove_bg, recenter=recenter
    )

    if not success:
        raise RuntimeError("Preprocessing failed")

    return image_out


def get_image_base64(path: str) -> str:
    """Convert image file to base64 encoded string.

    Args:
        path: Path to image file.

    Returns:
        Base64 encoded image string with data URI prefix.
    """
    with open(path, "rb") as f:
        encoded_string = base64.b64encode(f.read()).decode()
    return f"data:image/png;base64,{encoded_string}"


def get_available_device() -> str:
    """Returns the current CUDA device or ``"cpu"`` if CUDA is unavailable.

    When CUDA is available, uses the device ID from ``torch.cuda.current_device()``
    so the caller runs on the same device as the active CUDA context.

    Returns:
        str: Device string (e.g., ``"cuda:0"`` or ``"cpu"``).
    """
    if torch.cuda.is_available():
        current_device_id = torch.cuda.current_device()
        device = f"cuda:{current_device_id}"
    else:
        device = "cpu"
    return device


@contextmanager
def easy_memory_manager(model: torch.nn.Module, device: str = "cuda"):
    """Context manager that moves a model to GPU during use and back to CPU afterward.

    Reduces GPU memory footprint by transferring the model off CUDA and clearing
    the cache when the block exits. Use when running inference in memory-constrained
    environments.

    Args:
        model (torch.nn.Module): The model to manage. Will be moved to the current
            CUDA device (or CPU if unavailable) for the duration of the context.
        device (str, optional): Unused; kept for API compatibility. Default: ``"cuda"``.

    Yields:
        torch.nn.Module: The model, ready for use on the target device.

    Example:
        >>> with easy_memory_manager(model):
        ...     output = model(input_tensor)
    """
    device = get_available_device()
    model.to(device)
    try:
        yield model
    finally:
        model.to("cpu")
        if torch.cuda.is_available():
            torch.cuda.empty_cache()