Skip to content

Commit c8596fe

Browse files
committed
Don't send is_old_image boolean from get_frame
1 parent 03c36da commit c8596fe

7 files changed

+41
-34
lines changed

src/AutoSplit.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def __update_live_image_details(self, capture: MatLike | None, called_from_timer
252252
if called_from_timer:
253253
if self.is_running or self.start_image:
254254
return
255-
capture, _ = self.capture_method.get_frame()
255+
capture = self.capture_method.get_frame()
256256

257257
# Update title from target window or Capture Device name
258258
capture_region_window_label = (
@@ -310,7 +310,7 @@ def __start_image_function(self):
310310
self.start_image_status_value_label.setText("ready")
311311
self.__update_split_image(self.start_image)
312312

313-
capture, _ = self.__get_capture_for_comparison()
313+
capture = self.__get_capture_for_comparison()
314314
start_image_threshold = self.start_image.get_similarity_threshold(self)
315315
start_image_similarity = self.start_image.compare_with_capture(self, capture)
316316

@@ -390,7 +390,7 @@ def __take_screenshot(self):
390390
screenshot_index += 1
391391

392392
# Grab screenshot of capture region
393-
capture, _ = self.capture_method.get_frame()
393+
capture = self.capture_method.get_frame()
394394
if not is_valid_image(capture):
395395
error_messages.region()
396396
return
@@ -415,14 +415,16 @@ def __check_fps(self):
415415

416416
# run X iterations of screenshotting capture region + comparison + displaying.
417417
t0 = time()
418+
last_capture: MatLike | None = None
418419
for image in images:
419420
count = 0
420421
while count < CHECK_FPS_ITERATIONS:
421-
capture, is_old_image = self.__get_capture_for_comparison()
422-
_ = image.compare_with_capture(self, capture)
423-
# TODO: If is_old_image=true is always returned, this becomes an infinite loop
424-
if not is_old_image:
422+
new_capture = self.__get_capture_for_comparison()
423+
_ = image.compare_with_capture(self, new_capture)
424+
# TODO: If an old image is always returned, this becomes an infinite loop
425+
if new_capture is not last_capture:
425426
count += 1
427+
last_capture = new_capture
426428

427429
# calculate FPS
428430
t1 = time()
@@ -641,7 +643,7 @@ def __similarity_threshold_loop(self, number_of_split_images: int, dummy_splits_
641643

642644
start = time()
643645
while True:
644-
capture, _ = self.__get_capture_for_comparison()
646+
capture = self.__get_capture_for_comparison()
645647

646648
if self.__reset_if_should(capture):
647649
return True
@@ -710,7 +712,7 @@ def __pause_loop(self, stop_time: float, message: str):
710712
pause_split_image_number = self.split_image_number
711713
while True:
712714
# Calculate similarity for Reset Image
713-
if self.__reset_if_should(self.__get_capture_for_comparison()[0]):
715+
if self.__reset_if_should(self.__get_capture_for_comparison()):
714716
return True
715717

716718
time_delta = time() - start_time
@@ -785,7 +787,7 @@ def gui_changes_on_reset(self, safe_to_reload_start_image: bool = False):
785787

786788
def __get_capture_for_comparison(self):
787789
"""Grab capture region and resize for comparison."""
788-
capture, is_old_image = self.capture_method.get_frame()
790+
capture = self.capture_method.get_frame()
789791

790792
# This most likely means we lost capture
791793
# (ie the captured window was closed, crashed, lost capture device, etc.)
@@ -800,10 +802,10 @@ def __get_capture_for_comparison(self):
800802
self.live_image.setText(message)
801803
recovered = self.capture_method.recover_window(self.settings_dict["captured_window_title"])
802804
if recovered:
803-
capture, _ = self.capture_method.get_frame()
805+
capture = self.capture_method.get_frame()
804806

805807
self.__update_live_image_details(capture)
806-
return capture, is_old_image
808+
return capture
807809

808810
def __reset_if_should(self, capture: MatLike | None):
809811
"""Checks if we should reset, resets if it's the case, and returns the result."""

src/capture_method/BitBltCaptureMethod.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class BitBltCaptureMethod(CaptureMethodBase):
3636
_render_full_content = False
3737

3838
@override
39-
def get_frame(self) -> tuple[MatLike | None, bool]:
39+
def get_frame(self) -> MatLike | None:
4040
selection = self._autosplit_ref.settings_dict["capture_region"]
4141
hwnd = self._autosplit_ref.hwnd
4242
image: MatLike | None = None
4343

4444
if not self.check_selected_region_exists():
45-
return None, False
45+
return None
4646

4747
# If the window closes while it's being manipulated, it could cause a crash
4848
try:
@@ -70,7 +70,7 @@ def get_frame(self) -> tuple[MatLike | None, bool]:
7070
image = np.frombuffer(bitmap.GetBitmapBits(True), dtype=np.uint8)
7171
except (win32ui.error, pywintypes.error):
7272
# Invalid handle or the window was closed while it was being manipulated
73-
return None, False
73+
return None
7474

7575
if is_blank(image):
7676
image = None
@@ -82,7 +82,7 @@ def get_frame(self) -> tuple[MatLike | None, bool]:
8282
try_delete_dc(compatible_dc)
8383
win32gui.ReleaseDC(hwnd, window_dc)
8484
win32gui.DeleteObject(bitmap.GetHandle())
85-
return image, False
85+
return image
8686

8787
@override
8888
def recover_window(self, captured_window_title: str):

src/capture_method/CaptureMethodBase.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def close(self):
2727
# Some capture methods don't need an initialization process
2828
pass
2929

30-
def get_frame(self) -> tuple[MatLike | None, bool]: # noqa: PLR6301
30+
def get_frame(self) -> MatLike | None: # noqa: PLR6301
3131
"""
3232
Captures an image of the region for a window matching the given
3333
parameters of the bounding box.
3434
3535
@return: The image of the region in the window in BGRA format
3636
"""
37-
return None, False
37+
return None
3838

3939
def recover_window(self, captured_window_title: str) -> bool: # noqa: PLR6301
4040
return False

src/capture_method/DesktopDuplicationCaptureMethod.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_frame(self):
3939
hwnd = self._autosplit_ref.hwnd
4040
hmonitor = ctypes.windll.user32.MonitorFromWindow(hwnd, win32con.MONITOR_DEFAULTTONEAREST)
4141
if not hmonitor or not self.check_selected_region_exists():
42-
return None, False
42+
return None
4343

4444
left_bounds, top_bounds, *_ = get_window_bounds(hwnd)
4545
self.desktop_duplication.display = next(
@@ -59,5 +59,5 @@ def get_frame(self):
5959
self.desktop_duplication.screenshot((left, top, right, bottom)),
6060
)
6161
if screenshot is None:
62-
return None, False
63-
return cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGRA), False
62+
return None
63+
return cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGRA)

src/capture_method/VideoCaptureDeviceCaptureMethod.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class VideoCaptureDeviceCaptureMethod(CaptureMethodBase):
4343
capture_thread: Thread | None = None
4444
stop_thread: Event
4545
last_captured_frame: MatLike | None = None
46+
last_converted_frame: MatLike | None = None
4647
is_old_image = False
4748

4849
def __read_loop(self):
@@ -121,13 +122,16 @@ def close(self):
121122
@override
122123
def get_frame(self):
123124
if not self.check_selected_region_exists():
124-
return None, False
125+
return None
125126

126127
image = self.last_captured_frame
127128
is_old_image = self.is_old_image
128129
self.is_old_image = True
129130
if not is_valid_image(image):
130-
return None, is_old_image
131+
return None
132+
133+
if is_old_image:
134+
return self.last_converted_frame
131135

132136
selection = self._autosplit_ref.settings_dict["capture_region"]
133137
# Ensure we can't go OOB of the image
@@ -137,7 +141,8 @@ def get_frame(self):
137141
y: y + selection["height"],
138142
x: x + selection["width"],
139143
]
140-
return cv2.cvtColor(image, cv2.COLOR_BGR2BGRA), is_old_image
144+
self.last_converted_frame = cv2.cvtColor(image, cv2.COLOR_BGR2BGRA)
145+
return self.last_converted_frame
141146

