This commit is contained in:
恍兮惚兮 2024-07-13 21:55:16 +08:00
parent 79e6dfa28f
commit 9296699635
10 changed files with 158 additions and 6 deletions

3
.gitmodules vendored
View File

@ -10,3 +10,6 @@
[submodule "plugins/libs/rapidfuzz-cpp"] [submodule "plugins/libs/rapidfuzz-cpp"]
path = plugins/libs/rapidfuzz-cpp path = plugins/libs/rapidfuzz-cpp
url = https://github.com/rapidfuzz/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

View File

@ -1,8 +1,4 @@
import requests
import random import random
import json
import os
import sys
from hashlib import md5 from hashlib import md5
from ocrengines.baseocrclass import baseocr from ocrengines.baseocrclass import baseocr

View File

@ -106,6 +106,8 @@ class baseocr(commonbase):
self.level2init() self.level2init()
try: try:
text = self.ocr(imagebinary) text = self.ocr(imagebinary)
if text is None:
text = ""
except Exception as e: except Exception as e:
self.needinit = True self.needinit = True
raise e raise e

View File

@ -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)

View File

@ -1454,6 +1454,11 @@
"use": false, "use": false,
"name": "manga-ocr", "name": "manga-ocr",
"type": "offline" "type": "offline"
},
"weixinocr": {
"use": false,
"name": "WeChatOCR",
"type": "offline"
} }
}, },
"fanyi": { "fanyi": {

View File

@ -29,7 +29,7 @@ include(generate_product_version)
set(VERSION_MAJOR 5) set(VERSION_MAJOR 5)
set(VERSION_MINOR 11) set(VERSION_MINOR 11)
set(VERSION_PATCH 1) set(VERSION_PATCH 2)
add_library(pch pch.cpp) add_library(pch pch.cpp)
target_precompile_headers(pch PUBLIC pch.h) target_precompile_headers(pch PUBLIC pch.h)
@ -40,4 +40,5 @@ add_subdirectory(winrtutils)
add_subdirectory(winsharedutils) add_subdirectory(winsharedutils)
add_subdirectory(hookmagpie) add_subdirectory(hookmagpie)
add_subdirectory(shareddllproxy) add_subdirectory(shareddllproxy)
add_subdirectory(applicationloopbackaudio) add_subdirectory(applicationloopbackaudio)
add_subdirectory(wcocr)

@ -0,0 +1 @@
Subproject commit ec76d84b950b68e5c3e2a06bc3d62f28bd06e19f

View File

@ -5,8 +5,10 @@ if x86:
shutil.copy('../builds/_x86/loopbackaudio.exe','../../LunaTranslator/files/plugins') shutil.copy('../builds/_x86/loopbackaudio.exe','../../LunaTranslator/files/plugins')
shutil.copy('../builds/_x86/winrtutils32.dll','../../LunaTranslator/files/plugins/DLL32') shutil.copy('../builds/_x86/winrtutils32.dll','../../LunaTranslator/files/plugins/DLL32')
shutil.copy('../builds/_x86/winsharedutils32.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: else:
shutil.copy('../builds/_x64/shareddllproxy64.exe','../../LunaTranslator/files/plugins') shutil.copy('../builds/_x64/shareddllproxy64.exe','../../LunaTranslator/files/plugins')
shutil.copy('../builds/_x64/hookmagpie.dll','../../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/winrtutils64.dll','../../LunaTranslator/files/plugins/DLL64')
shutil.copy('../builds/_x64/winsharedutils64.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')

View File

@ -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()

51
plugins/wcocr/wcocr.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <stdafx.h>
#include <wechatocr.h>
#include <nlohmann/json.hpp>
#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<CWeChatOCR *>(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<CWeChatOCR *>(pobj);
CWeChatOCR::result_t res;
std::string imgpath = u8path;
if (!obj->doOCR(imgpath, &res))
return 0;
std::vector<std::wstring> rets;
std::vector<int> 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;
}