Skip to content

Commit

Permalink
resultHandler class and method added
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamJain7 committed Jul 22, 2020
1 parent 7a0c497 commit 7436413
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
26 changes: 23 additions & 3 deletions addon/globalPlugins/objectDetection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@
import scriptHandler
import vision
from visionEnhancementProviders.screenCurtain import ScreenCurtainSettings
from contentRecog.recogUi import RecogResultNVDAObject

from ._doObjectDetection import *
from ._resultUI import recognizeNavigatorObject

from ._resultUI import recognizeNavigatorObject

def isScreenCurtainEnabled():
return any([x.providerId == ScreenCurtainSettings.getId() for x in vision.handler.getActiveProviderInfos()])


class PresentResults():
def __init__(self, result):
self.result = result

def speakResult(self):
ui.message(self.result)

def createVirtualWindow(self):
result = contentRecog.SimpleTextResult(self.result)
resObj = RecogResultNVDAObject(result=result)
# This method queues an event to the main thread.
resObj.setFocus()

def speakResultAndCreateVirtualWindow(self):
ui.message(self.result)
result = contentRecog.SimpleTextResult(self.result)
resObj = RecogResultNVDAObject(result=result)
# This method queues an event to the main thread.
resObj.setFocus()

class GlobalPlugin(globalPluginHandler.GlobalPlugin):

@script(
Expand All @@ -28,7 +48,7 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
def script_detectObjectsTinyYOLOv3(self, gesture):
scriptCount = scriptHandler.getLastScriptRepeatCount()
if not isScreenCurtainEnabled():
x = doDetectionTinyYOLOv3()
x = doDetectionTinyYOLOv3(PresentResults)
# `Alt+NVDA+D` -> filter non-graphic elements
if scriptCount==0:
recognizeNavigatorObject(x, True)
Expand Down
16 changes: 14 additions & 2 deletions addon/globalPlugins/objectDetection/_doObjectDetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os
import threading
import tempfile
from typing import Any

import wx
import ui
import contentRecog
Expand All @@ -17,6 +19,9 @@
_sizeThreshold = 128

class doObjectDetection(contentRecog.ContentRecognizer):
def __init__(self, resultHandlerClass):
self.resultHandlerClass = resultHandlerClass

def recognize(self, pixels, imgInfo, onResult):
bmp = wx.EmptyBitmap(imgInfo.recogWidth, imgInfo.recogHeight, 32)
bmp.CopyFromBuffer(pixels, wx.BitmapBufferFormat_RGB32)
Expand All @@ -35,8 +40,6 @@ def _bgRecog(self):
finally:
os.remove(self._imagePath)
if self._onResult:
ui.message(result)
result = contentRecog.SimpleTextResult(result)
self._onResult(result)

def cancel(self):
Expand All @@ -51,8 +54,17 @@ def validateObject(self, nav):
def validateBounds(self, left, top, width, height):
return True

def createResultHandler(self, result: Any):
handler = self.resultHandlerClass(result)
handler.createVirtualWindow()
return handler


class doDetectionTinyYOLOv3(doObjectDetection):

def __init__(self, resultHandlerClass):
super(doDetectionTinyYOLOv3, self).__init__(resultHandlerClass)

def detect(self, imagePath):
result = YOLOv3Detection(imagePath, tiny=True).getSentence()
return result
Expand Down
15 changes: 9 additions & 6 deletions addon/globalPlugins/objectDetection/_resultUI.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from typing import Optional

import api
import ui
import screenBitmap
from logHandler import log
import queueHandler
from contentRecog.recogUi import RecogResultNVDAObject, RecogImageInfo
from controlTypes import ROLE_GRAPHIC
from contentRecog import ContentRecognizer, RecogImageInfo
from contentRecog.recogUi import RecogResultNVDAObject

#: Keeps track of the recognition in progress, if any.
_activeRecog = None
_activeRecog: Optional[ContentRecognizer] = None

def recognizeNavigatorObject(recognizer, filterNonGraphic=True):
"""User interface function to recognize content in the navigator object.
Expand Down Expand Up @@ -47,10 +49,12 @@ def recognizeNavigatorObject(recognizer, filterNonGraphic=True):
sb = screenBitmap.ScreenBitmap(imgInfo.recogWidth, imgInfo.recogHeight)
pixels = sb.captureImage(left, top, width, height)
_activeRecog = recognizer

recognizer.recognize(pixels, imgInfo, _recogOnResult)

def _recogOnResult(result):
global _activeRecog
recognizer: ContentRecognizer = _activeRecog
_activeRecog = None
# This might get called from a background thread, so any UI calls must be queued to the main thread.
if isinstance(result, Exception):
Expand All @@ -59,6 +63,5 @@ def _recogOnResult(result):
queueHandler.queueFunction(queueHandler.eventQueue,
ui.message, _("Recognition failed"))
return
resObj = RecogResultNVDAObject(result=result)
# This method queues an event to the main thread.
resObj.setFocus()
if recognizer:
handler = recognizer.createResultHandler(result)

0 comments on commit 7436413

Please sign in to comment.