test1 experiments. some optimizations

- added self.test1 for experimenting cleaner
- began steps to calculate map size
- optimize several functions by moving from mean to max
This commit is contained in:
Doc
2026-01-13 04:07:19 -05:00
parent f386b154ab
commit 941eae5d57

View File

@@ -1,4 +1,5 @@
import time
import timeit
from pathlib import Path
import cv2
@@ -8,6 +9,8 @@ from loguru import logger
from .waytools import capActiveWindow, focusWindow, moveMouse
from .waytools import sendKey as _sendKey
# TODO: Consider type hinting images from cv2.typing import MatLike
class DFWINDOW:
class TOOLS:
@@ -29,20 +32,20 @@ class DFWINDOW:
) -> tuple[int, int]:
# Check the first (num_rows) rows at the top of the image,
# ignoring (ignore_cols) number of pixels at each end of teh line.
test_mean = np.mean(
test_max = np.max(
cv2.cvtColor(image_in[0:num_rows, ignore_cols:-ignore_cols], cv2.COLOR_BGR2GRAY),
axis=1, # get the mean along the x-axis
)
# TODO: handle when 0 results return
# Test the mean darkness, get the first row darker than 4
content_y = np.where(test_mean < mean_threshold)[0][0]
content_y = np.where(test_max < mean_threshold)[0][0]
_ignore_rows = max(ignore_rows, content_y + 1)
test_mean = np.mean(
test_max = np.max(
cv2.cvtColor(image_in[_ignore_rows:-_ignore_rows, 0:num_cols], cv2.COLOR_BGR2GRAY),
axis=0, # get the mean along the y-axis
)
content_x = np.where(test_mean < mean_threshold)[0][0]
content_x = np.where(test_max < mean_threshold)[0][0]
logger.debug(f"Content origin ({content_x}, {content_y})")
@@ -89,14 +92,14 @@ class DFWINDOW:
return not np.any(thresh)
@staticmethod
def firstNotBlackX(img):
first_x = np.where(np.mean(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), axis=0) > 15)[0][0]
return first_x
def firstNotBlackX(img) -> int:
first_x = np.where(np.max(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), axis=0) > 15)[0][0]
return int(first_x)
@staticmethod
def firstNotBlackY(img):
first_y = np.where(np.mean(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), axis=1) > 15)[0][0]
return first_y
def firstNotBlackY(img) -> int:
first_y = np.where(np.max(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), axis=1) > 15)[0][0]
return int(first_y)
bottom_to_ignore = 120
sleep_after_mouse = 0.2
@@ -318,6 +321,9 @@ class DFWINDOW:
logger.debug("Calibration error. Not at requested upper left of map")
raise Exception("Calibration error. Not at requested upper left of map")
cal_left_border = self.TOOLS.firstNotBlackX(img)
cal_top_border = self.TOOLS.firstNotBlackY(img)
# Test going to (max,max)
self.setGridPos(self.maxGridX, self.maxGridY)
time.sleep(self.sleep_after_panning)
@@ -330,7 +336,37 @@ class DFWINDOW:
f"Grid calibration complete. Grid steps ({self._gridy_max + 1},{self._gridy_max + 1}), step sizes({self._step_size_x},{self._step_size_y})"
)
def test1(self):
rawimg = cv2.imread("./test_img.png")
img = rawimg[100 : -self.bottom_to_ignore - 70, 65:-65]
tlb = self.TOOLS.firstNotBlackX(img)
ttb = self.TOOLS.firstNotBlackY(img)
tt_setup = r"gc.enable() ; import cv2 ; import numpy as np ; timg = cv2.imread('./test_img.png', cv2.IMREAD_UNCHANGED)"
tt1 = timeit.Timer(
"np.where(np.mean(cv2.cvtColor(timg, cv2.COLOR_BGR2GRAY), axis=0) > 15)[0][0]",
setup=tt_setup,
)
tt2 = timeit.Timer(
"np.where(np.max(cv2.cvtColor(timg, cv2.COLOR_BGR2GRAY), axis=0) > 25)[0][0]",
setup=tt_setup,
)
tt3 = timeit.Timer(
"np.where(np.max(cv2.cvtColor(timg, cv2.COLOR_BGRA2GRAY), axis=0) > 25)[0][0]",
setup=tt_setup,
)
num_tests = 80
r1 = tt1.timeit(number=num_tests)
r2 = tt2.timeit(number=num_tests)
r3 = tt3.timeit(number=num_tests)
logger.debug("Pause here for testing")
def getPanoramaMap(self):
self.test1()
return
self.calibrateGrid()
# Test getting pieces and stitching