CoolFace
Apppublic

yslan/ObjCtrl-2.5D

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
10likes
customized_cam.py90 linesDownload Raw Back to utils
1import numpy as np2 3def rotation(num_poses, rotation_angle=360, radius=1.0, height=0.81):4    '''5    Input:6        num_poses: number of poses7        rotation_angle: angle of rotation in degrees8        radius: radius of rotation9        height: height of the camera above the ground10        11    Output:12        poses: list of rotation matrices and translation vectors13    '''14    15    poses = []16    17    rotation_angle_rad = np.deg2rad(rotation_angle)18    angle_step = rotation_angle_rad / num_poses19 20    for i in range(num_poses):21        22        theta = i * angle_step23        24        # Rotation matrix25        R = np.array([26            [np.cos(theta), 0, np.sin(theta)],27            [0, 1, 0],28            [-np.sin(theta), 0, np.cos(theta)]29        ])30        31        # Translation vector32        t = np.array([0.01 * np.sin(theta), 0, height - 0.01 * np.cos(theta)])33        34        # Combine rotation matrix and translation vector into RT matrix35        RT = np.hstack((R, t.reshape(-1, 1)))36        poses.append(RT)37        38    poses = np.stack(poses, axis=0)39    40    return poses41 42def clockwise(angle, n_frames):43    # Convert angle to radians44    angle_rad = np.deg2rad(angle)45    46    # Determine the direction of rotation based on the sign of the angle47    if angle_rad < 0:48        clockwise = True49        angle_rad = -angle_rad  # Make the angle positive for calculation50    else:51        clockwise = False52    53    # Generate rotation matrices for each frame54    rotation_matrices = []55    for i in range(n_frames):56        theta = i * angle_rad / (n_frames - 1)57        if clockwise:58            theta = -theta59        R = np.array([60            [np.cos(theta), -np.sin(theta), 0],61            [np.sin(theta), np.cos(theta), 0],62            [0, 0, 1]63        ])64        rotation_matrices.append(R)65    66    # Generate translation vectors (assuming no translation)67    translation_vectors = [np.zeros((3, 1)) for _ in range(n_frames)]68    69    # Combine rotation matrices and translation vectors into RT matrices70    RT_matrices = []71    for R, T in zip(rotation_matrices, translation_vectors):72        RT = np.hstack((R, T))73        RT_matrices.append(RT)74        75    RT_matrices = np.stack(RT_matrices, axis=0)76    77    return RT_matrices78 79def pan_and_zoom(T, speed, base_T=1.5, n=16):80    RT = []81    for i in range(n):82        R = np.array([[1.0, 0.0, 0.0],83                      [0, 1.0, 0.0],84                      [0.0, 0.0, 1.0]])85        _T=(i/n)*speed*base_T*(T[i])86        _RT = np.concatenate([R,_T], axis=1)87        RT.append(_RT)88    RT = np.stack(RT)89    90    return RT