def preprocess_image(img_path, img_size):
if not os.path.exists(img_path):
print(img_path)
return None, 0, 0
input_image = 255 * caffe.io.load_image(img_path)
image = PILImage.fromarray(np.uint8(input_image))
image = np.array(image)
mean_vec = np.array([103.939, 116.779, 123.68], dtype=np.float32)
reshaped_mean_vec = mean_vec.reshape(1, 1, 3);
preprocess_img = image[:,:,::-1]
preprocess_img = preprocess_img - reshaped_mean_vec
# Pad as necessary
cur_h, cur_w, cur_c = preprocess_img.shape
pad_h = img_size - cur_h
pad_w = img_size - cur_w
preprocess_img = np.pad(preprocess_img, pad_width=((0, pad_h), (0, pad_w), (0, 0)), mode = 'constant', constant_values = 0)
return preprocess_img, cur_h, cur_w
评论列表
文章目录