-
-
Save arunm8489/e80ba9a1b6b78c75f5641ee2c3828967 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# function converting images from opencv format to torch format | |
def preprocess_image(img, inp_dim): | |
""" | |
Prepare image for inputting to the neural network. | |
Returns processed image, original image and original image dimension | |
""" | |
orig_im = cv2.imread(img) | |
dim = orig_im.shape[1], orig_im.shape[0] | |
img = (canvas_image(orig_im, (inp_dim, inp_dim))) | |
img = img[:,:,::-1] | |
img_ = img.transpose((2,0,1)).copy() | |
img_ = torch.from_numpy(img_).float().div(255.0).unsqueeze(0) | |
return img_, orig_im, dim | |
#function letterbox_image that resizes our image, keeping the | |
# aspect ratio consistent, and padding the left out areas with the color (128,128,128) | |
def canvas_image(img, conf_inp_dim): | |
'''resize image with unchanged aspect ratio using padding''' | |
img_w, img_h = img.shape[1], img.shape[0] | |
w, h = conf_inp_dim # dimension from configuration file | |
new_w = int(img_w * min(w/img_w, h/img_h)) | |
new_h = int(img_h * min(w/img_w, h/img_h)) | |
resized_image = cv2.resize(img, (new_w,new_h), interpolation = cv2.INTER_CUBIC) | |
# we fill the extra pixels with 128 | |
canvas = np.full((conf_inp_dim[1], conf_inp_dim[0], 3), 128) | |
canvas[(h-new_h)//2:(h-new_h)//2 + new_h,(w-new_w)//2:(w-new_w)//2 + new_w,:] = resized_image | |
return canvas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment