| import os
|
| import json
|
|
|
|
|
| def generate_pic_related_json(pic_dir, pre_dec_json_dir, gt_json_dir, json_output_dir, example_template_path):
|
| """
|
| Generate a JSON annotation file for each PNG image in the given image directory.
|
|
|
| For every image file under `pic_dir`, this function:
|
| 1. Loads a JSON template from `example_template_path`.
|
| 2. Fills in image-related fields (image name, relative paths to image, prediction JSON, and GT JSON).
|
| 3. Assigns default values to auxiliary annotation fields.
|
| 4. Writes the resulting JSON file to `json_output_dir`.
|
|
|
| All file paths stored in the generated JSON are relative to `json_output_dir`,
|
| which makes the dataset portable across different directory layouts.
|
|
|
| Parameters
|
| ----------
|
| pic_dir : str
|
| Directory containing PNG image files.
|
| pre_dec_json_dir : str
|
| Directory containing pre-decision JSON files (aligned predictions).
|
| gt_json_dir : str
|
| Directory containing ground-truth JSON files.
|
| json_output_dir : str
|
| Output directory where generated JSON files will be saved.
|
| example_template_path : str
|
| Path to the example/template JSON file used as a base structure.
|
| """
|
|
|
|
|
| os.makedirs(json_output_dir, exist_ok=True)
|
|
|
|
|
| with open(example_template_path, "r", encoding="utf-8") as f:
|
| template = json.load(f)
|
|
|
|
|
| for pic_filename in os.listdir(pic_dir):
|
| if not pic_filename.endswith(".png"):
|
| continue
|
|
|
|
|
| pic_basename = os.path.splitext(pic_filename)[0]
|
|
|
|
|
| pic_full_path = os.path.join(pic_dir, pic_filename)
|
| gt_json_path = os.path.join(gt_json_dir, f"{pic_basename}_gt.json")
|
| pre_dec_full_path = os.path.join(pre_dec_json_dir, f"{pic_basename}_aligned.json")
|
|
|
|
|
|
|
| relative_pic_path = os.path.relpath(pic_full_path, json_output_dir)
|
| relative_gt_json_path = os.path.relpath(gt_json_path, json_output_dir)
|
| relative_pre_dec_path = os.path.relpath(pre_dec_full_path, json_output_dir)
|
|
|
|
|
| new_json = template.copy()
|
| new_json["pic_name"] = pic_basename
|
| new_json["pic_path"] = relative_pic_path
|
| new_json["pre_dec_file"] = relative_pre_dec_path
|
| new_json["gt_json_file"] = relative_gt_json_path
|
|
|
|
|
| new_json["is_longtail"] = "True"
|
| new_json["Sup_description"] = ""
|
| new_json["lt_ele"] = ""
|
| new_json["acc_factors"] = ""
|
| new_json["COT"] = ""
|
| new_json["post_dec"] = ""
|
| new_json["is_transfer2p"] = "No"
|
|
|
|
|
| output_json_path = os.path.join(json_output_dir, f"{pic_basename}.json")
|
| with open(output_json_path, "w", encoding="utf-8") as f:
|
| json.dump(new_json, f, indent=2, ensure_ascii=False)
|
|
|
| print(f"All image-related JSON files have been generated and saved to: {json_output_dir}")
|
|
|
|
|
| def uniform_rename_files(pic_dir, pre_dec_json_dir, gt_json_dir, json_dir):
|
| """
|
| Uniformly rename related files in multiple directories and update JSON contents accordingly.
|
|
|
| This function processes corresponding groups of files:
|
| - Image file: XXX.png
|
| - Pre-decision JSON: XXX_aligned.json
|
| - Ground-truth JSON: XXX_gt.json
|
| - Metadata JSON: XXX.json
|
|
|
| Each group is renamed sequentially to:
|
| - data1.png, data2.png, ...
|
| - data1_aligned.json, ...
|
| - data1_gt.json, ...
|
| - data1.json, ...
|
|
|
| After renaming, the function updates the internal fields of the JSON files to:
|
| - Reflect the new base name (dataX)
|
| - Maintain relative paths with respect to `json_dir`
|
|
|
| Parameters
|
| ----------
|
| pic_dir : str
|
| Directory containing image files.
|
| pre_dec_json_dir : str
|
| Directory containing pre-decision JSON files.
|
| gt_json_dir : str
|
| Directory containing ground-truth JSON files.
|
| json_dir : str
|
| Directory containing metadata JSON files (used as the relative path base).
|
| """
|
|
|
|
|
| file_groups = []
|
| for json_filename in os.listdir(json_dir):
|
| if not json_filename.endswith(".json"):
|
| continue
|
|
|
| json_basename = os.path.splitext(json_filename)[0]
|
|
|
|
|
| pic_path = os.path.join(pic_dir, f"{json_basename}.png")
|
| pre_dec_path = os.path.join(pre_dec_json_dir, f"{json_basename}_aligned.json")
|
| gt_json_path = os.path.join(gt_json_dir, f"{json_basename}_gt.json")
|
| json_path = os.path.join(json_dir, json_filename)
|
|
|
|
|
| if os.path.exists(pic_path) and os.path.exists(pre_dec_path):
|
| file_groups.append((pic_path, pre_dec_path, gt_json_path, json_path, json_basename))
|
| else:
|
| print(f"Warning: Missing pic/pre_dec files for {json_basename}, skipping this entry.")
|
|
|
|
|
| for idx, (pic_path, pre_dec_path, gt_json_path, json_path, old_basename) in enumerate(file_groups, start=1):
|
| new_basename = f"data{idx}"
|
|
|
|
|
| new_pic_path = os.path.join(pic_dir, f"{new_basename}.png")
|
| os.rename(pic_path, new_pic_path)
|
|
|
|
|
| new_pre_dec_path = os.path.join(pre_dec_json_dir, f"{new_basename}_aligned.json")
|
| os.rename(pre_dec_path, new_pre_dec_path)
|
|
|
|
|
| new_gt_json_path = os.path.join(gt_json_dir, f"{new_basename}_gt.json")
|
| os.rename(gt_json_path, new_gt_json_path)
|
|
|
|
|
| new_json_path = os.path.join(json_dir, f"{new_basename}.json")
|
| os.rename(json_path, new_json_path)
|
|
|
|
|
| relative_new_pic_path = os.path.relpath(new_pic_path, json_dir)
|
| relative_new_pre_dec_path = os.path.relpath(new_pre_dec_path, json_dir)
|
|
|
|
|
| with open(new_json_path, "r+", encoding="utf-8") as f:
|
| json_data = json.load(f)
|
| json_data["pic_name"] = new_basename
|
| json_data["pic_path"] = relative_new_pic_path
|
| json_data["pre_dec_file"] = relative_new_pre_dec_path
|
| json_data["gt_json_file"] = new_gt_json_path
|
| f.seek(0)
|
| json.dump(json_data, f, indent=2, ensure_ascii=False)
|
| f.truncate()
|
|
|
|
|
| new_gt_json_path_local = os.path.join(gt_json_dir, f"{new_basename}_gt.json")
|
| with open(new_gt_json_path_local, "r+", encoding="utf-8") as f:
|
| json_data = json.load(f)
|
| json_data["source_scene"] = relative_new_pre_dec_path
|
| f.seek(0)
|
| json.dump(json_data, f, indent=2, ensure_ascii=False)
|
| f.truncate()
|
|
|
| print(f"Completed uniform renaming and JSON updates for {len(file_groups)} file groups.")
|
|
|
|
|
| if __name__ == "__main__":
|
|
|
| pic_dir = "pic"
|
| pre_dec_json_dir = "json/pre_dec_json"
|
| gt_json_dir = "json/gt_json"
|
| json_dir = "json"
|
| example_template_path = "example.json"
|
|
|
|
|
|
|
|
|
|
|
| uniform_rename_files(pic_dir, pre_dec_json_dir, gt_json_dir, json_dir)
|
|
|