Improve OCR speed and preprocessing
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
import pyperclip
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
from common.runtime_log import appendRuntimeLog
|
||||
from screen.screen_actions import captureOcrRegion
|
||||
|
||||
os.environ["FLAGS_enable_pir_api"] = "0"
|
||||
os.environ["FLAGS_use_onednn"] = "0"
|
||||
os.environ["FLAGS_use_mkldnn"] = "0"
|
||||
@@ -14,6 +8,17 @@ os.environ["FLAGS_use_onednn_bfloat16"] = "0"
|
||||
os.environ["MKLDNN_DISABLE_WORKSPACE"] = "1"
|
||||
os.environ["ONEDNN_VERBOSE"] = "0"
|
||||
|
||||
import pyperclip
|
||||
from paddleocr import PaddleOCR
|
||||
from PIL import Image
|
||||
from PIL import ImageFilter
|
||||
from PIL import ImageOps
|
||||
|
||||
from common.env_config import getEnvBool
|
||||
from common.env_config import getEnvFloat
|
||||
from common.runtime_log import appendRuntimeLog
|
||||
from screen.screen_actions import captureOcrRegion
|
||||
|
||||
_ocr_engine = None
|
||||
|
||||
|
||||
@@ -27,10 +32,17 @@ def getOcrEngine():
|
||||
|
||||
if _ocr_engine is None:
|
||||
log("INFO", "PaddleOCR initializing")
|
||||
_ocr_engine = PaddleOCR(
|
||||
lang="japan",
|
||||
use_angle_cls=True,
|
||||
)
|
||||
options = {
|
||||
"lang": "japan",
|
||||
"use_angle_cls": getEnvBool("OCR_USE_ANGLE_CLS", False),
|
||||
"show_log": False,
|
||||
}
|
||||
|
||||
try:
|
||||
_ocr_engine = PaddleOCR(**options)
|
||||
except (TypeError, ValueError):
|
||||
options.pop("show_log", None)
|
||||
_ocr_engine = PaddleOCR(**options)
|
||||
|
||||
return _ocr_engine
|
||||
|
||||
@@ -99,6 +111,37 @@ def printOcrResult(text):
|
||||
print(line, flush=True)
|
||||
|
||||
|
||||
def preprocessOcrImage(image_path):
|
||||
enabled = getEnvBool("OCR_PREPROCESS", True)
|
||||
if not enabled:
|
||||
return image_path
|
||||
|
||||
try:
|
||||
image = Image.open(image_path)
|
||||
image = image.convert("L")
|
||||
image = ImageOps.autocontrast(image)
|
||||
image = image.filter(ImageFilter.SHARPEN)
|
||||
|
||||
scale = max(1.0, min(2.0, getEnvFloat("OCR_SCALE", 1.5)))
|
||||
if scale > 1.0:
|
||||
width, height = image.size
|
||||
resampling = getattr(
|
||||
getattr(Image, "Resampling", Image),
|
||||
"LANCZOS",
|
||||
)
|
||||
image = image.resize(
|
||||
(int(width * scale), int(height * scale)),
|
||||
resampling,
|
||||
)
|
||||
|
||||
image.save(image_path)
|
||||
log("INFO", f"OCR image preprocessed={image_path}")
|
||||
except Exception as e:
|
||||
log("ERROR", f"OCR preprocess failed detail={e}")
|
||||
|
||||
return image_path
|
||||
|
||||
|
||||
def runPaddleOcr(engine, image_path):
|
||||
if hasattr(engine, "predict"):
|
||||
return engine.predict(str(image_path))
|
||||
@@ -109,6 +152,7 @@ def runOcrFromImage(image_path, show_result=True):
|
||||
log("ACTION", f"PaddleOCR start image={image_path}")
|
||||
|
||||
try:
|
||||
image_path = preprocessOcrImage(image_path)
|
||||
engine = getOcrEngine()
|
||||
result = runPaddleOcr(engine, image_path)
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user