Skip to content

Commit

Permalink
snq bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arcb01 committed Jan 14, 2024
1 parent 45c97a9 commit 586bbdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
7 changes: 4 additions & 3 deletions gnarrator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ def check_events(self):
if event.event_type == keyboard.KEY_DOWN and event.name == self.SMALL_N_QUICK:
window = Window(settings=self.settings, mode="snq")
self.content = self.reading_engine.read_screen(mode="snq", window=window)
self.content.show()
self.reading_engine.say_content_immediatly()
self.app.exec_()
if self.reading_engine.detectionsFound():
self.content.show()
self.reading_engine.say_content_immediatly()
self.app.exec_()

def run(self):
"""
Expand Down
13 changes: 8 additions & 5 deletions gnarrator/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,20 @@ def get_detections(self):
def find_closest_detection(self, mouse_position):
"""
Given a mouse position, this function returns the nearest detection
# NOTE: List of detections is updated
:param mouse_pos: (x,y) coordinate of the mouse position
:return # NOTE: Updates the list of detections
:return: text of the nearest detection
"""

# NOTE: Map mouse position to local coordinates
mouse_position = (mouse_position[0] - self.region[0], mouse_position[1] - self.region[1])

closest_element = min(self.detections, key=lambda element: min(np.linalg.norm(np.array(point) - np.array(mouse_position))
for point in element[0]))

self.detections = [closest_element]
if self.detections:
closest_element = min(self.detections, key=lambda element: min(np.linalg.norm(np.array(point) - np.array(mouse_position))
for point in element[0]))
self.detections = [closest_element]
text = closest_element[1]
return text

def empty_detections(self):
self.detections = []
24 changes: 12 additions & 12 deletions gnarrator/reading_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ def read_regional_screen(self, screen_region):
# Read textual elements
self.OCR.read()

def detectionsFound(self):
return True if len(self.OCR.get_detections) > 0 else False

def read_screen(self, mode, window, screen_region=None):
"""
Read the screen content and create buttons for each detection
:param window: Window where the buttons will be displayed
:param mode: mode of reading (full, regional, small_n_quick)
:param screen_region: (x,y,w,h) coordinates of the region to be captured
if None, the whole screen will be captured
:return: Window with the detections
"""

self.window = window
Expand All @@ -92,7 +96,7 @@ def read_screen(self, mode, window, screen_region=None):

# Get screenshot results (bounding boxes)
# Create a button for each bounding box
if len(self.OCR.get_detections) > 0:
if self.detectionsFound():
for det in self.OCR.get_detections:
det_text_content = det[1]
det_coords = get_detection_coords(det) # x, y, w, h
Expand All @@ -104,16 +108,14 @@ def read_screen(self, mode, window, screen_region=None):
# Give buttons style
self.window.style_buttons()

# Launch window
if self.screen_region:
# NOTE: This can be changed to be all screen if needed (using map_coordinates function)
self.window.set_to_regional(screen_region=self.screen_region)
self.window.reset_opacity()
# NOTE: This can be changed to be all screen if needed (using map_coordinates function)
self.window.set_to_regional(screen_region=self.screen_region)
self.window.reset_opacity()

# if only 1 detection was found, read it directly
# TODO: Apply reading stylesheet to the button
# NOTE: for snq mode can't be done here
if len(self.OCR.get_detections) == 1 and mode == "regional":
# TODO: Apply reading stylesheet to the button
# NOTE: for snq mode can't be done here
self.say_content_immediatly(det_text_content)

return self.window
Expand All @@ -134,10 +136,8 @@ def read_snq_screen(self):
self.OCR.take_screenshot(screen_region=screen_region)
self.OCR.read()
# 3. Find the nearest detection
self.OCR.find_closest_detection((xmouse, ymouse))
self.det_text_content = self.OCR.get_detections[0][1]
# 4. Draw the button...
return screen_region
nearest_det_txt = self.OCR.find_closest_detection((xmouse, ymouse))
self.det_text_content = nearest_det_txt

def say_content_immediatly(self, content=None):
if not content:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setuptools.setup(
name='gnarrator',
version='1.1.263',
version='1.1.274',
python_requires='>=3.9.0',
author='Arnau Castelalno',
author_email='arcascb2001@gmail.com',
Expand Down

0 comments on commit 586bbdc

Please sign in to comment.