142147
@override
143148
def check_selected_region_exists(self):

src/capture_method/WindowsGraphicsCaptureMethod.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class WindowsGraphicsCaptureMethod(CaptureMethodBase):
3838
frame_pool: Direct3D11CaptureFramePool | None = None
3939
session: GraphicsCaptureSession | None = None
4040
"""This is stored to prevent session from being garbage collected"""
41-
last_captured_frame: MatLike | None = None
41+
last_converted_frame: MatLike | None = None
4242

4343
def __init__(self, autosplit: "AutoSplit"):
4444
super().__init__(autosplit)
@@ -82,25 +82,25 @@ def close(self):
8282
self.session = None
8383

8484
@override
85-
def get_frame(self) -> tuple[MatLike | None, bool]:
85+
def get_frame(self) -> MatLike | None:
8686
selection = self._autosplit_ref.settings_dict["capture_region"]
8787
# We still need to check the hwnd because WGC will return a blank black image
8888
if not (
8989
self.check_selected_region_exists()
9090
# Only needed for the type-checker
9191
and self.frame_pool
9292
):
93-
return None, False
93+
return None
9494

9595
try:
9696
frame = self.frame_pool.try_get_next_frame()
9797
# Frame pool is closed
9898
except OSError:
99-
return None, False
99+
return None
100100

101101
# We were too fast and the next frame wasn't ready yet
102102
if not frame:
103-
return self.last_captured_frame, True
103+
return self.last_converted_frame
104104

105105
async def coroutine():
106106
return await SoftwareBitmap.create_copy_from_surface_async(frame.surface)
@@ -110,13 +110,13 @@ async def coroutine():
110110
except SystemError as exception:
111111
# HACK: can happen when closing the GraphicsCapturePicker
112112
if str(exception).endswith("returned a result with an error set"):
113-
return self.last_captured_frame, True
113+
return self.last_converted_frame
114114
raise
115115

116116
if not software_bitmap:
117117
# HACK: Can happen when starting the region selector
118118
# TODO: Validate if this is still true
119-
return self.last_captured_frame, True
119+
return self.last_converted_frame
120120
# raise ValueError("Unable to convert Direct3D11CaptureFrame to SoftwareBitmap.")
121121
bitmap_buffer = software_bitmap.lock_buffer(BitmapBufferAccessMode.READ_WRITE)
122122
if not bitmap_buffer:
@@ -128,8 +128,8 @@ async def coroutine():
128128
selection["y"]: selection["y"] + selection["height"],
129129
selection["x"]: selection["x"] + selection["width"],
130130
]
131-
self.last_captured_frame = image
132-
return image, False
131+
self.last_converted_frame = image
132+
return image
133133

134134
@override
135135
def recover_window(self, captured_window_title: str):

src/region_selection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def align_region(autosplit: "AutoSplit"):
201201

202202
# Obtaining the capture of a region which contains the
203203
# subregion being searched for to align the image.
204-
capture, _ = autosplit.capture_method.get_frame()
204+
capture = autosplit.capture_method.get_frame()
205205

206206
if not is_valid_image(capture):
207207
error_messages.region()

0 commit comments

Comments
 (0)