MetaPKLot-Dataset / tools /PLds /convertPLds2PKLot2.0.py
LoanMaikon's picture
Add files using upload-large-folder tool
07444d1 verified
Raw
History Blame Contribute Delete
3.7 kB
import json
import sys
import argparse
import os
import tqdm
import subprocess
from glob import glob
def main():
check_dependencies()
args = get_args()
args.output += "/PLds_converted" if not args.output.endswith("/") else "PLds_converted"
rar_files = [args.Images_rar, args.All_QRIDR_rar, args.All_VXUSD_rar]
unrar_images(rar_files, args.output)
convert_plds_to_pklot2_0(args.spots_jsons, args.output)
def convert_plds_to_pklot2_0(plds_spot_jsons_path, output_dir):
jsons_names = ['isshk_spots.json', 'qridr_spots.json', 'vmlix_spots.json', 'vxusd_spots.json']
plds_spot_jsons_path += '/' if not plds_spot_jsons_path.endswith('/') else ''
output_dir += '/' if not output_dir.endswith('/') else ''
plds_spot_jsons = [json.load(open(plds_spot_jsons_path + json_name)) for json_name in jsons_names]
path_dict = {}
for plds_spot_json in plds_spot_jsons:
for image in plds_spot_json['images']:
path_dict[image['file_name'].split('/')[-1].lower()] = image['file_name']
repeated_path = output_dir + "repeated/"
os.makedirs(repeated_path, exist_ok=True)
images = glob(os.path.join(output_dir, '*.jpg'))
for image in images:
image_name = image.split('/')[-1].lower()
if image_name in path_dict:
image_path = path_dict[image_name].split('/')[:-1]
image_path = '/'.join(image_path)
os.makedirs(os.path.join(output_dir, image_path), exist_ok=True)
subprocess.run(['mv', image, os.path.join(output_dir, image_path)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
else:
subprocess.run(['mv', image, os.path.join(repeated_path, image_name)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
def unrar_images(rar_files, output):
for rar_file in rar_files:
if not os.path.exists(rar_file):
print(f'{rar_file} not found')
sys.exit(1)
if os.path.exists(output):
print('Output directory already exists, Overwrite? (y/n)')
if input().lower() == 'n':
sys.exit(0)
else:
print(f"Removing {output}")
subprocess.run(['rm', '-rf', output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print(f"Uncrompressing images to {output}")
os.makedirs(output, exist_ok=True)
for rar_file in rar_files:
result = subprocess.run(['unrar', 'e', rar_file, output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode != 0:
print('Failed to unrar images')
print(result.stderr)
sys.exit(1)
def check_dependencies():
try:
result = subprocess.run(['which', 'unrar'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
if not result.stdout.strip():
print('unrar not found. Please install unrar')
sys.exit(1)
except subprocess.CalledProcessError:
print('unrar not found. Please install unrar')
sys.exit(1)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--Images_rar', type=str, required=True, help='Path to Images.rar')
parser.add_argument('--All_QRIDR_rar', type=str, required=True, help='Path to All_QRIDR.rar')
parser.add_argument('--All_VXUSD_rar', type=str, required=True, help='Path to All_VXUSD.rar')
parser.add_argument('--output', type=str, required=True, help='Path to the output directory')
parser.add_argument('--spots_jsons', type=str, required=True, help='Path to the spots json files in the PKLot2.0 repository')
return parser.parse_args()
if __name__ == '__main__':
main()