用paddleocr识别微信聊天的内容,调用文心一言API进行回复。
PaddleOCR
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
项目地址:https://gitcode.com/gh_mirrors/pa/PaddleOCR
免费下载资源
·
设想
挂在电脑上,自动回复相关信息,当时只做了一个大概。后面可以成为客服务系统,订单查询,图片回复,用这样的方式微信也不会封。当然也可以写游戏的操作软件。
首先注册百度文心一言
相关注册可以在网上查询,同时生成自己的key 以及secret 。
https://login.bce.baidu.com/?account=
其实里面都说相关代码的说明。进行二次开发。
代码
*- coding: UTF-8 -*-
import sys
import time
import pyautogui
import pyperclip
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import re
import requests
import json
API_KEY = "<>"
SECRET_KEY = "<>"
URL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/aquilachat_7b?access_token="
language = 'ch'
img_path = 'screenshot_region.png'
result_img = 'screenshot_region_out.png'
ocr = PaddleOCR(use_angle_cls=True, use_gpu=False,lang=language)
def sendwx(target, msg, delay_time=2):
"""
实现原理:1、通过打开微信的快捷键ctrl+alt+w,打开微信窗口
2、通过搜索快捷键ctrl+f,打开搜索窗口
3、通过ctrl+v快捷键实现输入功能
4、通过ctrl+enter快捷键实现搜索or发送功能
:param target: 发送目标
:param msg: 需要发送的消息
:param delay_time: 延迟时间 默认2秒
:return:无返回值
"""
try:
# 清空剪切板并将目标写入到剪切板
pyperclip.copy("")
pyperclip.copy(target)
# 打开微信窗口
# pyautogui.hotkey("ctrl", "alt", "w")
# time.sleep(delay_time)
# 使用快捷键ctrl+f定位到微信搜索栏
pyautogui.hotkey("ctrl", "f")
time.sleep(delay_time)
# 使用快捷键ctrl+v将目标粘贴到微信搜索栏,微信将自动搜索
pyautogui.hotkey("ctrl", "v")
time.sleep(delay_time)
# 按回车键打开搜索出的目标
pyautogui.press("enter")
time.sleep(delay_time)
# 清空剪切板并将未点检信息写入到剪切板
pyperclip.copy("")
pyperclip.copy(msg)
# 使用快捷键ctrl+v将信息粘贴到微信输入框,按回车发送消息
pyautogui.hotkey("ctrl", "v")
time.sleep(delay_time)
pyautogui.press("enter")
# log
im = pyautogui.screenshot()
im.save('screenshot.png')
except Exception as ex:
print("发送微信消息出现异常: " + str(ex))
sys.exit(0)
def cutscreen(name):
pyperclip.copy(name)
pyautogui.hotkey("ctrl", "f")
time.sleep(50)
pyautogui.hotkey("ctrl", "v")
pyautogui.press("enter")
time.sleep(50)
# 清空剪切板并将未点检信息写入到剪切板
pyperclip.copy("")
screenshot = pyautogui.screenshot(region=(259, 64, 703, 637))
# 如果你想将截图保存为文件
screenshot.save('screenshot_region.png')
def ocrImg():
# need to run only once to download and load model into memory
result = ocr.ocr(img_path, cls=True)
worlds = ""
for line in result:
worlds = worlds + str(line[-1][0])
# 显示结果#
image = Image.open(img_path).convert('RGB')
boxes = [line[0] for line in result]
txts = [line[1][0] for line in result]
scores = [line[1][1] for line in result]
im_show = draw_ocr(image, boxes, txts, scores)
im_show = Image.fromarray(im_show)
im_show.save(result_img)
return worlds
def yiyananswer(question):
url = URL + get_access_token()
payload = json.dumps({
"messages": [
{
"role": "user",
"content": question
},
]
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
dict_obj = json.loads(response.text)
return dict_obj['result']
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
names=["微信名1","微信名2"]
while True:
for name in names:
cutscreen(name)
#会 识别最后一个提问后的文字发送到API
text = ocrImg(language, img_path, result_img)
matches = re.findall(r'提问(.*?)(?=\n|$)', text, re.DOTALL)
results = [match.split()[-1].strip() for match in matches]
if results:
last_poem = results[0]
input_string = yiyananswer(last_poem)
if input_string != "":
sendwx(name, input_string + "\n来源一言", 2)
time.sleep(50)
else:
time.sleep(50)
time.sleep(50)
GitHub 加速计划 / pa / PaddleOCR
44
11
下载
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
最近提交(Master分支:4 个月前 )
0697d248
2 天前
04c989b7
6 天前
更多推荐
已为社区贡献4条内容
所有评论(0)