def openpose_convert_labelme_format(input_json, input_image_path=None, output_dir=None): 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 = 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] 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"] 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): labelme_object.shapes.append(point_shape(track_id, hand_keypoints_order[index].format("left_"), keypoint)) for index, keypoint in enumerate(hand_right_keypoints_2d): 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 start_convert(): labelme_base_path = "F:/DataSet/douyin/my_works/{}" index = ["000003"] for i in tqdm(index): path = labelme_base_path.format(i) print(path) labelme_json_paths = utils_io_folder.get_immediate_childfile_paths( path, ext="json") # TODO 之后要改成在统一的一个目录下 output_folder = "F:/DataSet/douyin/my_works/{}".format(i) utils_io_folder.create_folder(output_folder) for labelme_json_path in tqdm(labelme_json_paths): number = int( utils_io_folder.get_file_name_without_ext_from_path( labelme_json_path)) labelme_to_openpose(labelme_json_path, output_folder)
def start_tmp(): labelme_base_path = "E:/temp/anno/{}" output_base_path = "E:/temp/anno/output/{}" temp_video_types = ["test", "train"] for video_type in temp_video_types: openpose_json_folder_type = output_base_path.format(video_type) labelme_folder = labelme_base_path.format(video_type) folder_name = utils_io_folder.get_file_name_without_ext_from_path(labelme_folder) openpose_json_folder = os.path.join(openpose_json_folder_type, folder_name) utils_io_folder.create_folder(openpose_json_folder) labelme_json_paths = utils_io_folder.get_immediate_childfile_paths(labelme_folder, ext="json") for labelme_json_path in tqdm(labelme_json_paths): # convert json one by one labelme_to_openpose(labelme_json_path, openpose_json_folder)
def work(): root_path = "/media/jion/D/chenhaoming/DataSet/PoseTrack2017/cropped_image" first_paths = utils_io_folder.get_immediate_subfolder_paths(root_path) for first_path in first_paths: second_paths = utils_io_folder.get_immediate_subfolder_paths( first_path) for second_path in second_paths: image_paths = utils_io_folder.get_immediate_childfile_paths( second_path) if image_paths is None or len(image_paths) == 0: os.rmdir(second_path) else: pa = "/media/jion/D/chenhaoming/experiment/hmr" print(pa + second_path[61:]) # new_path="" utils_io_folder.create_folder(pa + second_path[61:])
def start_convert(): labelme_base_path = "F:/DataSet/DouYin/annotation_labelme_image/{}" output_base_path = "F:/DataSet/DouYin/annotation_openpose/{}" for video_type in tqdm(video_types): openpose_json_folder_type = output_base_path.format(video_type) labelme_folder = labelme_base_path.format(video_type) video_folder_sequence = utils_io_folder.get_immediate_subfolder_paths(labelme_folder) for video_folder in tqdm(video_folder_sequence): folder_name = utils_io_folder.get_file_name_without_ext_from_path(video_folder) if folder_name.isdigit(): openpose_json_folder = os.path.join(openpose_json_folder_type, folder_name) utils_io_folder.create_folder(openpose_json_folder) labelme_json_paths = utils_io_folder.get_immediate_childfile_paths(video_folder, ext="json") for labelme_json_path in tqdm(labelme_json_paths): # convert json one by one labelme_to_openpose(labelme_json_path, openpose_json_folder)
def work(): cmd = "mv {} {}" root_path = "/media/jion/D/chenhaoming/DataSet/PoseTrack2017/posetrack_data/images" change_path = "/media/jion/D/chenhaoming/DataSet/PoseTrack2017/cropped_image/{}/{}" folder_paths = utils_io_folder.get_immediate_subfolder_paths(root_path) for folder_path in tqdm(folder_paths): first_folder_name = utils_io_folder.get_file_name_without_ext_from_path(folder_path) # print(first_folder_name) paths = utils_io_folder.get_immediate_subfolder_paths(folder_path) for path in paths: second_folder_name = utils_io_folder.get_file_name_without_ext_from_path(path) # print(second_folder_name) images_path = utils_io_folder.get_immediate_childfile_paths(path) new_path = change_path.format(first_folder_name, second_folder_name) utils_io_folder.create_folder(new_path) for image_path in images_path: image_name = utils_io_folder.get_file_name_without_ext_from_path(image_path) if image_name.find("transform") >= 0: cmd_i = cmd.format(image_path, os.path.join(new_path, image_name)) os.system(cmd_i)
def make_images_from_video(video_path, outimages_path=None): cap = cv2.VideoCapture(video_path) isOpened = cap.isOpened() video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fps = cap.get(cv2.CAP_PROP_FPS) # 帧率<每秒中展示多少张图片> width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 获取宽度 height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 获取宽度 i = 0 if outimages_path is not None: if not utils_io_folder.folder_exists(outimages_path): utils_io_folder.create_folder(outimages_path) assert isOpened, "Can't find video" for index in tqdm(range(video_length)): (flag, data) = cap.read() # 读取每一张 flag<读取是否成功> frame<内容> file_name = "{0:08d}.jpg".format(index + 1) if outimages_path is not None: file_path = os.path.join(outimages_path, file_name) else: utils_io_folder.create_folder("output") file_path = os.path.join("output", file_name) if flag: # 读取成功的话 # 写入文件,1 文件名 2 文件内容 3 质量设置 cv2.imwrite(file_path, data, [cv2.IMWRITE_JPEG_QUALITY, 100])
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)