def segment_board_from_edges(img, edges): intersections = [] for i in range(4): intersection = utils.find_intersection(edges[i], edges[(i + 1) % 4]) intersections.append((int(intersection[0]), int(intersection[1]))) return segment_board(img, intersections)
def get_intersections(lines): intersections = [] for i in range(len(lines)): for j in range(i + 1, len(lines)): intersection = utils.find_intersection(lines[i], lines[j]) if intersection is not None: try: intersections.append( (int(intersection[0]), int(intersection[1]))) except OverflowError: pass return intersections
def move(self, colliders): rem_vel_x = self.velocity_x rem_vel_y = self.velocity_y while rem_vel_x or rem_vel_y: # while the ball can still move target_pos = Vector(rem_vel_x, rem_vel_y) + self.center for collider in colliders: point1, point2 = collider bounce_pos = utils.find_intersection(*self.center, *target_pos, *point1, *point2) if bounce_pos is None: continue # Will never collide unless angle changes distance_bounce = utils.distance(*self.center, *bounce_pos) - self.radius distance_target = utils.distance(*self.center, *target_pos) if distance_bounce > distance_target: continue # Did not collide yet if not utils.is_between(*collider, bounce_pos): continue # Moves past collider break else: # Did not collide with any collider -> free to move self.center_x += rem_vel_x * self.mod self.center_y += rem_vel_y * self.mod break dist_x = utils.to_zero(bounce_pos[0] - self.center_x, rem_vel_x) dist_y = utils.to_zero(bounce_pos[1] - self.center_y, rem_vel_y) rem_vel_x -= dist_x rem_vel_y -= dist_y if collider[0][0] == collider[1][0]: # collider is vertical dist_x = -dist_x rem_vel_x = -rem_vel_x self.velocity_x = -self.velocity_x elif collider[0][1] == collider[1][1]: # collider is horizontal dist_y = -dist_y rem_vel_y = -rem_vel_y self.velocity_y = -self.velocity_y else: raise ValueError("Collider", collider, "has to be a straight line") self.center_x += dist_x * self.mod self.center_y += dist_y * self.mod self.mod += .1
# set to validate after injection print("Validating all the sameAs links in " + val_path) val_set = inj.extract_sameas(val_path) V = len(val_set) # the golden standard i.e. all the correct sameAs gold_set = inj.extract_sameas(refalign_path) G = len(gold_set) # sanity check assert int(G / V) == int(1 / (1 + ratio)) # validate the sameAs links wrong_sameas = val.invalidate_sameas(val_set, g_source, g_target, depth) inters = ut.find_intersection(wrong_sameas, error_links) end_time = tm.time() # print("#False sameAs links found: \t%d" % len(wrong_sameas)) # print("#Error links injected: \t\t%d" % len(error_links)) # print("#SameAs links to validate: \t%d" % V) # print("#False sameAs links found among the injected ones: \t%d\n" % len(inters)) precision = len(inters) / len(error_links) recall = len(inters) / len(wrong_sameas) if len(wrong_sameas) != 0 else 0 time = end_time - start_time print("------------------------") print("Precision:\t", precision) print("Recall:\t\t", recall) print("Running time:\t %f s" % time)
def forward(self): """ This is the main function that imports other modules to achieve the proposed goal of iterative and adaptive sampling. Returns: all_maps_dict (dict): Dictionary of image path:best saliency map dict. """ all_maps_dict = {} for img_path in tqdm(glob.glob(self.query_path)): self.im_path = img_path img = self.read_tensor(self.im_path) img = img.cuda() self.explainer.generate_b_masks(self.mask_path, self.init_window_size, self.init_stride) self.LRPF = LRPFSA(img, self.model_name, self.lambda_fac, self.t_thresh, net_in_size) # Annotations were named comparable to images im_name = img_path.split('/')[-1].split('.')[0] init_sal = self.get_initial_map(img) # Setting the best deletion and insertion area to 100000 and 0 for max and min value. best_del_area = 100000 best_iou = 0 for n, i in enumerate(range(self.k)): new_sal, Har_mask = self.LRPF.forward(init_sal) # Loading annotations for evaluation mask = cv2.imread( "{}{}.png".format(self.annotation_path, im_name.split('_')[0]), 0) mask = mask / 255 mask = cv2.resize(mask, (net_in_size, net_in_size)) # Calculate the IoU between thresholded saliency map and segmentation annotation # and evaluating to store the best saliency map for viewing if self.save_final_sal == True: iou = get_iou_(mask, Har_mask) f1 = f1_score(mask.flatten().round(), Har_mask.flatten().astype(float).round(), average='micro') point_game = pointing_game_score(mask, Har_mask) area_del, area_ins = self.evaluate(img, new_sal) # IoU set as selection criteria if iou > best_iou: best_iou = iou best_sal = new_sal # Attentuation factor for window size and stride. self.explainer.generate_b_masks( window_size=int(self.init_window_size - ((n + 1) * 1.5)), stride=int(self.init_stride - (0.2 * n)), savepath=self.mask_path) # Obtaining sampling regions for next iteration. new_Har_masks = torch.cat(find_intersection( self.explainer.masks, Har_mask), dim=0).unsqueeze(1) # Loading new masks for use in next iteration self.explainer.masks = new_Har_masks.cuda() self.explainer.N = new_Har_masks.size(0) init_sal = self.get_initial_map(img) # Saving the best saliency map for each query image. if self.save_final_sal == True: all_maps_dict[self.im_path] = best_sal np.save((self.save_path + self.im_path.split('/')[-1].split('.')[0] + '.npy'), best_sal) plt.figure() plt.axis('off') plt.imshow(best_sal, cmap='jet') plt.savefig(self.save_path + self.im_path.split('/')[-1]) return all_maps_dict
def update(self): focus, directrix = self.parents fx, fy = focus.coord d1x, d1y = directrix.parents[0].coord d2x, d2y = directrix.parents[1].coord mdist = dist((fx, fy), directrix) * .5 mpoints = (fx + mdist, fy), (fx - mdist, fy), (fx, fy + mdist), (fx, fy - mdist) mpoint = filter(lambda x: round(dist((fx, fy), x), 1) == round(dist(x, directrix), 1), mpoints) if mpoint: mx, my = mpoint[0] line1 = (fx, fy), (fx + 1, fy + 1) line2 = (fx, fy), (fx + 1, fy - 1) d = (d1x, d1y), (d2x, d2y) int1 = find_intersection(line1, d) int2 = find_intersection(line2, d) if int1 and int2: t1x,t1y = int1 t2x,t2y = int2 elif int1 and not int2: t1x,t1y = int1 t2x,t2y = mx,my elif not int1 and int2: t1x,t1y = mx,my t2x,t2y = int2 int1_points = (t1x, fy), (fx, t1y) int2_points = (t2x, fy), (fx, t2y) int1 = filter( lambda x: round(dist(x, directrix), 1) == round(dist(x, (fx, fy)), 1), int1_points) int2 = filter( lambda x: round(dist(x, directrix), 1) == round(dist(x, (fx, fy)), 1), int2_points) if int1: t1x, t1y = int1[0] if int2: t2x, t2y = int2[0] # Determine the extreme points. far1_points = (t1x - 2000, t1y), (t1x + 2000, t1y), (t1x, t1y - 2000), (t1x, t1y + 2000) far2_points = (t2x - 2000, t2y), (t2x + 2000, t2y), (t2x, t2y - 2000), (t2x, t2y + 2000) far1 = filter( lambda x: round(dist(x, directrix), 1) == round(dist(x, (fx, fy)), 1), far1_points) far2 = filter( lambda x: round(dist(x, directrix), 1) == round(dist(x, (fx, fy)), 1), far2_points) if far1: far1x, far1y = far1[0] if far2: far2x, far2y = far2[0] if int1 and int2 and far1 and far2: c.coords(self.handle, far1x, far1y, t1x, t1y, mx, my, t2x, t2y, far2x, far2y)
def update(self): line, p = self.parents px, py = p.coord # Get the distance between the point and the line. pl_dist = dist((px, py), line) r = pl_dist + 50 # Find the four points where the circle lines from p intersect the line. cp = (px + r, py), (px, py + r), (px - r, py), (px, py - r) intersections = [] intersections.append(find_intersection((cp[0], cp[1]), (line.parents[0].coord, line.parents[1].coord))) intersections.append(find_intersection((cp[1], cp[2]), (line.parents[0].coord, line.parents[1].coord))) intersections.append(find_intersection((cp[2], cp[3]), (line.parents[0].coord, line.parents[1].coord))) intersections.append(find_intersection((cp[3], cp[0]), (line.parents[0].coord, line.parents[1].coord))) intersections = [intersection for intersection in intersections if intersection] def on_circle(point): d = round(dist(p.coord, point), 1) radius = round(r, 1) return d == radius # Get only the points on the circle radius r from p. intersections = filter(on_circle, intersections) if len(intersections) == 2: p1,p2 = intersections midp = (p1[0] + p2[0])*.5, (p1[1] + p2[1])*.5 k = dist(p1, midp) def circle_points((cx,cy), radius): return ( (cx + radius, cy), (cx - radius, cy), (cx, cy + radius), (cx, cy - radius)) def find_closest(f1, f2): f1x, f1y = f1 f2x, f2y = f2 # The circle points k from f1. f1c = ((f1x + k, f1y), (f1x, f1y + k), (f1x - k, f1y), (f1x, f1y - k)) # The circle points k from f2. f2c = ((f2x + k, f2y), (f2x, f2y + k), (f2x - k, f2y), (f2x, f2y - k)) # Find the two points x such that f1x == f2x. near_points = set(filter( lambda x: dist(f1, x) == dist(f2, x), f1c + f2c)) return near_points def make_arms(ax,ay): step = .1 test_points = ((ax + step, ay), (ax, ay + step), (ax - step, ay), (ax, ay - step)) free_points = filter( lambda x: dist(p1, x) > dist(p1, (ax, ay)) and \ dist(p2, x) > dist(p2, (ax, ay)), test_points) if len(free_points) == 1: bx, by = free_points[0] if bx < ax: cx = 0 elif bx > ax: cx = 2000 elif bx == ax: cx = ax if by < ay: cy = 0 elif by > ay: cy = 2000 elif by == ay: cy = ay elif len(free_points) == 2: b1x, b1y = free_points[0] b2x, b2y = free_points[1] if min(b1x,b2x) < ax: cx = 0 elif max(b1x,b2x) > ax: cx = 2000 elif b1x == ax and b2x == ax: cx = ax if min(b1y,b2y) < ay: cy = 0 elif max(b1y,b2y) > ay: cy = 2000 elif b1y == ay and b2y == ay: cy = ay return cx, cy def make_midset(f1, f2): # Make the middle segment. np = find_closest(f1, f2) if len(np) == 2: np1, np2 = np np1x, np1y = np1 np2x, np2y = np2 c.coords(self.handle, np1x, np1y, np2x, np2y) cx, cy = make_arms(np1x, np1y) dx, dy = make_arms(np2x, np2y) c.coords(self.block1, np1x, np1y, cx, cy) c.coords(self.block2, np2x, np2y, dx, dy) # The midset. make_midset(p1, p2)
def _find_best_fit(self, puzzle): """ calculate the best fit for a given word. Prefer words with intersections over non intersecting spaces :return: position (x, y) in the puzzle_matrix """ word = puzzle['answer'] # if first word print(len(self.filled_pos)) if len(self.filled_pos) == 0: x = random.randint(0, 4) y = random.randint(0, 4) print("first_word: {} x:{} y:{}".format(word, x, y)) print("will_fit: {}".format(will_fit[ACROSS](x, y, length( word, self.lang)))) if will_fit[ACROSS](x, y, length(word, self.lang)): puzzle['orientation'] = "across" # puzzle['position'] = t + 1 puzzle['startx'] = x + 1 puzzle['starty'] = y + 1 self._fill_word_in_matrix(word, ACROSS, (x, y)) return puzzle # first find the location where it overlaps.. then move to the other ones to keep it interesting for key in self.filled_pos: #the orientation for this word should be perpendicular to the one we are trying to match pos = int(not self.filled_pos[key]['orientation']) # find the intersecting letters between the two words intersect = find_intersection(key, word, self.lang) print("trying to intersect filled_word={} with word={}".format( key, word)) if len(intersect) == 0: # no letters matched.. lets find the next continue else: a = [-10, -10] print("intersecting letters={}".format(intersect)) for letter in intersect: indexes1 = find_all_char_pos(key, letter, self.lang) for index in indexes1: # index = filled_pos[key]['word'].find(letter) print("location of the letter={} in word={} is {}". format(letter, key, index)) filled_word_pos = self.filled_pos[key]['position'] a[pos] = filled_word_pos[pos] + index indexes2 = find_all_char_pos(word, letter, self.lang) for index2 in indexes2: # index2 = word.find(letter) print("location of the letter={} in word={} is {}". format(letter, word, index2)) a[self.filled_pos[key] ['orientation']] = filled_word_pos[int( not pos)] - index2 print("looking for match in location={}".format(a)) print("will_fit={}".format(will_fit[pos]( a[0], a[1], length(word, self.lang)))) if will_fit[pos](a[0], a[1], length(word, self.lang)): if not self._check_overlap( word, pos, a[0], a[1]): self._fill_word_in_matrix( word, pos, (a[0], a[1])) calculate_free_rows( self.puzzle_matrix, self.height) puzzle[ 'orientation'] = "down" if pos else "across" # puzzle['position'] = t + 1 puzzle['startx'] = a[0] + 1 puzzle['starty'] = a[1] + 1 return puzzle # if we are still here then we havent found a place for this word # fill it in an empty space free_blocks_across = calculate_free_rows(self.puzzle_matrix, self.height) print("@@@@@@filling a random across free_blocks_across={}".format( free_blocks_across)) for key, val in sorted(free_blocks_across.items()): print("key={} val={}".format(key, val)) if key >= length(word, self.lang): pos = val.pop(random.randint(0, len(val) - 1)) if will_fit[ACROSS](pos[0], pos[1], length( word, self.lang)) and not self._check_overlap( word, ACROSS, pos[0], pos[1]): self._fill_word_in_matrix(word, ACROSS, (pos)) puzzle['orientation'] = "across" # puzzle['position'] = t + 1 puzzle['startx'] = pos[0] + 1 puzzle['starty'] = pos[1] + 1 return puzzle