coco关键点数据集水平翻转数据增强,连同标注一起变化

对coco图片进行水平翻转时,连同关键点标注也要对应着进行变化。
首先是x坐标,从x变为了width-1-x,然后对应的,人体的左右边要进行交换,比如原来的左手就变成右手,原来的右手变成左手,通过以下代码完成。

 for (q, w) in symmetry:
        cod[q], cod[w] = cod[w], cod[q]`

关键点标注变化的核心代码如下:

    img = cv2.flip(img, 1)
    label = points
    cod = []
    allc = []
    for i in range(17):
        x, y = label[i][0], label[i][1]
        if x >= 0:
            x = width - 1 - x
        cod.append((x, y, label[i][2]))
    # # **** the joint index depends on the dataset ****
    for (q, w) in symmetry:
        cod[q], cod[w] = cod[w], cod[q]
    for i in range(17):
        allc.append(cod[i][0])
        allc.append(cod[i][1])
        allc.append(cod[i][2])
    label = np.array(allc).reshape(17, 3)

以下为完整运行代码

	num_class = 17
    a =  {'unit': {'num_keypoints': 14, 'keypoints': [368, 61, 1, 369, 52, 2, 0, 0, 0, 382, 48, 2, 0, 0, 0, 368, 84, 2, 435, 81, 2, 362, 125, 2, 446, 125, 2, 360, 153, 2, 0, 0, 0, 397, 167, 1, 439, 166, 1, 369, 193, 2, 461, 234, 2, 361, 246, 2, 474, 287, 2], 'GT_bbox': [339, 22, 493, 322]}, 'imgInfo': {'imgID': 391895, 'img_paths': '000000391895.jpg'}, 'operation': 0}
    image_name = a['imgInfo']['img_paths']
    points = np.array(a['unit']['keypoints']).reshape(num_class, 3).astype(np.float32)
    gt_bbox = a['unit']['GT_bbox']
    symmetry = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10), (11, 12), (13, 14), (15, 16)]
    img =  cv2.imread(image_name)
    plt.imshow(img)
    plt.show()

    height, width = img.shape[0], img.shape[1]

    img = cv2.flip(img, 1)
    label = points
    cod = []
    allc = []
    for i in range(17):
        x, y = label[i][0], label[i][1]
        if x >= 0:
            x = width - 1 - x
        cod.append((x, y, label[i][2]))
    # # **** the joint index depends on the dataset ****
    for (q, w) in symmetry:
        cod[q], cod[w] = cod[w], cod[q]
    for i in range(17):
        allc.append(cod[i][0])
        allc.append(cod[i][1])
        allc.append(cod[i][2])
    label = np.array(allc).reshape(17, 3)

你可能感兴趣的:(COCO数据集,数据增强,深度学习)