Add VRC OSC tooling

This commit is contained in:
messypy
2026-06-03 21:39:55 +09:00
committed by every_holiday
parent 072e819b0b
commit 54854969b9
24 changed files with 2982 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
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"
os.environ["FLAGS_use_onednn_bfloat16"] = "0"
os.environ["MKLDNN_DISABLE_WORKSPACE"] = "1"
os.environ["ONEDNN_VERBOSE"] = "0"
_ocr_engine = None
def log(level, message):
now = datetime.now().strftime("%H:%M:%S")
print(f"[{level}] {now} {message}", flush=True)
def getOcrEngine():
global _ocr_engine
if _ocr_engine is None:
log("INFO", "PaddleOCR initializing")
_ocr_engine = PaddleOCR(
lang="japan",
use_angle_cls=True,
)
return _ocr_engine
def saveOcrText(image_path, text):
return appendRuntimeLog(
"OCR TEXT",
f"image={image_path}\n{text}",
)
def extractTextFromAny(obj):
lines = []
def walk(x):
if x is None:
return
if isinstance(x, dict):
for key in ("rec_texts", "texts"):
value = x.get(key)
if isinstance(value, list):
for item in value:
text = str(item).strip()
if text:
lines.append(text)
for key in ("text", "rec_text"):
value = x.get(key)
if isinstance(value, str) and value.strip():
lines.append(value.strip())
for value in x.values():
walk(value)
return
if isinstance(x, (list, tuple)):
if len(x) >= 2 and isinstance(x[1], (list, tuple)) and len(x[1]) >= 1:
candidate = x[1][0]
if isinstance(candidate, str) and candidate.strip():
lines.append(candidate.strip())
for item in x:
walk(item)
walk(obj)
unique_lines = []
seen = set()
for line in lines:
if line not in seen:
unique_lines.append(line)
seen.add(line)
return "\n".join(unique_lines)
def printOcrResult(text):
cleaned = text.strip()
if not cleaned:
log("INFO", "OCR結果は空です")
return
log("CHECK", "OCR結果 full")
for line in cleaned.splitlines():
print(line, flush=True)
def runPaddleOcr(engine, image_path):
if hasattr(engine, "predict"):
return engine.predict(str(image_path))
return engine.ocr(str(image_path))
def runOcrFromImage(image_path, show_result=True):
log("ACTION", f"PaddleOCR start image={image_path}")
try:
engine = getOcrEngine()
result = runPaddleOcr(engine, image_path)
except Exception as e:
log("ERROR", f"PaddleOCR failed detail={e}")
return None
text = extractTextFromAny(result)
text_path = saveOcrText(image_path, text)
try:
pyperclip.copy(text)
log("INFO", "OCR結果をクリップボードへコピーしました")
except Exception as e:
log("ERROR", f"clipboard copy failed detail={e}")
log("INFO", f"OCR text appended={text_path}")
if show_result:
printOcrResult(text)
return {
"image_path": image_path,
"text_path": text_path,
"text": text,
}
def runOcrFromScreen():
log("ACTION", "runOcrFromScreen called")
image_path = captureOcrRegion()
return runOcrFromImage(image_path, show_result=True)