Improve OCR speed and preprocessing
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import pyautogui
|
||||
import pygetwindow as gw
|
||||
|
||||
from common.env_config import getEnvFloat
|
||||
from common.project_paths import RUNTIME_DIR
|
||||
|
||||
SCREENSHOT_PATH = RUNTIME_DIR / "latest_screenshot.png"
|
||||
@@ -93,10 +94,44 @@ def getVrchatRegion():
|
||||
return left, top, width, height
|
||||
|
||||
|
||||
def captureVrchatWindowTo(image_path):
|
||||
def clampRatio(value):
|
||||
return max(0.0, min(1.0, value))
|
||||
|
||||
|
||||
def getOcrRegion():
|
||||
left, top, width, height = getVrchatRegion()
|
||||
|
||||
# Default to the lower central area. It is much faster than OCRing the
|
||||
# whole VRChat window and usually contains chat/subtitle text.
|
||||
crop_left = clampRatio(getEnvFloat("OCR_CROP_LEFT", 0.05))
|
||||
crop_top = clampRatio(getEnvFloat("OCR_CROP_TOP", 0.35))
|
||||
crop_right = clampRatio(getEnvFloat("OCR_CROP_RIGHT", 0.95))
|
||||
crop_bottom = clampRatio(getEnvFloat("OCR_CROP_BOTTOM", 0.95))
|
||||
|
||||
if crop_right <= crop_left:
|
||||
crop_left = 0.0
|
||||
crop_right = 1.0
|
||||
|
||||
if crop_bottom <= crop_top:
|
||||
crop_top = 0.0
|
||||
crop_bottom = 1.0
|
||||
|
||||
region_left = left + int(width * crop_left)
|
||||
region_top = top + int(height * crop_top)
|
||||
region_width = max(1, int(width * (crop_right - crop_left)))
|
||||
region_height = max(1, int(height * (crop_bottom - crop_top)))
|
||||
|
||||
log(
|
||||
"INFO",
|
||||
f"OCR crop left={region_left} top={region_top} width={region_width} height={region_height}",
|
||||
)
|
||||
return region_left, region_top, region_width, region_height
|
||||
|
||||
|
||||
def captureRegionTo(image_path, region):
|
||||
image_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
left, top, width, height = getVrchatRegion()
|
||||
left, top, width, height = region
|
||||
|
||||
image = pyautogui.screenshot(region=(left, top, width, height))
|
||||
image.save(image_path)
|
||||
@@ -105,12 +140,22 @@ def captureVrchatWindowTo(image_path):
|
||||
return image_path
|
||||
|
||||
|
||||
def captureVrchatWindowTo(image_path):
|
||||
return captureRegionTo(
|
||||
image_path,
|
||||
getVrchatRegion(),
|
||||
)
|
||||
|
||||
|
||||
def captureVrchatWindow():
|
||||
return captureVrchatWindowTo(SCREENSHOT_PATH)
|
||||
|
||||
|
||||
def captureOcrRegion():
|
||||
return captureVrchatWindowTo(OCR_SCREENSHOT_PATH)
|
||||
return captureRegionTo(
|
||||
OCR_SCREENSHOT_PATH,
|
||||
getOcrRegion(),
|
||||
)
|
||||
|
||||
|
||||
def captureScreen():
|
||||
|
||||
Reference in New Issue
Block a user