Data Augmentation


Resize the image data such that the aspect ratio is still constant.


def resizeRetainAspect(img,targetdim=(640,480)):
    targetaspect = targetdim[0]/targetdim[1]
    shape = img.shape[:2]
    origaspect = shape[0]/shape[1]
    if targetaspect == origaspect:
        ratio = targetdim[0]/shape[0]
        shift = 0
        resizeimg = cv2.resize(img,targetdim[::-1])
        return resizeimg,ratio,shift,None,resizeimg.shape[:2]
    elif targetaspect > origaspect:
        ratio = targetdim[1]/shape[1]
        resizeimg = cv2.resize(img,
                (targetdim[1],int(ratio*shape[0])))
        padimg = np.zeros(targetdim+(3,),resizeimg.dtype)
        shift = abs(targetdim[0]-resizeimg.shape[0])//2
        padimg[shift:shift+resizeimg.shape[0],:,:] = resizeimg
        description = 't > o'
    elif targetaspect < origaspect:
        ratio = targetdim[0]/shape[0]
        resizeimg = cv2.resize(img,
                (int(ratio*shape[1]),targetdim[0]))
        padimg = np.zeros(targetdim+(3,),resizeimg.dtype)
        shift = abs(targetdim[1]-resizeimg.shape[1])//2
        padimg[:,shift:shift+resizeimg.shape[1],:] = resizeimg
        description = 'o>t'
    return padimg,ratio,shift,description,resizeimg.shape[:2]
    

New Coordinates of points after resizing


bbox = []
for x0,y0,x1,y1 in zip(x0l,y0l,x1l,y1l):
    if d:
        if d == "t>o":
            x0,x1 = int(x0*r),int(x1*r)
            r0,r1 = y0/h,y1/h
            y0,y1 = int(r0*shp[0]+s),int(r1*shp[0]+s)
        elif d == "o>t":
            y0,y1 = int(y0*r),int(y1*r)
            r0,r1 = x0/w,x1/w
            x0,x1 = int(r0*shp[1]+s),int(r1*shp[1]+s)
    else:
        x0,y0,x1,y1 = int(x0*r),int(y0*r),int(x1*r),int(y1*r)
    bbox.append([x0,y0,x1,y1])
    

Rotate 90,180,270 degrees


def rotate(img,bbox,deg):
    if deg == 0:
        return img,bbox
    tmpimg = Image.fromarray(img)
    rot = np.array(tmpimg.rotate(deg))
    rad = np.deg2rad(deg)
    bbox_ = []
    for box in bbox:
        x0,y0,x1,y1 = box
        x0_ = int(np.cos(rad)*(x0-320)+np.sin(rad)*(y0-320))+320
        y0_ = int(np.cos(rad)*(y0-320)-np.sin(rad)*(x0-320))+320
        x1_ = int(np.cos(rad)*(x1-320)+np.sin(rad)*(y1-320))+320
        y1_ = int(np.cos(rad)*(y1-320)-np.sin(rad)*(x1-320))+320
        bbox_.append([x0_,y0_,x1_,y1_])
    return rot,bbox_
    

Flip x and y


def flip(img,bbox,mode='x'):
    h,w,_ = img.shape
    if mode == 'x':
        f = img[:,::-1,:]
    elif mode == 'y':
        f = img[::-1,:,:]
    f = np.ascontiguousarray(f)
    bbox_ = []
    for box in bbox:
        x0,y0,x1,y1 = box
        if mode == 'x':
            x0 = int(w-x0); x1 = int(w-x1)
        elif mode == 'y':
            y0 = int(h-y0); y1 = int(h-y1)
        if x0>x1:
            tmp = x0; x0 = x1; x1 = tmp
        if y0>y1:
            tmp = y0; y0 = y1; y1 = tmp
        bbox_.append([x0,y0,x1,y1])
    return f,bbox_
    

References