Spaces:
Build error
Build error
File size: 2,145 Bytes
4dbe5d1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
from typing import Tuple
import cv2
import numpy as np
def reduce_max_side(
img: np.ndarray, max_side_len: int = 2000
) -> Tuple[np.ndarray, float, float]:
h, w = img.shape[:2]
ratio = 1.0
if max(h, w) > max_side_len:
if h > w:
ratio = float(max_side_len) / h
else:
ratio = float(max_side_len) / w
resize_h = int(h * ratio)
resize_w = int(w * ratio)
resize_h = int(round(resize_h / 32) * 32)
resize_w = int(round(resize_w / 32) * 32)
try:
if int(resize_w) <= 0 or int(resize_h) <= 0:
raise ResizeImgError("resize_w or resize_h is less than or equal to 0")
img = cv2.resize(img, (resize_w, resize_h))
except Exception as exc:
raise ResizeImgError() from exc
ratio_h = h / resize_h
ratio_w = w / resize_w
return img, ratio_h, ratio_w
def increase_min_side(
img: np.ndarray, min_side_len: int = 30
) -> Tuple[np.ndarray, float, float]:
h, w = img.shape[:2]
ratio = 1.0
if min(h, w) < min_side_len:
if h < w:
ratio = float(min_side_len) / h
else:
ratio = float(min_side_len) / w
resize_h = int(h * ratio)
resize_w = int(w * ratio)
resize_h = int(round(resize_h / 32) * 32)
resize_w = int(round(resize_w / 32) * 32)
try:
if int(resize_w) <= 0 or int(resize_h) <= 0:
raise ResizeImgError("resize_w or resize_h is less than or equal to 0")
img = cv2.resize(img, (resize_w, resize_h))
except Exception as exc:
raise ResizeImgError() from exc
ratio_h = h / resize_h
ratio_w = w / resize_w
return img, ratio_h, ratio_w
def add_round_letterbox(
img: np.ndarray,
padding_tuple: Tuple[int, int, int, int],
) -> np.ndarray:
padded_img = cv2.copyMakeBorder(
img,
padding_tuple[0],
padding_tuple[1],
padding_tuple[2],
padding_tuple[3],
cv2.BORDER_CONSTANT,
value=(0, 0, 0),
)
return padded_img
class ResizeImgError(Exception):
pass
|