def reconstruct(Iorig, I, Y, out_size, threshold=.9): net_stride = 2**4 side = ((208. + 40.) / 2.) / net_stride # 7.75 Probs = Y[..., 0] Affines = Y[..., 2:] rx, ry = Y.shape[:2] ywh = Y.shape[1::-1] iwh = np.array(I.shape[1::-1], dtype=float).reshape((2, 1)) xx, yy = np.where(Probs > threshold) WH = getWH(I.shape) MN = WH / net_stride vxx = vyy = 0.5 #alpha base = lambda vx, vy: np.matrix([[-vx, -vy, 1.], [vx, -vy, 1.], [vx, vy, 1.], [-vx, vy, 1.]]).T labels = [] for i in range(len(xx)): y, x = xx[i], yy[i] affine = Affines[y, x] prob = Probs[y, x] mn = np.array([float(x) + .5, float(y) + .5]) A = np.reshape(affine, (2, 3)) A[0, 0] = max(A[0, 0], 0.) A[1, 1] = max(A[1, 1], 0.) pts = np.array(A * base(vxx, vyy)) #*alpha pts_MN_center_mn = pts * side pts_MN = pts_MN_center_mn + mn.reshape((2, 1)) pts_prop = pts_MN / MN.reshape((2, 1)) labels.append(DLabel(0, pts_prop, prob)) final_labels = nms(labels, .1) TLps = [] if len(final_labels): final_labels.sort(key=lambda x: x.prob(), reverse=True) for i, label in enumerate(final_labels): t_ptsh = getRectPts(0, 0, out_size[0], out_size[1]) ptsh = np.concatenate((label.pts * getWH(Iorig.shape).reshape( (2, 1)), np.ones((1, 4)))) H = find_T_matrix(ptsh, t_ptsh) Ilp = cv2.warpPerspective(Iorig, H, out_size, borderValue=.0) TLps.append(Ilp) return final_labels, TLps
def augment_sample(I,pts, dim_w, dim_h, cls): maxsum,maxangle = 50,np.array([20.,20.,25.]) angles = np.random.rand(3)*maxangle if angles.sum() > maxsum: angles = (angles/angles.sum())*(maxangle/maxangle.sum()) I = im2single(I) iwh = getWH(I.shape) # codigo original # whratio = random.uniform(2., 4.) # wsiz = random.uniform(dim * .2, dim * 1.) # carro if cls=='0': whratio = random.uniform(2.75, 3.25) wsiz = random.uniform(dim_w * .2, dim_w * 0.7) else: whratio = random.uniform(0.7, 1.2) wsiz = random.uniform(dim_w * .2, dim_w * 0.7) # whratio = 1. # wsiz = random.uniform(dim_w*.2, dim_w*1.) # moto # whratio = random.uniform(0.7, 1.2) # wsiz = random.uniform(dim_w * .3, dim_w * 0.7) hsiz = wsiz/whratio dx = random.uniform(0.,dim_w - wsiz) dy = random.uniform(0.,dim_h - hsiz) pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz) pts = pts*iwh.reshape((2,1)) T = find_T_matrix(pts2ptsh(pts),pph) H = perspective_transform((dim_w,dim_h),angles=angles) H = np.matmul(H,T) Iroi,pts = project(I,H,pts,dim_w, dim_h) # hsv_mod = np.random.rand(3).astype('float32') # hsv_mod = (hsv_mod - .5)*.3 # hsv_mod[0] *= 360 # Iroi = hsv_transform(Iroi,hsv_mod) Iroi = np.clip(Iroi,0.,1.) pts = np.array(pts) # if random.random() > .5: # Iroi,pts = flip_image_and_pts(Iroi,pts) tl,br = pts.min(1),pts.max(1) llp = Label(0,tl,br) return Iroi,llp,pts
def augment_sample(I,pts,dim): maxsum,maxangle = 120,np.array([80.,80.,45.]) angles = np.random.rand(3)*maxangle if angles.sum() > maxsum: angles = (angles/angles.sum())*(maxangle/maxangle.sum()) I = im2single(I) # /255.0 归一化 iwh = getWH(I.shape) #得到宽高 whratio = random.uniform(2.,4.) # 随即取一个2-4的值作为宽高比 wsiz = random.uniform(dim*.2,dim*1.) # 宽取0.2*208 到 208之间 hsiz = wsiz/whratio dx = random.uniform(0.,dim - wsiz) dy = random.uniform(0.,dim - hsiz) #下面涉及到整个变换 # In the first 3 lines, the original corner points are transformed into a rectangular bounding box with aspect ratio # varying between 2:1 and 4:1. In other words, T matrix rectifies the LP with a random aspect ratio. Then, pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz) pts = pts*iwh.reshape((2,1)) #将点恢复到真实坐标值 T = find_T_matrix(pts2ptsh(pts),pph) #in the next two lines, a perspective transformation with random rotation (H) is combined with T to #generate the final transformation. H = perspective_transform((dim,dim),angles=angles) H = np.matmul(H,T) Iroi,pts = project(I,H,pts,dim) hsv_mod = np.random.rand(3).astype('float32') hsv_mod = (hsv_mod - .5)*.3 hsv_mod[0] *= 360 Iroi = hsv_transform(Iroi,hsv_mod) Iroi = np.clip(Iroi,0.,1.) pts = np.array(pts) if random.random() > .5: Iroi,pts = flip_image_and_pts(Iroi,pts) tl,br = pts.min(1),pts.max(1) llp = Label(0,tl,br) return Iroi,llp,pts
def augment_sample(I,pts,dim): maxsum,maxangle = 120,np.array([80.,80.,45.]) angles = np.random.rand(3)*maxangle if angles.sum() > maxsum: angles = (angles/angles.sum())*(maxangle/maxangle.sum()) # chuyển qua ma trận float32, chia cho 255 I = im2single(I) # Lấy w, h dạng ma trận iwh = getWH(I.shape) whratio = random.uniform(2.,4.) wsiz = random.uniform(dim*.2,dim*1.) hsiz = wsiz/whratio dx = random.uniform(0.,dim - wsiz) dy = random.uniform(0.,dim - hsiz) pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz) # Lấy tọa độ thật pts = pts*iwh.reshape((2,1)) T = find_T_matrix(pts2ptsh(pts),pph) H = perspective_transform((dim,dim),angles=angles) H = np.matmul(H,T) Iroi,pts = project(I,H,pts,dim) hsv_mod = np.random.rand(3).astype('float32') hsv_mod = (hsv_mod - .5)*.3 hsv_mod[0] *= 360 Iroi = hsv_transform(Iroi,hsv_mod) Iroi = np.clip(Iroi,0.,1.) pts = np.array(pts) if random.random() > .5: Iroi,pts = flip_image_and_pts(Iroi,pts) # lấy giá trị tọa độ top-left, bot-right tl,br = pts.min(1),pts.max(1) llp = Label(0,tl,br) return Iroi,llp,pts