import os
import argparse
import glob
import numpy as np
from tqdm import tqdm
import cv2
def preprocess(image_src_path, bin_file_path):
file_list = glob.glob(os.path.join(image_src_path, '*.jpg'))
for file in tqdm(file_list):
img = cv2.imread(file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = letterbox(img, auto=False)
image = image.transpose((2, 0, 1))
image = np.expand_dims(image, 0)
image = np.ascontiguousarray(image)
im = image.astype(np.float32)
im /= 255
file_name = file.split('/')[-1].split('.')[0] + ".bin"
im.tofile(os.path.join(bin_file_path, file_name))
def letterbox(im, new_shape=(1280, 1280), color=(114, 114, 114), auto=True, scaleup=True, stride=32):
shape = im.shape[:2]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup:
r = min(r, 1.0)
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
if auto:
dw, dh = np.mod(dw, stride), np.mod(dh, stride)
dw /= 2
dh /= 2
if shape[::-1] != new_unpad:
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
return im
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='preprocess of MaskRCNN PyTorch model')
parser.add_argument("--image_src_path", default="./coco/images/val2017", help='image of dataset')
parser.add_argument("--bin_file_path", default="./val2017_bin", help='Preprocessed image buffer')
opt = parser.parse_args()
if not os.path.exists(opt.bin_file_path):
os.makedirs(opt.bin_file_path)
print('start preprocess...')
preprocess(opt.image_src_path, opt.bin_file_path)
print('preprocess Done!')