Skip to content

Commit

Permalink
Fix circular imports, mak OCR as clearly experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Jun 16, 2024
1 parent 2f03a90 commit 797492f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
8 changes: 4 additions & 4 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ You can have one (and only one) image with the keyword `reset` in its name. Auto

The Start Image is similar to the Reset Image. You can only have one Start Image with the keyword `start_auto_splitter`.You can reload the image using the "`Reload Start Image`" button. The pause time is the amount of seconds AutoSplit will wait before starting comparisons of the first split image. Delay times will be used to delay starting your timer after the threshold is met.

### Text Recognition (OCR)
### Text Recognition / Optical Character Recognition (OCR) ⚠️EXPERIMENTAL⚠️

You can use text recognition as an alternative comparison method.

Expand All @@ -189,7 +189,7 @@ First you need to install tesseract and include it in your system or user enviro

#### Usage

To use this feature you need to place a text file (.txt) in your splits folder instead of an image file.
To use this feature you need to place a text file (`.txt`) in your splits folder instead of an image file.

An example file name and content could look like this:

Expand Down Expand Up @@ -229,7 +229,7 @@ If you're used to working in corner coordinates, you can think of `top_left = [l
Currently there are two comparison methods:

- `0` - uses the Levenshtein distance (the default)
- `1` - checks if the OCR text contains the searched text (`0.0` or `1.0`)
- `1` - checks if the OCR text contains the searched text (results in matches of either `0.0` or `1.0`)

If you only want a perfect full match, use "Levenshtein" with a threshold of `(1.0)` on your file name.

Expand All @@ -241,7 +241,7 @@ methods = [1, 0]

The methods are then checked in the order you defined and the best match upon them wins.

Note: This method can cause high CPU usage at the standard comparison FPS. You should therefor limit the comparison FPS when you use this method to 1 or 2 FPS using the `fps_limit` option.
Note: This method can cause high CPU usage at the standard comparison FPS. You should therefor limit the comparison FPS when you use this method to 1 or 2 FPS using the `fps_limit` option.
The size of the selected rectangle can also impact the CPU load (bigger = more CPU load).

### Profiles
Expand Down
3 changes: 1 addition & 2 deletions src/capture_method/VideoCaptureDeviceCaptureMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from cv2.typing import MatLike
from typing_extensions import override

from capture_method import get_input_device_resolution
from capture_method.CaptureMethodBase import CaptureMethodBase
from error_messages import CREATE_NEW_ISSUE_MESSAGE, exception_traceback
from utils import ImageShape, is_valid_image
from utils import ImageShape, get_input_device_resolution, is_valid_image

if TYPE_CHECKING:
from AutoSplit import AutoSplit
Expand Down
27 changes: 2 additions & 25 deletions src/capture_method/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

from capture_method.CaptureMethodBase import CaptureMethodBase
from capture_method.VideoCaptureDeviceCaptureMethod import VideoCaptureDeviceCaptureMethod
from utils import WGC_MIN_BUILD, WINDOWS_BUILD_NUMBER, first, try_get_direct3d_device
from utils import WGC_MIN_BUILD, WINDOWS_BUILD_NUMBER, first, get_input_device_resolution, try_get_direct3d_device

if sys.platform == "win32":
from _ctypes import COMError # noqa: PLC2701
from _ctypes import COMError # noqa: PLC2701 # comtypes is untyped

from pygrabber.dshow_graph import FilterGraph

Expand Down Expand Up @@ -205,29 +205,6 @@ def get_input_devices():
return cameras


def get_input_device_resolution(index: int) -> tuple[int, int] | None:
if sys.platform != "win32":
return (0, 0)
filter_graph = FilterGraph()
try:
filter_graph.add_video_input_device(index)
# This can happen with virtual cameras throwing errors.
# For example since OBS 29.1 updated FFMPEG breaking VirtualCam 3.0
# https://github.com/Toufool/AutoSplit/issues/238
except COMError:
return None

try:
resolution = filter_graph.get_input_device().get_current_format()
# For unknown reasons, some devices can raise "ValueError: NULL pointer access".
# For instance, Oh_DeeR's AVerMedia HD Capture C985 Bus 12
except ValueError:
return None
finally:
filter_graph.remove_filters()
return resolution


def get_all_video_capture_devices():
named_video_inputs = get_input_devices()

Expand Down
27 changes: 27 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
if sys.platform == "win32":
import ctypes
import ctypes.wintypes
from _ctypes import COMError # noqa: PLC2701 # comtypes is untyped

import win32gui
import win32ui
from pygrabber.dshow_graph import FilterGraph
from winsdk.windows.ai.machinelearning import LearningModelDevice, LearningModelDeviceKind
from winsdk.windows.media.capture import MediaCapture

STARTUPINFO: TypeAlias = subprocess.STARTUPINFO
else:
STARTUPINFO: TypeAlias = None
Expand Down Expand Up @@ -148,6 +151,30 @@ def get_window_bounds(hwnd: int) -> tuple[int, int, int, int]:
return window_left_bounds, window_top_bounds, window_width, window_height


# Note: maybe reorganize capture_method module to have different helper modules and a methods submodule
def get_input_device_resolution(index: int) -> tuple[int, int] | None:
if sys.platform != "win32":
return (0, 0)
filter_graph = FilterGraph()
try:
filter_graph.add_video_input_device(index)
# This can happen with virtual cameras throwing errors.
# For example since OBS 29.1 updated FFMPEG breaking VirtualCam 3.0
# https://github.com/Toufool/AutoSplit/issues/238
except COMError:
return None

try:
resolution = filter_graph.get_input_device().get_current_format()
# For unknown reasons, some devices can raise "ValueError: NULL pointer access".
# For instance, Oh_DeeR's AVerMedia HD Capture C985 Bus 12
except ValueError:
return None
finally:
filter_graph.remove_filters()
return resolution


def open_file(file_path: str | bytes | os.PathLike[str] | os.PathLike[bytes]):
if sys.platform == "win32":
os.startfile(file_path) # noqa: S606
Expand Down

0 comments on commit 797492f

Please sign in to comment.