From 9296699635b42901d2e96081d76cb630ae5afd13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Sat, 13 Jul 2024 21:55:16 +0800 Subject: [PATCH] wx --- .gitmodules | 3 + .../LunaTranslator/ocrengines/baiduocr_ts.py | 4 -- .../LunaTranslator/ocrengines/baseocrclass.py | 2 + .../LunaTranslator/ocrengines/weixinocr.py | 61 +++++++++++++++++++ .../files/defaultconfig/config.json | 5 ++ plugins/CMakeLists.txt | 5 +- plugins/libs/wechat-ocr | 1 + plugins/scripts/copytarget.py | 2 + plugins/wcocr/CMakeLists.txt | 30 +++++++++ plugins/wcocr/wcocr.cpp | 51 ++++++++++++++++ 10 files changed, 158 insertions(+), 6 deletions(-) create mode 100644 LunaTranslator/LunaTranslator/ocrengines/weixinocr.py create mode 160000 plugins/libs/wechat-ocr create mode 100644 plugins/wcocr/CMakeLists.txt create mode 100644 plugins/wcocr/wcocr.cpp diff --git a/.gitmodules b/.gitmodules index 5df6ef92..e1a01cde 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [submodule "plugins/libs/rapidfuzz-cpp"] path = plugins/libs/rapidfuzz-cpp url = https://github.com/rapidfuzz/rapidfuzz-cpp +[submodule "plugins/libs/wechat-ocr"] + path = plugins/libs/wechat-ocr + url = https://github.com/swigger/wechat-ocr diff --git a/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py b/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py index 05bb4bee..ba463d67 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py +++ b/LunaTranslator/LunaTranslator/ocrengines/baiduocr_ts.py @@ -1,8 +1,4 @@ -import requests import random -import json -import os -import sys from hashlib import md5 from ocrengines.baseocrclass import baseocr diff --git a/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py b/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py index abfabf72..22689072 100644 --- a/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py +++ b/LunaTranslator/LunaTranslator/ocrengines/baseocrclass.py @@ -106,6 +106,8 @@ class baseocr(commonbase): self.level2init() try: text = self.ocr(imagebinary) + if text is None: + text = "" except Exception as e: self.needinit = True raise e diff --git a/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py b/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py new file mode 100644 index 00000000..a8d697ea --- /dev/null +++ b/LunaTranslator/LunaTranslator/ocrengines/weixinocr.py @@ -0,0 +1,61 @@ +import gobject, os, uuid, json +from ocrengines.baseocrclass import baseocr +from ctypes import CDLL, c_void_p, c_wchar_p, c_char_p, cast + + +class OCR(baseocr): + def initocr(self): + self.wcocr = CDLL(gobject.GetDllpath(("wcocr.dll"))) + wcocr_init = self.wcocr.wcocr_init + wcocr_init.argtypes = ( + c_wchar_p, + c_wchar_p, + ) + wcocr_init.restype = c_void_p + + base = r"C:\Program Files\Tencent\WeChat" + for _ in os.listdir(base): + if _.startswith("["): + wechat_path = os.path.join( + base, + _, + ) + break + wechatocr_path = ( + os.getenv("APPDATA") + + r"\Tencent\WeChat\XPlugin\Plugins\WeChatOCR\7079\extracted\WeChatOCR.exe" + ) + self.pobj = wcocr_init(wechatocr_path, wechat_path) + + def end(self): + + wcocr_destroy = self.wcocr.wcocr_destroy + wcocr_destroy.argtypes = c_void_p + wcocr_destroy(self.pobj) + + def ocr(self, imagebinary): + if not self.pobj: + raise Exception("找不到微信&微信OCR路径") + fname = gobject.gettempdir(str(uuid.uuid4()) + ".png") + with open(fname, "wb") as ff: + ff.write(imagebinary) + imgfile = os.path.abspath(fname) + wcocr_ocr = self.wcocr.wcocr_ocr + wcocr_ocr.argtypes = c_void_p, c_char_p + wcocr_ocr.restype = c_void_p + wcocr_free_str = self.wcocr.wcocr_free_str + wcocr_free_str.argtypes = (c_void_p,) + pstring = wcocr_ocr(self.pobj, imgfile.encode("utf8")) + if not pstring: + return + string = cast(pstring, c_char_p).value.decode("utf8") + wcocr_free_str(pstring) + + os.remove(imgfile) + boxs = [] + texts = [] + for line in json.loads(string): + x1, y1, x2, y2, text = line + boxs.append((x1, y1, x2, y2)) + texts.append(text) + return self.common_solve_text_orientation(boxs, texts) diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index 478b670c..877306b9 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1454,6 +1454,11 @@ "use": false, "name": "manga-ocr", "type": "offline" + }, + "weixinocr": { + "use": false, + "name": "WeChatOCR", + "type": "offline" } }, "fanyi": { diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 4fc8318a..ac89cc6f 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -29,7 +29,7 @@ include(generate_product_version) set(VERSION_MAJOR 5) set(VERSION_MINOR 11) -set(VERSION_PATCH 1) +set(VERSION_PATCH 2) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h) @@ -40,4 +40,5 @@ add_subdirectory(winrtutils) add_subdirectory(winsharedutils) add_subdirectory(hookmagpie) add_subdirectory(shareddllproxy) -add_subdirectory(applicationloopbackaudio) \ No newline at end of file +add_subdirectory(applicationloopbackaudio) +add_subdirectory(wcocr) \ No newline at end of file diff --git a/plugins/libs/wechat-ocr b/plugins/libs/wechat-ocr new file mode 160000 index 00000000..ec76d84b --- /dev/null +++ b/plugins/libs/wechat-ocr @@ -0,0 +1 @@ +Subproject commit ec76d84b950b68e5c3e2a06bc3d62f28bd06e19f diff --git a/plugins/scripts/copytarget.py b/plugins/scripts/copytarget.py index b8098ebe..884100c8 100644 --- a/plugins/scripts/copytarget.py +++ b/plugins/scripts/copytarget.py @@ -5,8 +5,10 @@ if x86: shutil.copy('../builds/_x86/loopbackaudio.exe','../../LunaTranslator/files/plugins') shutil.copy('../builds/_x86/winrtutils32.dll','../../LunaTranslator/files/plugins/DLL32') shutil.copy('../builds/_x86/winsharedutils32.dll','../../LunaTranslator/files/plugins/DLL32') + shutil.copy('../builds/_x86/wcocr.dll','../../LunaTranslator/files/plugins/DLL32') else: shutil.copy('../builds/_x64/shareddllproxy64.exe','../../LunaTranslator/files/plugins') shutil.copy('../builds/_x64/hookmagpie.dll','../../LunaTranslator/files/plugins') shutil.copy('../builds/_x64/winrtutils64.dll','../../LunaTranslator/files/plugins/DLL64') shutil.copy('../builds/_x64/winsharedutils64.dll','../../LunaTranslator/files/plugins/DLL64') + shutil.copy('../builds/_x64/wcocr.dll','../../LunaTranslator/files/plugins/DLL64') diff --git a/plugins/wcocr/CMakeLists.txt b/plugins/wcocr/CMakeLists.txt new file mode 100644 index 00000000..1a8df647 --- /dev/null +++ b/plugins/wcocr/CMakeLists.txt @@ -0,0 +1,30 @@ + +project(wcocr) + +set(wcocr_src wechat-ocr/src/mojocall.cpp wechat-ocr/src/ocr_protobuf.pb.cc wechat-ocr/src/wechatocr.cpp) +message(${wcocr_src}) +string(REPLACE ";" ";../libs/" wcocr_src "${wcocr_src}") +message(${wcocr_src}) +set(wcocr_src "../libs/${wcocr_src}") +message(${wcocr_src}) + + +add_library(wcocr MODULE wcocr.cpp ${wcocr_src}) +target_include_directories(wcocr PUBLIC ../libs/wechat-ocr/vs.proj PUBLIC ../libs/wechat-ocr/src PRIVATE ../libs/wechat-ocr/spt) + +target_compile_options(wcocr + PUBLIC + /std:c++17 + /MD + /wd4018 + /DUNICODE + /D_UNICODE +) + +if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) + target_link_libraries(wcocr ${CMAKE_SOURCE_DIR}/libs/wechat-ocr/spt/x64/libprotobuf-lite.lib ${CMAKE_SOURCE_DIR}/libs/wechat-ocr/spt/x64/mmmojo_64.lib) + target_link_options(wcocr PRIVATE "/DELAYLOAD:mmmojo_64.dll") +else() + target_link_libraries(wcocr ${CMAKE_SOURCE_DIR}/libs/wechat-ocr/spt/x86/libprotobuf-lite.lib ${CMAKE_SOURCE_DIR}/libs/wechat-ocr/spt/x86/mmmojo.lib) + target_link_options(wcocr PRIVATE "/DELAYLOAD:mmmojo.dll") +endif() diff --git a/plugins/wcocr/wcocr.cpp b/plugins/wcocr/wcocr.cpp new file mode 100644 index 00000000..6a294133 --- /dev/null +++ b/plugins/wcocr/wcocr.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#define DECLARE extern "C" __declspec(dllexport) + +DECLARE void *wcocr_init(const wchar_t *wexe, const wchar_t *wwcdir) +{ + auto obj = new CWeChatOCR(wexe, wwcdir); + if (obj->wait_connection(5000)) + { + return obj; + } + else + { + delete obj; + return nullptr; + } +} + +DECLARE void wcocr_destroy(void *pobj) +{ + if (!pobj) + return; + auto obj = reinterpret_cast(pobj); + delete obj; +} +DECLARE void wcocr_free_str(char *ptr) +{ + delete[] ptr; +} +DECLARE char *wcocr_ocr(void *pobj, const char *u8path) +{ + if (!pobj) + return 0; + auto obj = reinterpret_cast(pobj); + CWeChatOCR::result_t res; + std::string imgpath = u8path; + if (!obj->doOCR(imgpath, &res)) + return 0; + std::vector rets; + std::vector xs, ys, xs2, ys2; + nlohmann::json js; + for (auto &blk : res.ocr_response) + { + js.push_back({blk.left, blk.top, blk.right, blk.bottom, blk.text}); + } + std::string _s = js.dump(); + auto s = new char[_s.size() + 1]; + strcpy(s, _s.c_str()); + return s; +} \ No newline at end of file