Silverwing123's picture
initial commit
2b95373 verified
raw
history blame contribute delete
No virus
1.46 kB
import numpy as np
import cv2
def format_img_size(img):
""" formats the image size based on config """
img_min_side = float(640)
(height,width,_) = img.shape
if width <= height:
ratio = img_min_side/width
new_height = int(ratio * height)
new_width = int(img_min_side)
else:
ratio = img_min_side/height
new_width = int(ratio * width)
new_height = int(img_min_side)
img = cv2.resize(img, (new_width, new_height), interpolation=cv2.INTER_CUBIC)
return img, ratio
def format_img_channels(img):
""" formats the image channels based on config """
img_channel_mean = [103.939, 116.779, 123.68]
img_scaling_factor = 1.0
img = img[:, :, (2, 1, 0)]
img = img.astype(np.float32)
img[:, :, 0] -= img_channel_mean[0]
img[:, :, 1] -= img_channel_mean[1]
img[:, :, 2] -= img_channel_mean[2]
img /= img_scaling_factor
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
return img
def format_img(img):
""" formats an image for model prediction based on config """
img, ratio = format_img_size(img)
img = format_img_channels(img)
return img, ratio
# Method to transform the coordinates of the bounding box to its original size
def get_real_coordinates(ratio, x1, y1, x2, y2):
real_x1 = int(round(x1 // ratio))
real_y1 = int(round(y1 // ratio))
real_x2 = int(round(x2 // ratio))
real_y2 = int(round(y2 // ratio))
return (real_x1, real_y1, real_x2 ,real_y2)