Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def translate(image, boxes, prob=0.5, border_value=(128, 128, 128)):
random_prob = np.random.uniform()
if random_prob < (1 - prob):
return image, boxes
h, w = image.shape[:2]
min_x1, min_y1 = np.min(boxes, axis=0)[:2]
max_x2, max_y2 = np.max(boxes, axis=0)[2:]
translation_matrix = translation_xy(min=(min(-min_x1 // 2, 0), min(-min_y1 // 2, 0)),
max=(max((w - max_x2) // 2, 1), max((h - max_y2) // 2, 1)), prob=1.)
translation_matrix = change_transform_origin(translation_matrix, (w / 2, h / 2))
image = cv2.warpAffine(
image,
translation_matrix[:2, :],
dsize=(w, h),
flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_CONSTANT,
borderValue=border_value,
)
new_boxes = []
for box in boxes:
x1, y1, x2, y2 = box
points = translation_matrix.dot([
[x1, x2, x1, x2],
[y1, y2, y2, y1],
def translate(image, boxes, prob=0.5, border_value=(128, 128, 128)):
boxes = boxes.astype(np.float32)
random_prob = np.random.uniform()
if random_prob < (1 - prob):
return image, boxes
h, w = image.shape[:2]
if boxes.shape[0] != 0:
min_x1, min_y1 = np.min(boxes, axis=0)[:2]
max_x2, max_y2 = np.max(boxes, axis=0)[2:]
translation_matrix = translation_xy(min=(min(-min_x1 // 2, 0), min(-min_y1 // 2, 0)),
max=(max((w - max_x2) // 2, 1), max((h - max_y2) // 2, 1)), prob=1.)
else:
translation_matrix = translation_xy(min=(min(-w // 8, 0), min(-h // 8, 0)),
max=(max(w // 8, 1), max(h // 8, 1)))
translation_matrix = change_transform_origin(translation_matrix, (w / 2, h / 2))
image = cv2.warpAffine(
image,
translation_matrix[:2, :],
dsize=(w, h),
flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_CONSTANT,
borderValue=border_value,
)
if boxes.shape[0] != 0:
new_boxes = []
for box in boxes:
x1, y1, x2, y2 = box
points = translation_matrix.dot([
[x1, x2, x1, x2],
def translate(image, boxes, prob=0.5, border_value=(128, 128, 128)):
boxes = boxes.astype(np.float32)
random_prob = np.random.uniform()
if random_prob < (1 - prob):
return image, boxes
h, w = image.shape[:2]
if boxes.shape[0] != 0:
min_x1, min_y1 = np.min(boxes, axis=0)[:2]
max_x2, max_y2 = np.max(boxes, axis=0)[2:]
translation_matrix = translation_xy(min=(min(-min_x1 // 2, 0), min(-min_y1 // 2, 0)),
max=(max((w - max_x2) // 2, 1), max((h - max_y2) // 2, 1)), prob=1.)
else:
translation_matrix = translation_xy(min=(min(-w // 8, 0), min(-h // 8, 0)),
max=(max(w // 8, 1), max(h // 8, 1)))
translation_matrix = change_transform_origin(translation_matrix, (w / 2, h / 2))
image = cv2.warpAffine(
image,
translation_matrix[:2, :],
dsize=(w, h),
flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_CONSTANT,
borderValue=border_value,
)
if boxes.shape[0] != 0:
new_boxes = []
for box in boxes: