def pre_process(image): height, width = image.shape[0:2] inp_height, inp_width = 512, 512 c = np.array([height / 2., width / 2.], dtype=np.float32) s = max(height, width) * 1.0 trans_input = get_affine_transform(c, s, 0, [inp_width, inp_height]) inp_image = cv2.warpAffine(image, trans_input, (inp_width, inp_height), flags=cv2.INTER_LINEAR) mean = np.array( [0.5194416012442385, 0.5378052387430711, 0.533462090585746], dtype=np.float32).reshape(1, 1, 3) std = np.array( [0.3001546018824507, 0.28620901391179554, 0.3014112676161966], dtype=np.float32).reshape(1, 1, 3) inp_image = ((inp_image / 255. - mean) / std).astype(np.float32) images = inp_image.transpose(2, 0, 1).reshape( 1, 3, inp_height, inp_width) # 三维reshape到4维,(1,3,512,512) images = torch.from_numpy(images) meta = { 'c': c, 's': s, 'out_height': inp_height // 4, 'out_width': inp_width // 4 } return images, meta
def pre_process(image, image_size=224): height, width = image.shape[0:2] inp_height, inp_width = image_size, image_size c = np.array([width / 2.0, height / 2.0], dtype=np.float32) s = max(height, width) * 1.0 trans_input = get_affine_transform(c, s, 0, [inp_width, inp_height]) inp_image = cv2.warpAffine(image, trans_input, (inp_width, inp_height), flags=cv2.INTER_LINEAR) # mean = np.array( # [0.5194416012442385, 0.5378052387430711, 0.533462090585746], dtype=np.float32 # ).reshape(1, 1, 3) # std = np.array( # [0.3001546018824507, 0.28620901391179554, 0.3014112676161966], dtype=np.float32 # ).reshape(1, 1, 3) mean = np.array([0.4803, 0.4641, 0.4476], dtype=np.float32).reshape(1, 1, 3) std = np.array([0.1878, 0.1881, 0.1959], dtype=np.float32).reshape(1, 1, 3) inp_image = ((inp_image / 255.0 - mean) / std).astype(np.float32) # inp_image = (inp_image / 255.0).astype(np.float32) images = inp_image.transpose(2, 0, 1).reshape( 1, 3, inp_height, inp_width) # 三维reshape到4维,(1,3,512,512) # print(inp_image) images = torch.from_numpy(images) meta = { "c": c, "s": s, "out_height": inp_height // 4, "out_width": inp_width // 4 } return images, meta
def transform_preds(coords, center, scale, output_size): target_coords = np.zeros(coords.shape) trans = get_affine_transform(center, scale, 0, output_size, inv=1) for p in range(coords.shape[0]): target_coords[p, 0:2] = affine_transform(coords[p, 0:2], trans) return target_coords