kanadeblisst00 / wechat_ocr Goto Github PK
View Code? Open in Web Editor NEW使用Python调用微信本地ocr服务
使用Python调用微信本地ocr服务
添加WeChatUtility.exe:
QRScan功能
下面这段代码中 ocr_result_callback
似乎没有办法设置 传参数量,默认一个 img_path
一个 result_json
没找到在哪改
def main():
ocr_manager = OcrManager(wechat_dir)
# 设置WeChatOcr目录
ocr_manager.SetExePath(wechat_ocr_dir)
# 设置微信所在路径
ocr_manager.SetUsrLibDir(wechat_dir)
# 设置ocr识别结果的回调函数
ocr_manager.SetOcrResultCallback(ocr_result_callback)
def ocr_result_callback(img_path:str, result_json:str, **args):
result_json = EasyDict(result_json)
找到
def CallUsrCallback(self, request_id:c_uint32, serialized_data: c_void_p, data_size: int):
ocr_response_ubyte = (c_ubyte * data_size).from_address(serialized_data)
ocr_response_array = bytearray(ocr_response_ubyte)
ocr_response = ocr_protobuf_pb2.OcrResponse()
ocr_response.ParseFromString(ocr_response_array)
json_response_str = MessageToJson(ocr_response)
task_id = ocr_response.task_id
if not self.m_id_path.get(task_id):
return
#print(f"收到识别结果, task_id: {task_id}, result: {json_response}")
pic_path = self.m_id_path[task_id]
if self.m_usr_callback:
self.m_usr_callback(pic_path, self.parse_json_response(json_response_str))
self.SetTaskIdIdle(task_id)
self.m_usr_callback(pic_path, self.parse_json_response(json_response_str))
改为
在初始化时候传参,不是源码里传参
能否改为 def test_kwargs(first, *args, **kwargs):
这样类型
目前个人是使用 ,**kwargs
但是还是不能传参,能否增加返回值的功能,就可以不使用全局变量来保存了
def ocr_result_callback(img_path:str, result_json:str, www=1):
result_json = EasyDict(result_json)
发来贺电,比其他那些什么ocr来的简单多了,谢谢!
cv2活pil图片对象直接传进去识别吗?
其中的mode表示是否换行保存text内容。
import json
json_file = r"G:\Desktop\python小项目\调用微信ocr进行识别文字\2.png.json"
save_file = "save.txt"
def save_text(json_file, save_file, mode=1):
# 打开 JSON 文件
with open(json_file, 'r', encoding='utf-8') as file:
# 从文件中加载 JSON 数据
data = json.load(file)
# 换行保存
if mode == 1:
with open(save_file, 'w', encoding='utf-8') as f:
# 提取每个对象的 text 字段
for item in data['ocrResult']:
print(item['text'])
f.write(item['text'] + '\n')
# 不换行保存
if mode == 2:
with open(save_file, 'w', encoding='utf-8') as f:
# 提取每个对象的 text 字段
for item in data['ocrResult']:
print(item['text'])
f.write(item['text'])
save_text(json_file, save_file, mode=1)
demo运行成功, 但是结果的json文件里面是空的
(web) D:\ocr>python t.py
等待Ocr服务连接成功!
OCRRemoteOnConnect 回调函数被调用, 参数, is_connected: True
DefaultRemoteProcessLaunched 回调函数被调用
OCRReadOnPush 回调函数被调用 参数, request_id: 1, request_info: 46523088635872
OCRReadOnPush 回调函数被调用 参数, request_id: 1, request_info: 46523088636096
正在解析pb数据,pb数据大小: 15
img_path: D:\ocr\1.png, result_file: 1.png.json
结果如下
{
"taskId": 1,
"ocrResult": []
}
所使用的机器是windows server2016 不能上网,微信安装了没有使用, 不知道是不是这个原因,请大佬帮忙定位下😭
调试通过之后,不希望在控制台中看到类似的日志打印输出:
OCRRemoteOnConnect 回调函数被调用, 参数, is_connected: True
DefaultRemoteProcessLaunched 回调函数被调用
OCRReadOnPush 回调函数被调用 参数, request_id: 1, request_info: 110054244927456
正在解析pb数据,pb数据大小: 65817
能否在在初始化的时候加个变量,指明现在是否是调试状态,调试状态下可以正常输出各种信息。非调试状态下就只输出报警或错误信息,上面的类似信息就不需要了。
我想做成不装微信也能使用的功能,但是我在一台没有装过微信的电脑上运行时,一直打印“等待Ocr服务连接成功” 因为m_connect_state.value一直是0
我在一台装过微信的电脑上可以正常使用(即使我把微信卸载了)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.