def pose_rotation(meta): deg = random.uniform(-15.0, 15.0) img = meta.img center = (img.shape[1] * 0.5, img.shape[0] * 0.5) # x, y rot_m = cv2.getRotationMatrix2D((int(center[0]), int(center[1])), deg, 1) ret = cv2.warpAffine(img, rot_m, img.shape[1::-1], flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT) if img.ndim == 3 and ret.ndim == 2: ret = ret[:, :, np.newaxis] neww, newh = RotationAndCropValid.largest_rotated_rect(ret.shape[1], ret.shape[0], deg) neww = min(neww, ret.shape[1]) newh = min(newh, ret.shape[0]) newx = int(center[0] - neww * 0.5) newy = int(center[1] - newh * 0.5) # print(ret.shape, deg, newx, newy, neww, newh) img = ret[newy:newy + newh, newx:newx + neww] # adjust meta data adjust_joint_list = [] for point in meta.joint_list: if point[0] < -100 or point[1] < -100: adjust_joint_list.append((-1000, -1000)) continue # if point[0] <= 0 or point[1] <= 0: # adjust_joint_list.append((-1, -1)) # continue x, y = _rotate_coord((meta.width, meta.height), (newx, newy), point, deg) adjust_joint_list.append((x, y)) meta.joint_list = adjust_joint_list meta.width, meta.height = neww, newh meta.img = img return meta
def pose_rotation(meta): deg = random.uniform(-15.0, 15.0) img = meta.img center = (img.shape[1] * 0.5, img.shape[0] * 0.5) # x, y rot_m = cv2.getRotationMatrix2D((int(center[0]), int(center[1])), deg, 1) ret = cv2.warpAffine(img, rot_m, img.shape[1::-1], flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT) if img.ndim == 3 and ret.ndim == 2: ret = ret[:, :, np.newaxis] neww, newh = RotationAndCropValid.largest_rotated_rect(ret.shape[1], ret.shape[0], deg) neww = min(neww, ret.shape[1]) newh = min(newh, ret.shape[0]) newx = int(center[0] - neww * 0.5) newy = int(center[1] - newh * 0.5) # print(ret.shape, deg, newx, newy, neww, newh) img = ret[newy:newy + newh, newx:newx + neww] # adjust meta data adjust_joint_list = [] for joint in meta.joint_list: adjust_joint = [] for point in joint: if point[0] < -100 or point[1] < -100: adjust_joint.append((-1000, -1000)) continue # if point[0] <= 0 or point[1] <= 0: # adjust_joint.append((-1, -1)) # continue x, y = _rotate_coord((meta.width, meta.height), (newx, newy), point, deg) adjust_joint.append((x, y)) adjust_joint_list.append(adjust_joint) meta.joint_list = adjust_joint_list meta.width, meta.height = neww, newh meta.img = img return meta
def pose_rotation(meta): if meta.hyperparams is None: max_rotate_degree = 40 #random.uniform(-15.0, 15.0) else: max_rotate_degree = meta.hyperparams['max_rotate_degree'] deg = random.uniform(-40.0, 40.0) img = meta.img center = (img.shape[1] * 0.5, img.shape[0] * 0.5) # x, y rot_m = cv2.getRotationMatrix2D(center, deg, 1) if not isinstance(meta, pose_dataset.MPIMetadata): ret = cv2.warpAffine(img, rot_m, img.shape[1::-1], flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT, borderValue=(128, 128, 128)) if img.ndim == 3 and ret.ndim == 2: ret = ret[:, :, np.newaxis] neww, newh = RotationAndCropValid.largest_rotated_rect( ret.shape[1], ret.shape[0], deg) neww = min(neww, ret.shape[1]) newh = min(newh, ret.shape[0]) newx = int(center[0] - neww * 0.5) newy = int(center[1] - newh * 0.5) img = ret[newy:newy + newh, newx:newx + neww] else: # rotation calculates the cos and sin, taking absolutes of those. abs_cos = abs(rot_m[0, 0]) abs_sin = abs(rot_m[0, 1]) # find the new width and height bounds bound_w = int(img.shape[0] * abs_sin + img.shape[1] * abs_cos) bound_h = int(img.shape[0] * abs_cos + img.shape[1] * abs_sin) # subtract old image center (bringing image back to orin) and adding # the new image center coordinates rot_m[0, 2] += bound_w / 2 - center[0] rot_m[1, 2] += bound_h / 2 - center[1] ret = cv2.warpAffine(img, rot_m, (bound_w, bound_h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT, borderValue=(128, 128, 128)) if img.ndim == 3 and ret.ndim == 2: ret = ret[:, :, np.newaxis] img = ret neww, newh = ret.shape[1], ret.shape[0] newx = int(center[0] - neww * 0.5) newy = int(center[1] - newh * 0.5) #adjust mask if isinstance(meta, pose_dataset.MPIMetadata): msk = cv2.warpAffine(meta.mask, rot_m, (bound_w, bound_h), flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT, borderValue=255) #borderMode=cv2.BORDER_CONSTANT) meta.mask = msk # adjust meta data adjust_joint_list = [] for joint in meta.joint_list: adjust_joint = [] for point in joint: if np.abs(point[0]) == np.inf or np.abs(point[1]) == np.inf: x = np.inf y = np.inf else: x, y = _rotate_coord((meta.width, meta.height), (newx, newy), point, deg) adjust_joint.append((x, y)) adjust_joint_list.append(adjust_joint) adjust_objPos = [] future_randpers = meta.randpers for c, pos in enumerate(meta.objpos): x, y = _rotate_coord((meta.width, meta.height), (newx, newy), pos, deg) if not (x > img.shape[1] or y > img.shape[0] or x < 0 or y < 0): adjust_objPos.append([x, y]) elif c <= meta.randpers: future_randpers -= 1 logger.warning('lost person during rotate') meta.randpers = future_randpers meta.objpos = adjust_objPos meta.joint_list = adjust_joint_list meta.width, meta.height = neww, newh meta.img = img return meta
def pose_rotation(image, annos, mask): img_shape = np.shape(image) height = img_shape[0] width = img_shape[1] deg = random.uniform(-15.0, 15.0) img = image center = (img.shape[1] * 0.5, img.shape[0] * 0.5) # x, y rot_m = cv2.getRotationMatrix2D((int(center[0]), int(center[1])), deg, 1) ret = cv2.warpAffine(img, rot_m, img.shape[1::-1], flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT) if img.ndim == 3 and ret.ndim == 2: ret = ret[:, :, np.newaxis] neww, newh = RotationAndCropValid.largest_rotated_rect( ret.shape[1], ret.shape[0], deg) neww = min(neww, ret.shape[1]) newh = min(newh, ret.shape[0]) newx = int(center[0] - neww * 0.5) newy = int(center[1] - newh * 0.5) # print(ret.shape, deg, newx, newy, neww, newh) img = ret[newy:newy + newh, newx:newx + neww] # adjust meta data adjust_joint_list = [] for joint in annos: adjust_joint = [] for point in joint: if point[0] < -100 or point[1] < -100: adjust_joint.append((-1000, -1000)) continue # if point[0] <= 0 or point[1] <= 0: # adjust_joint.append((-1, -1)) # continue x, y = _rotate_coord((width, height), (newx, newy), point, deg) # if x > neww or y > newh: # adjust_joint.append((-1000, -1000)) # continue if x > neww - 1 or y > newh - 1: adjust_joint.append((-1000, -1000)) continue if x < 0 or y < 0: adjust_joint.append((-1000, -1000)) continue adjust_joint.append((x, y)) adjust_joint_list.append(adjust_joint) joint_list = adjust_joint_list msk = mask center = (msk.shape[1] * 0.5, msk.shape[0] * 0.5) # x, y rot_m = cv2.getRotationMatrix2D((int(center[0]), int(center[1])), deg, 1) ret = cv2.warpAffine(msk, rot_m, msk.shape[1::-1], flags=cv2.INTER_AREA, borderMode=cv2.BORDER_CONSTANT) if msk.ndim == 3 and msk.ndim == 2: ret = ret[:, :, np.newaxis] neww, newh = RotationAndCropValid.largest_rotated_rect( ret.shape[1], ret.shape[0], deg) neww = min(neww, ret.shape[1]) newh = min(newh, ret.shape[0]) newx = int(center[0] - neww * 0.5) newy = int(center[1] - newh * 0.5) # print(ret.shape, deg, newx, newy, neww, newh) msk = ret[newy:newy + newh, newx:newx + neww] return img, joint_list, msk