wolfmanstout / screen-ocr

Easily perform OCR on portions of the screen, choosing from a selection of backends.
Apache License 2.0
37 stars 7 forks source link

Initial multi-monitor support #5

Closed LexiconCode closed 1 year ago

LexiconCode commented 3 years ago

This PR utilizes mss to screenshot screens. This should add a small speed boost compared to ImageGrab.grab for capturing the screenshot. mss is cross-platform but limited to Python 3.5 and above.

There's a few the reasons why I consider this draft.

def run_ocr(radius, start_coordinates):
    import timeit
    import screen_ocr

    ocr_reader = screen_ocr.Reader(backend="winrt").create_quality_reader(radius=radius)
    start_time = timeit.default_timer()
    screencontents = ocr_reader.read_nearby(start_coordinates, monitor=2)
    end_time = ("{0:.3f}".format(timeit.default_timer() - start_time))
    print("radius-{}_time-{}".format(radius, end_time)) # Debug

print(run_ocr(radius=3000, start_coordinates=(0,0)))
Traceback (most recent call last):
  File "c:\Users\Main\CodeShare\Laptop\ocr_scaling_test\tempCodeRunnerFile.python", line 11, in <module>
    print(run_ocr(radius=3000, start_coordinates=(0,0)))
  File "c:\Users\Main\CodeShare\Laptop\ocr_scaling_test\tempCodeRunnerFile.python", line 7, in run_ocr
    screencontents = ocr_reader.read_nearby(start_coordinates, monitor=2)
  File "d:\backup\library\documents\screen-ocr\screen_ocr\_screen_ocr.py", line 125, in read_nearby
    return self.read_image(screenshot,
  File "d:\backup\library\documents\screen-ocr\screen_ocr\_screen_ocr.py", line 132, in read_image
    result = self._backend.run_ocr(preprocessed_image)
  File "d:\backup\library\documents\screen-ocr\screen_ocr\_winrt.py", line 15, in run_ocr
    return self._executor.submit(lambda: self._run_ocr_sync(image)).result()
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\_base.py", line 439, in result
    return self.__get_result()
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\_base.py", line 388, in __get_result
    raise self._exception
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "d:\backup\library\documents\screen-ocr\screen_ocr\_winrt.py", line 15, in <lambda>
    return self._executor.submit(lambda: self._run_ocr_sync(image)).result()
  File "d:\backup\library\documents\screen-ocr\screen_ocr\_winrt.py", line 47, in _run_ocr_sync
    return asyncio.run(self._run_ocr_async(image))
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\Main\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "d:\backup\library\documents\screen-ocr\screen_ocr\_winrt.py", line 26, in run_ocr_async
    bytes_list = list(bytes)
MemoryError