import os
import argparse
import tqdm
import numpy as np
from PIL import Image
import torch
import torchvision.transforms as transforms
def jpg_to_bin(dataset_path, bin_path):
test_txt_path = os.path.join(dataset_path, 'test.txt')
image_names = []
with open(test_txt_path, 'r', encoding='utf-8') as f:
image_names = f.readlines()
preprocess = transforms.Compose(
[transforms.Resize((288, 800)),])
if not os.path.isdir(bin_path):
os.makedirs(bin_path)
for img_name in tqdm.tqdm(image_names):
src_path = os.path.join(dataset_path, img_name.strip())
if not os.path.isfile(src_path):
continue
dst_path = os.path.join(bin_path,
img_name.strip().replace('/', '-').replace('.jpg', '.bin'))
image = Image.open(src_path).convert('RGB')
input_tensor = preprocess(image)
img = np.array(input_tensor).astype(np.uint8)
img.tofile(dst_path)
if __name__=='__main__':
parser = argparse.ArgumentParser(
'convert original image to bin file.')
parser.add_argument('--dataset-path', type=str,
help='the root path of the dataset.')
parser.add_argument('--bin-path', type=str,
help='a directory to save bin file.')
args = parser.parse_args()
jpg_to_bin(args.dataset_path, args.bin_path)
print('Preprocess Done.')