MCPcopy Create free account
hub / github.com/ModelTC/LightX2V / crop

Function crop

tools/preprocess/pose2d_utils.py:1026–1056  ·  view source on GitHub ↗

Crop image according to the supplied bounding box. res: [rows, cols]

(img, center, scale, res)

Source from the content-addressed store, hash-verified

1024
1025
1026def crop(img, center, scale, res):
1027 """
1028 Crop image according to the supplied bounding box.
1029 res: [rows, cols]
1030 """
1031 # Upper left point
1032 ul = np.array(transform([1, 1], center, max(scale), res, invert=1)) - 1
1033 # Bottom right point
1034 br = np.array(transform([res[1] + 1, res[0] + 1], center, max(scale), res, invert=1)) - 1
1035
1036 # Padding so that when rotated proper amount of context is included
1037 pad = int(np.linalg.norm(br - ul) / 2 - float(br[1] - ul[1]) / 2)
1038
1039 new_shape = [br[1] - ul[1], br[0] - ul[0]]
1040 if len(img.shape) > 2:
1041 new_shape += [img.shape[2]]
1042 new_img = np.zeros(new_shape, dtype=np.float32)
1043
1044 # Range to fill new array
1045 new_x = max(0, -ul[0]), min(br[0], len(img[0])) - ul[0]
1046 new_y = max(0, -ul[1]), min(br[1], len(img)) - ul[1]
1047 # Range to sample from original image
1048 old_x = max(0, ul[0]), min(len(img[0]), br[0])
1049 old_y = max(0, ul[1]), min(len(img), br[1])
1050 try:
1051 new_img[new_y[0] : new_y[1], new_x[0] : new_x[1]] = img[old_y[0] : old_y[1], old_x[0] : old_x[1]]
1052 except Exception as e:
1053 print(e)
1054
1055 new_img = cv2.resize(new_img, (res[1], res[0])) # (cols, rows)
1056 return new_img, new_shape, (old_x, old_y), (new_x, new_y) # , ul, br
1057
1058
1059def split_kp2ds_for_aa(kp2ds, ret_face=False):

Callers 1

preprocessMethod · 0.90

Calls 2

transformFunction · 0.85
resizeMethod · 0.45

Tested by

no test coverage detected