|
@@ -3,26 +3,6 @@ from PIL.Image import Image as PIL_Image
|
|
|
|
|
|
DEFAULT_RATIO = np.sqrt(49 / 400)
|
|
|
|
|
|
-# def __expand_parts(p):
|
|
|
-# return p[:, 0], p[:, 1:3], p[:, 3].astype(bool)
|
|
|
-
|
|
|
-def rescale_parts(im, parts, part_rescale_size):
|
|
|
- if part_rescale_size is None or part_rescale_size < 0:
|
|
|
- return parts
|
|
|
- h, w, c = dimensions(im)
|
|
|
- scale = np.array([w, h]) / part_rescale_size
|
|
|
-
|
|
|
- xy = parts[:, 1:3]
|
|
|
- xy = xy * scale
|
|
|
- parts[:, 1:3] = xy
|
|
|
-
|
|
|
- if parts.shape[1] == 5:
|
|
|
- wh = parts[:, 3:5]
|
|
|
- wh = wh * scale
|
|
|
- parts[:, 3:5] = wh
|
|
|
-
|
|
|
- return parts
|
|
|
-
|
|
|
def dimensions(im):
|
|
|
if isinstance(im, np.ndarray):
|
|
|
if im.ndim != 3:
|
|
@@ -65,55 +45,7 @@ def uniform_parts(im, ratio=DEFAULT_RATIO, round_op=np.floor):
|
|
|
|
|
|
return parts
|
|
|
|
|
|
-def visible_part_locs(p):
|
|
|
- res = p.visible_locs()
|
|
|
- return res
|
|
|
-
|
|
|
- # idxs, locs, vis = __expand_parts(p)
|
|
|
- # return idxs[vis], locs[vis].T
|
|
|
-
|
|
|
-def crop(im, xy, w, h, padding_mode="edge", is_location=True):
|
|
|
-
|
|
|
- x, y = xy
|
|
|
- pad_h, pad_w = h // 2, w // 2
|
|
|
-
|
|
|
- padded_im = np.pad(im, [(pad_h, pad_h), (pad_w, pad_w), [0,0]], mode=padding_mode)
|
|
|
- x0, y0 = x + pad_w, y + pad_h
|
|
|
-
|
|
|
- if is_location:
|
|
|
- x0, y0 = x0 - w // 2, y0 - h // 2
|
|
|
-
|
|
|
- return padded_im[y0:y0+h, x0:x0+w]
|
|
|
-
|
|
|
-def crops(im, xys, ratio=DEFAULT_RATIO, padding_mode="edge"):
|
|
|
-
|
|
|
- h, w, c = dimensions(im)
|
|
|
- crop_h, crop_w = int(h * ratio), int(w * ratio)
|
|
|
- return np.stack([crop(im, xy, crop_w, crop_h, padding_mode)
|
|
|
- for xy in xys.T])
|
|
|
-
|
|
|
-def visible_crops(im, p, *args, **kw):
|
|
|
- res = p.visible_crops(*args, **kw)
|
|
|
- return res
|
|
|
-
|
|
|
- # idxs, locs, vis = __expand_parts(p)
|
|
|
- # parts = crops(asarray(im), locs[vis].T, *args, **kw)
|
|
|
- # res = np.zeros((len(idxs),) + parts.shape[1:], dtype=parts.dtype)
|
|
|
- # res[vis] = parts
|
|
|
- # return res
|
|
|
-
|
|
|
-def reveal_parts(im, xy, ratio=DEFAULT_RATIO):
|
|
|
- h, w, c = dimensions(im)
|
|
|
- crop_h, crop_w = int(h * ratio), int(w * ratio)
|
|
|
- im = asarray(im)
|
|
|
- res = np.zeros_like(im)
|
|
|
- for x, y in xy.T:
|
|
|
- x0, y0 = max(x - crop_w // 2, 0), max(y - crop_h // 2, 0)
|
|
|
- res[y0:y0+crop_h, x0:x0+crop_w] = im[y0:y0+crop_h, x0:x0+crop_w]
|
|
|
-
|
|
|
- return res
|
|
|
-
|
|
|
-def select(crops, mask):
|
|
|
+def select_crops(crops, mask):
|
|
|
selected = np.zeros_like(crops)
|
|
|
selected[mask] = crops[mask]
|
|
|
return selected
|
|
@@ -127,7 +59,7 @@ def random_select(idxs, xy, part_crops, *args, **kw):
|
|
|
xy = xy[:, rnd_idxs]
|
|
|
|
|
|
mask = selection_mask(idxs, len(part_crops))
|
|
|
- selected_crops = select(part_crops, mask)
|
|
|
+ selected_crops = select_crops(part_crops, mask)
|
|
|
|
|
|
return idxs, xy, selected_crops
|
|
|
|
|
@@ -143,3 +75,82 @@ def random_idxs(idxs, rnd=None, n_parts=None):
|
|
|
res = rnd.choice(idxs, n_parts, replace=False)
|
|
|
res.sort()
|
|
|
return res
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+##### DEPRECATED #####
|
|
|
+
|
|
|
+# def __expand_parts(p):
|
|
|
+# return p[:, 0], p[:, 1:3], p[:, 3].astype(bool)
|
|
|
+
|
|
|
+def rescale_parts(im, parts, part_rescale_size):
|
|
|
+ raise DeprecationWarning("Do not use me!")
|
|
|
+ # if part_rescale_size is None or part_rescale_size < 0:
|
|
|
+ # return parts
|
|
|
+ # h, w, c = dimensions(im)
|
|
|
+ # scale = np.array([w, h]) / part_rescale_size
|
|
|
+
|
|
|
+ # xy = parts[:, 1:3]
|
|
|
+ # xy = xy * scale
|
|
|
+ # parts[:, 1:3] = xy
|
|
|
+
|
|
|
+ # if parts.shape[1] == 5:
|
|
|
+ # wh = parts[:, 3:5]
|
|
|
+ # wh = wh * scale
|
|
|
+ # parts[:, 3:5] = wh
|
|
|
+
|
|
|
+ # return parts
|
|
|
+
|
|
|
+def visible_part_locs(p):
|
|
|
+ raise DeprecationWarning("Do not use me!")
|
|
|
+ # res = p.visible_locs()
|
|
|
+ # return res
|
|
|
+
|
|
|
+ # idxs, locs, vis = __expand_parts(p)
|
|
|
+ # return idxs[vis], locs[vis].T
|
|
|
+
|
|
|
+
|
|
|
+def crop(im, xy, w, h, padding_mode="edge", is_location=True):
|
|
|
+ raise DeprecationWarning("Do not use me!")
|
|
|
+
|
|
|
+ # x, y = xy
|
|
|
+ # pad_h, pad_w = h // 2, w // 2
|
|
|
+
|
|
|
+ # padded_im = np.pad(im, [(pad_h, pad_h), (pad_w, pad_w), [0,0]], mode=padding_mode)
|
|
|
+ # x0, y0 = x + pad_w, y + pad_h
|
|
|
+
|
|
|
+ # if is_location:
|
|
|
+ # x0, y0 = x0 - w // 2, y0 - h // 2
|
|
|
+
|
|
|
+ # return padded_im[y0:y0+h, x0:x0+w]
|
|
|
+
|
|
|
+def crops(im, xys, ratio=DEFAULT_RATIO, padding_mode="edge"):
|
|
|
+ raise DeprecationWarning("Do not use me!")
|
|
|
+
|
|
|
+ # h, w, c = dimensions(im)
|
|
|
+ # crop_h, crop_w = int(h * ratio), int(w * ratio)
|
|
|
+ # return np.stack([crop(im, xy, crop_w, crop_h, padding_mode)
|
|
|
+ # for xy in xys.T])
|
|
|
+
|
|
|
+def visible_crops(im, p, *args, **kw):
|
|
|
+ raise DeprecationWarning("Do not use me!")
|
|
|
+ # res = p.visible_crops(*args, **kw)
|
|
|
+ # return res
|
|
|
+
|
|
|
+ # idxs, locs, vis = __expand_parts(p)
|
|
|
+ # parts = crops(asarray(im), locs[vis].T, *args, **kw)
|
|
|
+ # res = np.zeros((len(idxs),) + parts.shape[1:], dtype=parts.dtype)
|
|
|
+ # res[vis] = parts
|
|
|
+ # return res
|
|
|
+
|
|
|
+def reveal_parts(im, xy, ratio=DEFAULT_RATIO):
|
|
|
+ raise DeprecationWarning("Do not use me!")
|
|
|
+ # h, w, c = dimensions(im)
|
|
|
+ # crop_h, crop_w = int(h * ratio), int(w * ratio)
|
|
|
+ # im = asarray(im)
|
|
|
+ # res = np.zeros_like(im)
|
|
|
+ # for x, y in xy.T:
|
|
|
+ # x0, y0 = max(x - crop_w // 2, 0), max(y - crop_h // 2, 0)
|
|
|
+ # res[y0:y0+crop_h, x0:x0+crop_w] = im[y0:y0+crop_h, x0:x0+crop_w]
|
|
|
+
|
|
|
+ # return res
|