From 07a7a19929dc9d75c9417f8129bef5a6fecd9b00 Mon Sep 17 00:00:00 2001 From: YoursFunny Date: Sun, 5 Jul 2026 16:40:00 +0800 Subject: [PATCH] feat: add area_center function and enhance OCR processing for white letters --- module/base/utils/utils.py | 14 ++++++++++++++ module/ocr/ocr.py | 20 +++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/module/base/utils/utils.py b/module/base/utils/utils.py index ce29352..4f2abea 100644 --- a/module/base/utils/utils.py +++ b/module/base/utils/utils.py @@ -274,6 +274,20 @@ def area_size(area): ) +def area_center(area): + """ + Get the center of an area + + Args: + area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y) + + Returns: + tuple: (x, y) + """ + x1, y1, x2, y2 = area + return (x1 + x2) / 2, (y1 + y2) / 2 + + def point_limit(point, area): """ Limit point in an area. diff --git a/module/ocr/ocr.py b/module/ocr/ocr.py index 4d36f7a..6c3b390 100644 --- a/module/ocr/ocr.py +++ b/module/ocr/ocr.py @@ -3,13 +3,14 @@ import time from datetime import timedelta from typing import Optional +import cv2 import numpy as np from pponnxcr.predict_system import BoxedResult import module.config.server as server from module.base.button import ButtonWrapper from module.base.decorator import cached_property -from module.base.utils import area_pad, corner2area, crop, float2str +from module.base.utils import area_pad, corner2area, crop, extract_white_letters, float2str from module.exception import ScriptError from module.logger import logger from module.ocr.keyword import Keyword @@ -416,3 +417,20 @@ class Duration(Ocr): if number is None: return 0 return int(number) + + +class OcrWhiteLetterOnComplexBackground(Ocr): + def pre_process(self, image): + image = extract_white_letters(image, threshold=255) + image = cv2.merge([image, image, image]) + return image + + def detect_and_ocr(self, *args, **kwargs): + # Try hard to lower TextSystem.box_thresh + backup = self.model.text_detector.box_thresh + self.model.text_detector.box_thresh = 0.2 + + result = super().detect_and_ocr(*args, **kwargs) + + self.model.text_detector.box_thresh = backup + return result