def createPlaceDefault(p,name): if not name in defaultPlace.keys(): name='unknown' MPS=defaultPlace[name][1] mps=defaultPlace[name][0] if(defaultPlace[name][2]=='free'): p1=Point(random.randint(0,CITY_SIZE_X),random.randint(0,CITY_SIZE_Y)) p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) if(defaultPlace[name][2]=='center'): p1=Point(random.randint(p.x-MPS,p.x+MPS),random.randint(p.y-MPS,p.y+MPS)) p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) if(defaultPlace[name][2]=='urban'): p1=Point(random.randint(int(p.x-int(CITY_SIZE_X/3)-MPS),int(p.x+int(CITY_SIZE_X/3)+MPS)),random.randint(int(p.y-int(CITY_SIZE_Y/3)-MPS),p.y+int(CITY_SIZE_Y/3)+MPS)) p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) if(defaultPlace[name][2]=='rural'): p1=Point(random.randint(int(p.x-int(CITY_SIZE_X/3)-MPS),int(p.x+int(CITY_SIZE_X/3)+MPS)),random.randint(int(p.y-int(CITY_SIZE_Y/3)-MPS),p.y+int(CITY_SIZE_Y/3)+MPS)) if(random.randint(0,1)==0): p1.x=p1.x+int(CITY_SIZE_X/3) else: p1.x=p1.x-(CITY_SIZE_X/3) if(random.randint(0,1)==0): p1.y=p1.y+int(CITY_SIZE_Y/3) else: p1.y=p1.y-int(CITY_SIZE_Y/3) p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) place=Rect(p1,p2) return place
def createPlaceDefault(p, name): if not name in list(defaultPlace.keys()): name = 'unknown' mps = eval_eqn(defaultPlace[name][0]) MPS = eval_eqn(defaultPlace[name][1]) #print(name,mps,MPS) thirdx = config['CITY_SIZE_X'] // 3 thirdx_up = thirdx + MPS thirdx_dn = thirdx - MPS thirdy = config['CITY_SIZE_Y'] // 3 thirdy_up = thirdy + MPS thirdy_dn = thirdy - MPS if (defaultPlace[name][2] == 'free'): p1 = Point(random.randint(0, config['CITY_SIZE_X']), random.randint(0, config['CITY_SIZE_Y'])) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) if (defaultPlace[name][2] == 'center'): p1 = Point(random.randint(p.x - MPS, p.x + MPS), random.randint(p.y - MPS, p.y + MPS)) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) if (defaultPlace[name][2] == 'urban'): p1 = Point(random.randint(p.x - thirdx_dn, p.x + thirdx_up), random.randint(p.y - thirdy_dn, p.y + thirdy_up)) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) if (defaultPlace[name][2] == 'rural'): p1 = Point(random.randint(p.x - thirdx_dn, p.x + thirdx_up), random.randint(p.y - thirdy_dn, p.y + thirdy_up)) if (random.randint(0, 1) == 0): p1.x = p1.x + thirdx else: p1.x = p1.x - thirdx if (random.randint(0, 1) == 0): p1.y = p1.y + thirdy else: p1.y = p1.y - thirdy p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) place = Rect(p1, p2) return place
def createPlaceDefault(p,name): if not name in list(defaultPlace.keys()): name='unknown' mps=eval_eqn(defaultPlace[name][0]) MPS=eval_eqn(defaultPlace[name][1]) #print(name,mps,MPS) thirdx = config['CITY_SIZE_X']//3 thirdx_up = thirdx + MPS thirdx_dn = thirdx - MPS thirdy = config['CITY_SIZE_Y']//3 thirdy_up = thirdy + MPS thirdy_dn = thirdy - MPS if(defaultPlace[name][2]=='free'): p1=Point(random.randint(0,config['CITY_SIZE_X']),random.randint(0,config['CITY_SIZE_Y'])) p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) if(defaultPlace[name][2]=='center'): p1=Point(random.randint(p.x-MPS,p.x+MPS),random.randint(p.y-MPS,p.y+MPS)) p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) if(defaultPlace[name][2]=='urban'): p1=Point( random.randint(p.x-thirdx_dn, p.x+thirdx_up), random.randint(p.y-thirdy_dn, p.y+thirdy_up) ) p2=Point( random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS) ) if(defaultPlace[name][2]=='rural'): p1=Point(random.randint( p.x-thirdx_dn,p.x+thirdx_up), random.randint(p.y-thirdy_dn,p.y+thirdy_up)) if(random.randint(0,1)==0): p1.x=p1.x+thirdx else: p1.x=p1.x-thirdx if(random.randint(0,1)==0): p1.y=p1.y+thirdy else: p1.y=p1.y-thirdy p2=Point(random.randint(p1.x+mps,p1.x+MPS), random.randint(p1.y+mps,p1.y+MPS)) place=Rect(p1,p2) return place
def createPlaceDefault(p, name): if not name in defaultPlace.keys(): name = 'unknown' MPS = defaultPlace[name][1] mps = defaultPlace[name][0] if (defaultPlace[name][2] == 'free'): p1 = Point(random.randint(0, CITY_SIZE_X), random.randint(0, CITY_SIZE_Y)) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) if (defaultPlace[name][2] == 'center'): p1 = Point(random.randint(p.x - MPS, p.x + MPS), random.randint(p.y - MPS, p.y + MPS)) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) if (defaultPlace[name][2] == 'urban'): p1 = Point( random.randint(int(p.x - int(CITY_SIZE_X / 3) - MPS), int(p.x + int(CITY_SIZE_X / 3) + MPS)), random.randint(int(p.y - int(CITY_SIZE_Y / 3) - MPS), p.y + int(CITY_SIZE_Y / 3) + MPS)) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) if (defaultPlace[name][2] == 'rural'): p1 = Point( random.randint(int(p.x - int(CITY_SIZE_X / 3) - MPS), int(p.x + int(CITY_SIZE_X / 3) + MPS)), random.randint(int(p.y - int(CITY_SIZE_Y / 3) - MPS), p.y + int(CITY_SIZE_Y / 3) + MPS)) if (random.randint(0, 1) == 0): p1.x = p1.x + int(CITY_SIZE_X / 3) else: p1.x = p1.x - (CITY_SIZE_X / 3) if (random.randint(0, 1) == 0): p1.y = p1.y + int(CITY_SIZE_Y / 3) else: p1.y = p1.y - int(CITY_SIZE_Y / 3) p2 = Point(random.randint(p1.x + mps, p1.x + MPS), random.randint(p1.y + mps, p1.y + MPS)) place = Rect(p1, p2) return place
def _restore_rects(self, mp, cls, scale, ratio, threshold=None, top=None): # print ratio map_h, map_w = mp.shape[:2] tmp = math.sqrt(ratio) rect_h = (scale / tmp) rect_w = (scale * tmp) # print "h, w", rect_h, rect_w def cut_top(res): res = sorted(res, reverse=True, key=lambda val: val[0]) if top is not None: res = res[:top] return res res = [] for y in range(0, map_h): for x in range(0, map_w): base_center = Point() base_center.y = (y + 0.5) / map_h base_center.x = (x + 0.5) / map_w dy, dx, sh, sw = mp[y, x].reshape((4, )) dy, dx, sh, sw = 0, 0, 0, 0 #sh, sw = 0, 0 #dy, dx = 0, 0 sh = min(max(sh, -10), 10) sw = min(max(sw, -10), 10) conf = cls[y][x] if threshold is not None or conf > threshold: r_x = dx * rect_w + base_center.x r_y = dy * rect_h + base_center.y r_w = rect_w * math.exp(sw) r_h = rect_h * math.exp(sh) center = Point() center.x = r_x center.y = r_y res.append((conf, center.rect_from_center(r_h, r_w))) if top is not None and len(res) > 2 * top: res = cut_top(res) return cut_top(res)
def _get_bblox_info(self, rects, img_h, img_w, map_h, map_w, scale, ratio): tmp = math.sqrt(ratio) # convert everything to img 0-1 axes rect_h = (scale / tmp) rect_w = (scale * tmp) # print "h, w", rect_h, rect_w result = [] for y in range(0, map_h): for x in range(0, map_w): base_center = Point() base_center.y = (y + 0.5) / map_h base_center.x = (x + 0.5) / map_w base_rect = base_center.rect_from_center(rect_h, rect_w) # l, t, r, b = cx - rect_w_2, cy - rect_h_2, cx + rect_w_2, cy + rect_h_2 for rect_real in rects: rect = rect_real.copy() rect.stretch(1.0 / img_h, 1.0 / img_w) inner = rect.intersection(base_rect) outer = rect.union(base_rect) r_height = rect.height() r_width = rect.width() is_same_oriented = (base_rect.width() <= base_rect.height()) == (r_width <= r_height) th_proportion = 4 is_high_disproportional = th_proportion * r_width < r_height or th_proportion * r_height < r_width th_weak_const = th_proportion * min( r_width, r_height) / max(r_width, r_height, 0.0001) # < 1 weak_const = th_weak_const if is_same_oriented and is_high_disproportional else 1 ''' if is_same_oriented and is_high_disproportional: if base_rect.width() > base_rect.height(): print '++++++++++' print '################################' print r_height, r_width print th_weak_const, weak_const print ',,,' base_rect.dump() rect.dump() inner.dump() outer.dump() print inner.area(), outer.area() ''' if inner.valid( ) and inner.area() > 0.5 * outer.area() * weak_const: ''' if is_same_oriented and is_high_disproportional: print '################################' print r_height, r_width print th_weak_const, weak_const print ',,,' base_rect.dump() rect.dump() inner.dump() outer.dump() print inner.area(), outer.area(), weak_const print '-----' base_rect.dump() rect.dump() inner.dump() outer.dump() print inner.area(), outer.area() ''' center = rect.center() max_shift = 2.0 dx = min((center.x - base_center.x) / (base_rect.width() + .0), max_shift) dy = min((center.y - base_center.y) / (base_rect.height() + .0), max_shift) max_stretch = 2.0 sh = math.log( min(rect.height() / (base_rect.height() + .0), max_stretch)) sw = math.log( min(rect.width() / (base_rect.width() + .0), max_stretch)) result.append(np.array([y, x, dy, dx, sh, sw])) return np.array(result)
def _restore_rects(self, tdy, tdx, tsh, tsw, cls, scale, ratio, threshold=None, top=None): # print ratio map_h, map_w = cls.shape[1:3] print map_h, map_w print cls.shape tmp = math.sqrt(ratio) rect_h = (scale / tmp) rect_w = (scale * tmp) # print "h, w", rect_h, rect_w def cut_top(res): res = sorted(res, reverse=True, key=lambda val: val[0]) if top is not None: res = res[:top] return res res = [] for y in range(0, map_h): for x in range(0, map_w): base_center = Point() base_center.y = (y + 0.5) / map_h base_center.x = (x + 0.5) / map_w # print tdy.shape dy, dx, sh, sw = tdy[0, y, x, 0], tdx[0, y, x, 0], tsh[0, y, x, 0], tsw[0, y, x, 0] dy, dx, sh, sw = 0, 0, 0, 0 #sh, sw = 0, 0 #dy, dx = 0, 0 # print cls.shape conf = cls[0, y, x, 0] if conf > 0: print conf if threshold is not None or conf > threshold: r_x = dx * rect_w + base_center.x r_y = dy * rect_h + base_center.y r_w = rect_w * math.exp(sw) r_h = rect_h * math.exp(sh) center = Point() center.x = r_x center.y = r_y res.append((conf, center.rect_from_center(r_h, r_w))) if top is not None and len(res) > 2 * top: res = cut_top(res) return cut_top(res)
def _get_bblox_info(self, rects, img_h, img_w, map_h, map_w, scale, ratio): tmp = math.sqrt(ratio) # convert everything to img 0-1 axes rect_h = (scale / tmp) rect_w = (scale * tmp) # print "h, w", rect_h, rect_w result = [] for y in range(0, map_h): for x in range(0, map_w): base_center = Point() base_center.y = (y + 0.5) / map_h base_center.x = (x + 0.5) / map_w base_rect = base_center.rect_from_center(rect_h, rect_w) # l, t, r, b = cx - rect_w_2, cy - rect_h_2, cx + rect_w_2, cy + rect_h_2 for rect_real in rects: rect = rect_real.copy() rect.stretch(1.0 / img_h, 1.0 / img_w) inner = rect.intersection(base_rect) outer = rect.union(base_rect) # r_l, r_t, r_r, r_b = np.array(rect, dtype=float) # r_l, r_t, r_r, r_b = r_l / img_w, r_t / img_h, r_r / img_w, r_b / img_h # i_l, i_t, i_r, i_b = max(l, r_l), max(t, r_t), min(r, r_r), min(b, r_b) # o_l, o_t, o_r, o_b = min(l, r_l), min(t, r_t), max(r, r_r), max(b, r_b) # print r_l, r_t, r_r, r_b # print l, t, r, b # print r_height = rect.height() r_width = rect.width() is_same_oriented = (base_rect.width() <= base_rect.height()) == (r_width <= r_height) th_proportion = 4 is_high_disproportional = th_proportion * r_width < r_height or th_proportion * r_height < r_width th_weak_const = th_proportion * min( r_width, r_height) / max(r_width, r_height, 0.0001) # < 1 weak_const = th_weak_const if is_same_oriented and is_high_disproportional else 1 ''' if is_same_oriented and is_high_disproportional: if base_rect.width() > base_rect.height(): print '++++++++++' print '################################' print r_height, r_width print th_weak_const, weak_const print ',,,' base_rect.dump() rect.dump() inner.dump() outer.dump() print inner.area(), outer.area() ''' if inner.valid( ) and inner.area() > 0.5 * outer.area() * weak_const: ''' if is_same_oriented and is_high_disproportional: print '################################' print r_height, r_width print th_weak_const, weak_const print ',,,' base_rect.dump() rect.dump() inner.dump() outer.dump() print inner.area(), outer.area(), weak_const print '-----' base_rect.dump() rect.dump() inner.dump() outer.dump() print inner.area(), outer.area() ''' # if i_l < i_r and i_t < i_b: # o_area = (o_r - o_l) * (o_b - o_t) # i_area = (i_r - i_l) * (i_b - i_t) # if i_area > o_area * 0.5: # r_c_x = 0.5 * (r_l + r_r) # r_c_y = 0.5 * (r_t + r_b) center = rect.center() # r_h = r_b - r_t # r_w = r_r - r_l max_shift = 2.0 dx = min((center.x - base_center.x) / (base_rect.width() + .0), max_shift) dy = min((center.y - base_center.y) / (base_rect.height() + .0), max_shift) # dy = (r_c_y - cy) / (2 * rect_h_2) # dx = (r_c_x - cx) / (2 * rect_w_2) max_stretch = 2.0 sh = math.log( min(rect.height() / (base_rect.height() + .0), max_stretch)) sw = math.log( min(rect.width() / (base_rect.width() + .0), max_stretch)) # sh = math.log(r_h / (2 * rect_h_2)) # sw = math.log(r_w / (2 * rect_w_2)) result.append(np.array([y, x, dy, dx, sh, sw])) return np.array(result)