MP:行为预测骨架的归一化操作

在这里插入图片描述

原始版本

import numpy as np

# TODO
def normalise_bounding_box(trajectory_coordinates, video_resolution):
    trajectory_length = trajectory_coordinates.shape[0]
    for step_idx in range(trajectory_length):
        coordinates = trajectory_coordinates[step_idx, :]
        if coordinates.any():#if any(coordinates):
            left, right, top, bottom = compute_bounding_box(coordinates, video_resolution=video_resolution)
            coordinates_reshaped = coordinates.reshape(-1, 2)
            xs, ys = coordinates_reshaped[:, 0], coordinates_reshaped[:, 1]
            xs, ys = np.where(xs == 0.0, float(left), xs), np.where(ys == 0.0, float(top), ys)
            xs = (xs - left) / (right - left)
            ys = (ys - top) / (bottom - top)
            coordinates_reshaped[:, 0], coordinates_reshaped[:, 1] = xs, ys
            coordinates = coordinates_reshaped.reshape(-1)
        trajectory_coordinates[step_idx, :] = coordinates

    return trajectory_coordinates



# TODO
def compute_bounding_box(keypoints, video_resolution, return_discrete_values=True):
    """Compute the bounding box of a set of keypoints.

    Argument(s):
        keypoints -- A numpy array, of shape (num_keypoints * 2,), containing the x and y values of each
            keypoint detected.
        video_resolution -- A numpy array, of shape (2,) and dtype float32, containing the width and the height of
            the video.

    Return(s):
        The bounding box of the keypoints represented by a 4-uple of integers. The order of the corners is: left,
        right, top, bottom.
    """
    width, height = video_resolution
    keypoints_reshaped = keypoints.reshape(-1, 2)
    x, y = keypoints_reshaped[:, 0], keypoints_reshaped[:, 1]
    x, y = x[x != 0.0], y[y != 0.0]
    try:
        left, right, top, bottom = np.min(x), np.max(x), np.min(y), np.max(y)
    except ValueError:
        # print('All joints missing for input skeleton. Returning zeros for the bounding box.')
        return 0, 0, 0, 0

    extra_width, extra_height = 0.1 * (right - left + 1), 0.1 * (bottom - top + 1)
    left, right = np.clip(left - extra_width, 0, width - 1), np.clip(right + extra_width, 0, width - 1)
    top, bottom = np.clip(top - extra_height, 0, height - 1), np.clip(bottom + extra_height, 0, height - 1)
    # left, right = left - extra_width, right + extra_width
    # top, bottom = top - extra_height, bottom + extra_height

    if return_discrete_values:
        return int(round(left)), int(round(right)), int(round(top)), int(round(bottom))
    else:
        return left, right, top, bottom


keypoints = np.random.random([100, 2])*100
video_resolution = np.array([300,600])# video_resolution是在计算框的大小时会一定的大小,为了防止超出图像范围,所以用长宽做个界限
res = compute_bounding_box(keypoints,video_resolution)
print(res)

trajectory_coordinates = np.random.random([3,100*2])*100
aaa = normalise_bounding_box(trajectory_coordinates,video_resolution)
print(aaa)

无须resolution版本

import numpy as np

# TODO
def normalise_bounding_box(trajectory_coordinates):
    trajectory_length = trajectory_coordinates.shape[0]
    for step_idx in range(trajectory_length):
        coordinates = trajectory_coordinates[step_idx, :]
        if coordinates.any():#if any(coordinates):
            left, right, top, bottom = compute_bounding_box(coordinates)
            coordinates_reshaped = coordinates.reshape(-1, 2)
            xs, ys = coordinates_reshaped[:, 0], coordinates_reshaped[:, 1]
            xs, ys = np.where(xs == 0.0, float(left), xs), np.where(ys == 0.0, float(top), ys)
            xs = (xs - left) / (right - left)
            ys = (ys - top) / (bottom - top)
            coordinates_reshaped[:, 0], coordinates_reshaped[:, 1] = xs, ys
            coordinates = coordinates_reshaped.reshape(-1)
        trajectory_coordinates[step_idx, :] = coordinates

    return trajectory_coordinates



# TODO
def compute_bounding_box(keypoints, return_discrete_values=True):
    """Compute the bounding box of a set of keypoints.

    Argument(s):
        keypoints -- A numpy array, of shape (num_keypoints * 2,), containing the x and y values of each
            keypoint detected.
        video_resolution -- A numpy array, of shape (2,) and dtype float32, containing the width and the height of
            the video.

    Return(s):
        The bounding box of the keypoints represented by a 4-uple of integers. The order of the corners is: left,
        right, top, bottom.
    """
    width, height = video_resolution
    keypoints_reshaped = keypoints.reshape(-1, 2)
    x, y = keypoints_reshaped[:, 0], keypoints_reshaped[:, 1]
    x, y = x[x != 0.0], y[y != 0.0]
    try:
        left, right, top, bottom = np.min(x), np.max(x), np.min(y), np.max(y)
    except ValueError:
        # print('All joints missing for input skeleton. Returning zeros for the bounding box.')
        return 0, 0, 0, 0

    extra_width, extra_height = 0.1 * (right - left + 1), 0.1 * (bottom - top + 1)
    left, right = left - extra_width, right + extra_width
    top, bottom = top - extra_height, bottom + extra_height

    if return_discrete_values:
        return int(round(left)), int(round(right)), int(round(top)), int(round(bottom))
    else:
        return left, right, top, bottom


# keypoints = np.random.random([100, 2])*100
# video_resolution = np.array([300,600])# video_resolution是在计算框的大小时会一定的大小,为了防止超出图像范围,所以用长宽做个界限
# res = compute_bounding_box(keypoints)
# print(res)

trajectory_coordinates = np.random.random([300,17*2])*100
aaa = normalise_bounding_box(trajectory_coordinates)
print(aaa.shape)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值