Ejemplo n.º 1
0
def gao(idx):
    # if os.path.exists(test_label_dir.joinpath('%s.torch' % idx)):
    # 	return
    anno_path = anno_dir.joinpath('%s_%s_keypoints.json' % (args.name, idx))
    anno = json.load(open(str(anno_path)))['people']
    if len(anno) == 0:
        print("warnings: %s no people" % idx)
        return [-1, -1, -1, -1]

    anno = anno[0]
    for key in anno:
        anno[key] = np.array(anno[key]).reshape(-1, 3).astype(np.float)
        anno[key][:, 0] -= 420
        anno[key][:, :2] = (anno[key][:, :2] / 1080. * 512).clip(min=0)
    x = anno['pose_keypoints_2d'][:, :2]

    s = np.linalg.norm(x[1, :] - x[8, :])
    y = max(x[21, 1], x[24, 1])
    if y > 10 and x[1, :].min() > 5 and x[8, :].min() > 5:
        w = x[:, 0].max() - x[:, 0].min()
        b = (y - ymin) / (ymax - ymin) * (fmax - fmin) + fmin
        l = (y - ymin) / (ymax - ymin) * (tmax / smax -
                                          tmin / smin) + tmin / smin

        left = w * (1 - l) * x[:, 1].min() / (512 - w)
        for key in anno:
            anno[key] *= l

        d = max(x[21, 1], x[24, 1]) - b
        # print(l, d)
        for key in anno:
            anno[key][:, 0] += left
            anno[key][:, 1] -= d
        # print(max(x[21,1], x[24,1]) - b)

    # img_path = img_dir.joinpath('%s.png'%idx)
    # img = cv2.imread(str(img_path))[:, 420: -420]
    # img = cv2.resize(img, (512, 512))
    # cv2.imwrite(str(test_image_dir.joinpath('%s.png'%idx)), img)
    # label = create_label_full((512, 512), anno)

    # s = label.max(axis = 2)[:,:, np.newaxis]
    # fig = plt.figure(1)
    # ax = fig.add_subplot(111)
    # ax.imshow((img * .8 + s * 255 * .2 ).astype(np.uint8))
    # ax.imshow((s[:,:, 0] * 255).astype(np.uint8))
    # plt.show()

    # label = torch.tensor(label).byte()
    # label_path = test_label_dir.joinpath('%s.torch'% idx)
    # torch.save(label, str(label_path))
    # print(str(test_image_dir.joinpath('%s.png'%idx)))

    # ================ Crop Face=====================
    face = anno['face_keypoints_2d']
    if face[:, 2].min() < 0.001:
        print(face[:, 2].min())
        return [-1, -1, -1, -1]
    minx, maxx = int(max(face[:, 1].min() - 20,
                         0)), int(min(face[:, 1].max() + 10, 512))
    miny, maxy = int(max(face[:, 0].min() - 15,
                         0)), int(min(face[:, 0].max() + 15, 512))

    face[:, 0] = (face[:, 0] - miny) / (maxy - miny + 1.) * 128.
    face[:, 1] = (face[:, 1] - minx) / (maxx - minx + 1.) * 128.
    face_label = create_face_label((128, 128), face)

    # fig = plt.figure(1)
    # ax = fig.add_subplot(111)
    # s = face_label.max(axis = 2, keepdims = False)
    # print(s.shape)
    # ax.imshow(s)
    # plt.show()

    face_label = torch.tensor(face_label).byte()
    face_label_dir = test_face_label_dir.joinpath('%s.torch' % idx)
    torch.save(face_label, str(face_label_dir))
    return [minx, maxx, miny, maxy]
def gao(idx):
    # if os.path.exists(train_head_dir.joinpath('%s.png' % idx)):
    # 	return
    print(idx)
    anno_path = anno_dir.joinpath('%s_%s_keypoints.json' % (args.name, idx))
    anno = json.load(open(str(anno_path)))['people']
    if len(anno) == 0:
        print("warnings: %s no people" % idx)
        return [-1, -1, -1, -1]
    anno = anno[0]
    anno.pop("person_id")
    # print(np.array(anno['pose_keypoints_2d']).reshape(-1, 3))
    for key in anno:
        anno[key] = np.array(anno[key]).reshape(-1, 3)
        anno[key][:, 0] -= 420
        # print(anno[key][:,  0].min())
        anno[key][:, :2] = (anno[key][:, :2] / 1080. * 512).clip(min=0)
    anno['person_id'] = temp_dict['person_id']
    # print(anno['pose_keypoints_2d'][:, :2][:, ::-1])

    img_path = img_dir.joinpath('%s.png' % idx)
    img = cv2.imread(str(img_path))[:, 420:-420]
    img = cv2.resize(img, (512, 512))

    cv2.imwrite(str(train_img_dir.joinpath(idx + '.png')), img)
    label = create_label_full((512, 512), anno)

    # s = label.max(axis = 2)[:,:, np.newaxis]
    # fig = plt.figure(1)
    # ax = fig.add_subplot(111)
    # ax.imshow((img * .8 + s * 255 * .2 ).astype(np.uint8))
    # ax.imshow((s[:,:, 0] * 255).astype(np.uint8))
    # plt.show()

    label = torch.tensor(label).byte()
    label_path = train_label_dir.joinpath('%s.torch' % idx)
    torch.save(label, str(label_path))
    # ===================== Crop Face ==============================

    face = anno['face_keypoints_2d']
    if face[:, 2].min() < 0.001:
        print(face[:, 2].min())
        return [-1, -1, -1, -1]
    minx, maxx = int(max(face[:, 1].min() - 20,
                         0)), int(min(face[:, 1].max() + 10, 512))
    miny, maxy = int(max(face[:, 0].min() - 15,
                         0)), int(min(face[:, 0].max() + 15, 512))
    # print(minx, maxx , miny, maxy )
    face_img = img[minx:maxx + 1, miny:maxy + 1, :]
    face_img = cv2.resize(face_img, (128, 128))
    cv2.imwrite(str(train_face_image_dir.joinpath('%s.png' % idx)), face_img)

    face[:, 0] = (face[:, 0] - miny) / (maxy - miny + 1.) * 128.
    face[:, 1] = (face[:, 1] - minx) / (maxx - minx + 1.) * 128.
    face_label = create_face_label((128, 128), face)

    # s = face_label.max(axis = 2)[:,:, np.newaxis]
    # fig = plt.figure(1)
    # ax = fig.add_subplot(111)
    # ax.imshow((s[:,:, 0] * 255).astype(np.uint8))
    # plt.show()

    face_label = torch.tensor(face_label).byte()
    face_label_dir = train_face_label_dir.joinpath('%s.torch' % idx)
    torch.save(face_label, str(face_label_dir))
    return [minx, maxx, miny, maxy]