Skip to content

Instantly share code, notes, and snippets.

@sizhky
Created January 8, 2021 05:51
Show Gist options
  • Save sizhky/756f7069541ed41f6c3368a0fabce9cb to your computer and use it in GitHub Desktop.
Save sizhky/756f7069541ed41f6c3368a0fabce9cb to your computer and use it in GitHub Desktop.
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/Untitled.ipynb (unless otherwise specified).
__all__ = ['iou', 'patch', 'multi_patch']
# Cell
#export
from torch_snippets import *
def iou(A,B):
A = np.array(A)
B = np.array(B)
low = np.s_[...,:2]
high = np.s_[...,2:]
A,B = A.copy()[:,None],B.copy()[None]
# A[high] += 1; B[high] += 1
intrs = (np.maximum(0,np.minimum(A[high],B[high])
-np.maximum(A[low],B[low]))).prod(-1)
return intrs / ((A[high]-A[low]).prod(-1)+(B[high]-B[low]).prod(-1)-intrs)
def patch(im, IM, origin=None, only_black_pixels=True):
"Modify IM inplace and return the origin of pasting"
try:
h, w = im.shape
H, W = IM.shape
if origin is not None:
x, y = origin
else:
x, y = randint(W-w), randint(H-h)
if only_black_pixels:
nzpxls = ys, xs = np.nonzero(255 - im)
nzpxls_IM = ys+y, xs+x
IM[nzpxls_IM] = im[nzpxls]
else:
IM[y:y+h, x:x+w] = im
return x, y
except Exception as e:
logger.warning(e)
return -1
def multi_patch(IM, ims):
H, W = IM.shape
head = _X, _Y = randint(10), randint(10)
heights = []
wbbs = []
lines, line, line_ixs, line_no = [], [], [], 0
for ix,im in enumerate(ims):
h, w = im.shape
heights.append(h)
if _X + w < W:
res = patch(im, IM, origin=(_X, _Y))
wbbs.append((_X,_Y,_X+w,_Y+h))
_X = _X + w + 20
else:
head = _X, _Y = randint(10), _Y + max(heights) + 10
res = patch(im, IM, origin=(_X, _Y))
wbbs.append((_X,_Y,_X+w,_Y+h))
_X = _X + w + 20
return wbbs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment