HACO / lib /utils /func_utils.py
dqj5182's picture
init
5732928
import cv2
import torch
import numpy as np
def load_img(path, order='RGB'):
img = cv2.imread(path, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION)
if not isinstance(img, np.ndarray):
raise IOError("Fail to read %s" % path)
if order=='RGB': img = img[:,:,::-1]
img = img.astype(np.float32)
return img
def get_bbox(joint_img, joint_valid, expansion_factor=1.0):
x_img, y_img = joint_img[:,0], joint_img[:,1]
x_img = x_img[joint_valid==1]; y_img = y_img[joint_valid==1];
xmin = min(x_img); ymin = min(y_img); xmax = max(x_img); ymax = max(y_img);
x_center = (xmin+xmax)/2.; width = (xmax-xmin)*expansion_factor;
xmin = x_center - 0.5*width
xmax = x_center + 0.5*width
y_center = (ymin+ymax)/2.; height = (ymax-ymin)*expansion_factor;
ymin = y_center - 0.5*height
ymax = y_center + 0.5*height
bbox = np.array([xmin, ymin, xmax - xmin, ymax - ymin]).astype(np.float32)
return bbox
def process_bbox(bbox, target_shape, original_img_shape):
# aspect ratio preserving bbox
w = bbox[2]
h = bbox[3]
c_x = bbox[0] + w/2.
c_y = bbox[1] + h/2.
aspect_ratio = target_shape[1]/target_shape[0]
if w > aspect_ratio * h:
h = w / aspect_ratio
elif w < aspect_ratio * h:
w = h * aspect_ratio
bbox[2] = w*1.25
bbox[3] = h*1.25
bbox[0] = c_x - bbox[2]/2.
bbox[1] = c_y - bbox[3]/2.
return bbox
import re
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [atoi(c) for c in re.split(r'(\d+)', text)]
# Load config
import yaml
def load_config(cfg_path):
with open(cfg_path, 'r') as f:
cfg = yaml.safe_load(f)
return cfg