def mouse_callback(event, x, y, flags, params): nonlocal tracked_box, tracked_point orig, trackers, bboxes, classes = params im = orig.copy() # Determine which bbox is corresponded to by the click click = np.array([x, y]) / WINDOW_SCALE radius = 10 # If there is no tracked point, determine which point gets clicked, if # any, and set variables accordingly. if tracked_point is None and event == cv2.EVENT_LBUTTONDOWN: for i, bbox in enumerate(bboxes): top_left = bbox[:2] top_right = top_left + [bbox[2], 0] bottom_left = top_left + [0, bbox[3]] bottom_right = top_left + bbox[2:] found = False for j, p in enumerate( [top_left, top_right, bottom_left, bottom_right]): if np.linalg.norm(p - click) < radius: tracked_point = POSITIONS[j] tracked_box = i modified.add(tracked_box) found = True if found: break elif tracked_point is not None and event == cv2.EVENT_LBUTTONDOWN: tracked_point = None elif tracked_point is not None: # There must be a tracked point, so move the point to the location # of the mouse click. p0 = bboxes[tracked_box][:2] p1 = p0 + bboxes[tracked_box][2:] if tracked_point == TOP_LEFT: p0 = click elif tracked_point == TOP_RIGHT: p0[1] = click[1] p1[0] = click[0] elif tracked_point == BOTTOM_LEFT: p0[0] = click[0] p1[1] = click[1] elif tracked_point == BOTTOM_RIGHT: p1 = click bboxes[tracked_box][:2] = p0 bboxes[tracked_box][2:] = p1 - p0 drawing_utils.draw_bboxes(im, bboxes, classes, args.scale) drawing_utils.draw_dots(im, bboxes) show_scaled(window, im)
def mouse_callback(event, x, y, flags, params): nonlocal tracked_point, is_top_left orig, trackers, bboxes, classes = params im = orig.copy() # Determine which bbox is corresponded to by the click click = np.array([x, y]) / WINDOW_SCALE radius = 10 # If there is no tracked point, determine which point gets clicked, if # any, and set variables accordingly. if tracked_point is None and event == cv2.EVENT_LBUTTONDOWN: for i, bbox in enumerate(bboxes): p0 = bbox[:2] p1 = p0 + bbox[2:] if np.linalg.norm(p0 - click) < radius: tracked_point = i modified.add(tracked_point) is_top_left = True break elif np.linalg.norm(p1 - click) < radius: tracked_point = i modified.add(tracked_point) is_top_left = False break elif tracked_point is not None and event == cv2.EVENT_LBUTTONDOWN: tracked_point = None elif tracked_point is not None: # There must be a tracked point, so move the point to the location # of the mouse click. p0 = bboxes[tracked_point][:2] p1 = p0 + bboxes[tracked_point][2:] if is_top_left: # No fancy handling necessary bboxes[tracked_point][:2] = click bboxes[tracked_point][2:] = p1 - click else: bboxes[tracked_point][2:] = click - bboxes[tracked_point][:2] drawing_utils.draw_bboxes(im, bboxes, classes, args.scale) drawing_utils.draw_dots(im, bboxes) show_scaled(window, im)
def correction_mode(orig, trackers, bboxes, classes, annotated_classes): frame = orig.copy() drawing_utils.draw_bboxes(frame, bboxes, annotated_classes, args.scale) drawing_utils.draw_dots(frame, bboxes) show_scaled(window, frame) modified = set() tracked_point = None is_top_left = False def mouse_callback(event, x, y, flags, params): nonlocal tracked_point, is_top_left orig, trackers, bboxes, classes = params im = orig.copy() # Determine which bbox is corresponded to by the click click = np.array([x, y]) / WINDOW_SCALE radius = 10 # If there is no tracked point, determine which point gets clicked, if # any, and set variables accordingly. if tracked_point is None and event == cv2.EVENT_LBUTTONDOWN: for i, bbox in enumerate(bboxes): p0 = bbox[:2] p1 = p0 + bbox[2:] if np.linalg.norm(p0 - click) < radius: tracked_point = i modified.add(tracked_point) is_top_left = True break elif np.linalg.norm(p1 - click) < radius: tracked_point = i modified.add(tracked_point) is_top_left = False break elif tracked_point is not None and event == cv2.EVENT_LBUTTONDOWN: tracked_point = None elif tracked_point is not None: # There must be a tracked point, so move the point to the location # of the mouse click. p0 = bboxes[tracked_point][:2] p1 = p0 + bboxes[tracked_point][2:] if is_top_left: # No fancy handling necessary bboxes[tracked_point][:2] = click bboxes[tracked_point][2:] = p1 - click else: bboxes[tracked_point][2:] = click - bboxes[tracked_point][:2] drawing_utils.draw_bboxes(im, bboxes, classes, args.scale) drawing_utils.draw_dots(im, bboxes) show_scaled(window, im) cv2.setMouseCallback(window, mouse_callback, param=(orig, trackers, bboxes, annotated_classes)) while True: key = cv2.waitKey(1) & 0xFF if key == ord('c') or key == ord(' '): for mod in modified: print("Reinitializing tracker %d" % mod) new_tracker = init_trackers(args.tracker, orig, [bboxes[mod]]) trackers[mod] = new_tracker[0] break elif key == ord('r'): refine_bboxes(bboxes, classes, orig, trackers) # Clear the mouse callback cv2.setMouseCallback(window, lambda *args: None)
def correction_mode(orig, trackers, bboxes, classes, annotated_classes): frame = orig.copy() drawing_utils.draw_bboxes(frame, bboxes, annotated_classes, args.scale) drawing_utils.draw_dots(frame, bboxes) show_scaled(window, frame) modified = set() tracked_box = None tracked_point = None TOP_LEFT = 0 TOP_RIGHT = 1 BOTTOM_LEFT = 2 BOTTOM_RIGHT = 3 POSITIONS = [TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT] def mouse_callback(event, x, y, flags, params): nonlocal tracked_box, tracked_point orig, trackers, bboxes, classes = params im = orig.copy() # Determine which bbox is corresponded to by the click click = np.array([x, y]) / WINDOW_SCALE radius = 10 # If there is no tracked point, determine which point gets clicked, if # any, and set variables accordingly. if tracked_point is None and event == cv2.EVENT_LBUTTONDOWN: for i, bbox in enumerate(bboxes): top_left = bbox[:2] top_right = top_left + [bbox[2], 0] bottom_left = top_left + [0, bbox[3]] bottom_right = top_left + bbox[2:] found = False for j, p in enumerate( [top_left, top_right, bottom_left, bottom_right]): if np.linalg.norm(p - click) < radius: tracked_point = POSITIONS[j] tracked_box = i modified.add(tracked_box) found = True if found: break elif tracked_point is not None and event == cv2.EVENT_LBUTTONDOWN: tracked_point = None elif tracked_point is not None: # There must be a tracked point, so move the point to the location # of the mouse click. p0 = bboxes[tracked_box][:2] p1 = p0 + bboxes[tracked_box][2:] if tracked_point == TOP_LEFT: p0 = click elif tracked_point == TOP_RIGHT: p0[1] = click[1] p1[0] = click[0] elif tracked_point == BOTTOM_LEFT: p0[0] = click[0] p1[1] = click[1] elif tracked_point == BOTTOM_RIGHT: p1 = click bboxes[tracked_box][:2] = p0 bboxes[tracked_box][2:] = p1 - p0 drawing_utils.draw_bboxes(im, bboxes, classes, args.scale) drawing_utils.draw_dots(im, bboxes) show_scaled(window, im) cv2.setMouseCallback(window, mouse_callback, param=(orig, trackers, bboxes, annotated_classes)) while True: key = cv2.waitKey(1) & 0xFF if key == ord('c') or key == ord(' '): for mod in modified: print("Reinitializing tracker %d" % mod) new_tracker = init_trackers(args.tracker, orig, [bboxes[mod]]) trackers[mod] = new_tracker[0] break elif key == ord('r'): refine_bboxes(bboxes, classes, orig, trackers) # Clear the mouse callback cv2.setMouseCallback(window, lambda *args: None)