Skip to content

Commit

Permalink
Fix some bugs when testing under Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
Breakthrough committed Oct 4, 2023
1 parent 5d63dc3 commit 50c352d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
17 changes: 9 additions & 8 deletions dvr_scan/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,18 +500,19 @@ def _select_roi(self) -> bool:
self._logger.info("Selecting area of interest:")
# TODO: We should process this frame.
frame_for_crop = self._input.read()
scale_factor = None
scale_factor = 1
screen_bounds = get_min_screen_bounds()
if not screen_bounds is None:
max_h = screen_bounds[0] if self._max_window_size[0] == 0 else self._max_window_size[0]
max_w = screen_bounds[1] if self._max_window_size[1] == 0 else self._max_window_size[1]
current_rois = []
if max_h > 0 or max_w > 0:
max_h = screen_bounds[0] if self._max_window_size[
0] == 0 else self._max_window_size[0]
max_w = screen_bounds[1] if self._max_window_size[
1] == 0 else self._max_window_size[1]
frame_h, frame_w = (frame_for_crop.shape[0], frame_for_crop.shape[1])
if (max_h > 0 and frame_h > max_h) or (max_w > 0 and frame_w > max_w):
logger.debug('Max window size: %d x %d', max_w, max_h)
frame_h, frame_w = (frame_for_crop.shape[0], frame_for_crop.shape[1])
# Downscale the image if it's too large for the screen.
factor_h = frame_h / float(max_h) if max_h > 0 and frame_h > max_h else 0
factor_w = frame_w / float(max_w) if max_w > 0 and frame_w > max_w else 0
factor_h = frame_h / float(max_h) if max_h > 0 and frame_h > max_h else 1
factor_w = frame_w / float(max_w) if max_w > 0 and frame_w > max_w else 1
scale_factor = round(max(factor_h, factor_w))
roi_list = SelectionWindow(frame_for_crop, scale_factor).run()
logging.info(str(roi_list))
Expand Down
42 changes: 23 additions & 19 deletions dvr_scan/selection_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

MAX_HISTORY_SIZE = 1024

MAX_DOWNSCALE_FACTOR = 1024

MAX_UPDATE_RATE_NORMAL = 20
MAX_UPDATE_RATE_DRAGGING = 5
Expand Down Expand Up @@ -114,7 +115,7 @@ def _rescale(self):
self._frame = self._original_frame.copy()
self._frame_size = Size(w=self._frame.shape[1], h=self._frame.shape[0])
self._redraw = True
logger.debug("Resizing window: scale = 1/%d%s, resolution = %d x %d", self._scale,
logger.debug("Resizing: scale = 1/%d%s, resolution = %d x %d", self._scale,
' (off)' if self._scale == 1 else '', self._frame_size.w, self._frame_size.h)

def _undo(self):
Expand All @@ -123,15 +124,15 @@ def _undo(self):
self._point_list = deepcopy(self._history[self._history_pos])
self._recalculate = True
self._redraw = True
logger.debug("Undo Applied[%d/%d]", self._history_pos, len(self._history))
logger.debug("Undo: [%d/%d]", self._history_pos, len(self._history))

def _redo(self):
if self._history_pos > 0:
self._history_pos -= 1
self._point_list = deepcopy(self._history[self._history_pos])
self._recalculate = True
self._redraw = True
logger.debug("Redo Applied [%d/%d]", self._history_pos, len(self._history))
logger.debug("Redo: [%d/%d]", self._history_pos, len(self._history))

def _commit(self):
self._history = self._history[self._history_pos:]
Expand All @@ -140,11 +141,13 @@ def _commit(self):
self._history_pos = 0
self._recalculate = True
self._redraw = True
logger.debug("Commit: size = %d, data = [%s]", len(self._history),
logger.debug("Commit: [%d] = %s",
len(self._history) - 1,
', '.join(f'P({x},{y})' for x, y in [point for point in self._point_list]))

def _emit_points(self):
logger.info("ROI:\n--roi %s", " ".join("%d %d" % (point.x, point.y) for point in self._point_list))
logger.info("ROI:\n--roi %s",
" ".join("%d %d" % (point.x, point.y) for point in self._point_list))

def _draw(self):
# TODO: Can cache a lot of the calculations below. Need to keep drawing as fast as possible.
Expand Down Expand Up @@ -245,12 +248,15 @@ def _hovering_over(self) -> ty.Optional[int]:
min_i = i
# If we've shrunk the image, we need to compensate for the size difference in the image.
# The control handles remain the same size but the image is smaller
return min_i if self._mouse_distances[min_i] <= (POINT_CONTROL_RADIUS * self._scale)**2 else None
return min_i if self._mouse_distances[min_i] <= (POINT_CONTROL_RADIUS *
self._scale)**2 else None

def run(self):
logger.debug('Creating window for frame (scale = %d)', self._scale)
cv2.imshow(WINDOW_NAME, self._frame)
cv2.setMouseCallback(WINDOW_NAME, self._handle_mouse_input)
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow(WINDOW_NAME, width=self._frame_size.w, height=self._frame_size.h)
cv2.imshow(WINDOW_NAME, mat=self._frame)
cv2.setMouseCallback(WINDOW_NAME, on_mouse=self._handle_mouse_input)
while True:
self._draw()
key = cv2.waitKey(
Expand All @@ -265,11 +271,13 @@ def run(self):
self._redraw = True
logger.debug("Antialiasing: %s", 'ON' if self._use_aa else 'OFF')
elif key == ord('w'):
self._scale += 1
self._rescale()
if self._scale < MAX_DOWNSCALE_FACTOR:
self._scale += 1
self._rescale()
elif key == ord('s'):
self._scale = max(1, self._scale - 1)
self._rescale()
if self._scale > 1:
self._scale = max(1, self._scale - 1)
self._rescale()
elif key == ord('z'):
self._undo()
elif key == ord('y'):
Expand Down Expand Up @@ -303,13 +311,9 @@ def _recalculate_data(self):
self._recalculate = False

def _handle_mouse_input(self, event, x, y, flags, param):
# TODO: Handle case where mouse leaves frame (stop highlighting).
self._curr_mouse_pos = bound_point(point=Point(x, y), size=self._frame_size)
self._curr_mouse_pos = Point(self._curr_mouse_pos.x * self._scale,
self._curr_mouse_pos.y * self._scale)
drag_started = False
# scale coordinates

bounded = bound_point(point=Point(x, y), size=self._frame_size)
self._curr_mouse_pos = Point(bounded.x * self._scale, bounded.y * self._scale)
if event == cv2.EVENT_LBUTTONDOWN:
if not self._hover_point is None:
self._dragging = True
Expand Down

0 comments on commit 50c352d

Please sign in to comment.