def openpose_convert_labelme_format(input_json, input_image_path=None, output_dir=None, only_pose=False): openPose_object = OpenPose_JSON(input_json) labelme_object = Labelme_JSON() image_name = utils_io_folder.get_file_name_from_path( input_image_path) # like 00000001.jpg labelme_object.imagePath = "" # labelme_object.imageData = "" labelme_object.imageData = utils_image.img_encode_to_base64( input_image_path) image_arr = utils_image.img_b64_to_arr( utils_image.img_encode_to_base64(input_image_path)) labelme_object.imageHeight, labelme_object.imageWidth = image_arr.shape[ 0], image_arr.shape[1] for person in openPose_object.people: track_id = person["person_id"] pose_keypoints_2d = person["pose_keypoints_2d"] hand_left_keypoints_2d = person["hand_left_keypoints_2d"] hand_right_keypoints_2d = person["hand_right_keypoints_2d"] if only_pose: for index, keypoint in enumerate(pose_keypoints_2d): labelme_object.shapes.append( point_shape(track_id, pose_keypoints_order[index], keypoint)) else: for index, keypoint in enumerate(pose_keypoints_2d): labelme_object.shapes.append( point_shape(track_id, pose_keypoints_order[index], keypoint)) for index, keypoint in enumerate(hand_left_keypoints_2d): if index in hand_keep_point: labelme_object.shapes.append( point_shape( track_id, hand_keypoints_order[index].format("left_"), keypoint)) for index, keypoint in enumerate(hand_right_keypoints_2d): if index in hand_keep_point: labelme_object.shapes.append( point_shape( track_id, hand_keypoints_order[index].format("right_"), keypoint)) if output_dir is None: # 输出 默认在input_json的路径下创建labelme文件夹。 output_dir = os.path.join( utils_io_folder.get_parent_folder_from_path(input_json)) # utils_io_folder.create_folder(output_dir) output_path = os.path.join( output_dir, utils_io_folder.replace_file_ext(image_name, "json")) else: utils_io_folder.create_folder(output_dir) output_path = os.path.join( output_dir, utils_io_folder.replace_file_ext(image_name, "json")) utils_json.write_json_to_file(labelme_object.to_dict(), output_path)
def openSVAI_convert_labelme_format(input_json, input_images_dir=None, output_dir=None): openSVAI_object = OpenSVAI_JSON(input_json) for frame in openSVAI_object.frames: labelme_object = Labelme_JSON() image_info = frame["image"] candidates_info = frame["candidates"] image_name = image_info["name"] # like 00000001.jpg if input_images_dir is None: labelme_object.imagePath = os.path.join(image_info["folder"], image_info["name"]) else: labelme_object.imagePath = os.path.join(input_images_dir, image_name) labelme_object.imageData = utils_image.img_encode_to_base64(labelme_object.imagePath) image_arr = utils_image.img_b64_to_arr(labelme_object.imageData) labelme_object.imageHeight, labelme_object.imageWidth = image_arr.shape[0], image_arr.shape[1] for candidate in candidates_info: track_id = candidate['track_id'] # 对应 labelme的group_id det_bbox = candidate["det_bbox"] # det_bbox (open_SVAI) - rectangle (labelme) labelme_object.shapes.append(rectangle_shape(track_id, det_bbox)) keypoints = numpy.array(candidate['pose_keypoints_2d']).reshape(-1, 3) for index, keypoint in enumerate(keypoints): # keypoint (open_SVAI) - point (labelme) labelme_object.shapes.append(point_shape(track_id, keypoints_order[index], keypoint)) if output_dir is None: # 输出 默认在input_json的路径下创建labelme文件夹。 output_dir = os.path.join(utils_io_folder.get_parent_folder_from_path(input_json), "labelme") utils_io_folder.create_folder(output_dir) output_path = os.path.join(output_dir, utils_io_folder.replace_file_ext(image_name, "json")) else: utils_io_folder.create_folder(output_dir) output_path = os.path.join(output_dir, utils_io_folder.replace_file_ext(image_name, "json")) utils_json.write_json_to_file(labelme_object.to_dict(), output_path)
def posetrack2017_convert_labelme_format(input_json, input_image_path_base, output_dir_base): """ 输入是一段视频的json 仅一个json文件,输出为每个帧的json文件 :param input_json: :param input_image_path_base: :param output_dir_base: :return: """ annolist = utils_json.read_json_from_file(input_json)["annolist"] for annotation in annolist: # 处理一张图片 # openPose_object = OpenPose_JSON(input_json) input_image_path = os.path.join(input_image_path_base, annotation["image"][0]["name"]) labelme_object = Labelme_JSON() image_name = utils_io_folder.get_file_name_from_path( input_image_path) # like 00000001.jpg labelme_object.imagePath = input_image_path labelme_object.imageData = utils_image.img_encode_to_base64( labelme_object.imagePath) image_arr = utils_image.img_b64_to_arr(labelme_object.imageData) labelme_object.imageHeight, labelme_object.imageWidth = image_arr.shape[ 0], image_arr.shape[1] people = annotation["annorect"] for person in people: track_id = person["track_id"] annopoints = person["annopoints"][0]["point"] for point in annopoints: point_id = point["id"][0] x = point["x"][0] y = point["y"][0] is_visible = point["is_visible"][0] keypoint = [x, y] labelme_object.shapes.append( point_shape( track_id, joint_names[point_id] + "_vis_{}".format(is_visible), keypoint)) for point_1 in annopoints: point_1_id = point_1["id"][0] x_1 = point_1["x"][0] y_1 = point_1["y"][0] for point_2 in annopoints: point_2_id = point_2["id"][0] x_2 = point_2["x"][0] y_2 = point_2["y"][0] for joint_pair in joint_pairs: point_1_name = joint_names[point_1_id] point_2_name = joint_names[point_2_id] if joint_pair[0] == point_1_name and joint_pair[ 1] == point_2_name: labelme_object.shapes.append( line_shape(track_id, point_1_name + point_2_name, [x_1, y_1], [x_2, y_2])) path = annotation["image"][0]["name"] output_dir = output_dir_base + path[path.index("/"):path.rindex("/" )] utils_io_folder.create_folder(output_dir) output_path = os.path.join( output_dir, utils_io_folder.replace_file_ext(image_name, "json")) utils_json.write_json_to_file(labelme_object.to_dict(), output_path)