使用OCR文字识别技术QQ、微信等发送来的消息内容,然后自动匹配内容实现县自动回复消息功能,这是使用Python实现。
安装pillow
直接使用pip install Pillow
安装tesseract-ocr
pip install pytesseract
window上需要自己下载对应的识别库,安装完成之后需要后加入环境变量里面。tesseract是ocr识别系统,用于训练图片,可以自己训练哦,官方提供的识别率不是很高,需要自己按需要训练模型。
import cv2
from PIL import ImageGrab
import numpy as np
import argparse
import time
import pytesseract
from message import Message
from sender import Sender
global img
global point1, point2
def on_mouse(event, x, y, flags, param):
global img, point1, point2
img2 = img.copy()
if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
point1 = (x, y)
cv2.circle(img2, point1, 10, (0, 255, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON): # 按住左键拖曳
cv2.rectangle(img2, point1, (x, y), (255, 0, 0), thickness=2)
cv2.imshow('image', img2)
elif event == cv2.EVENT_LBUTTONUP: # 左键释放
point2 = (x, y)
cv2.rectangle(img2, point1, point2, (0, 0, 255), thickness=2)
cv2.imshow('image', img2)
def select_roi(frame):
global img, point1, point2
img = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)
winname = 'image'
cv2.namedWindow(winname, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(winname, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback(winname, on_mouse)
cv2.imshow(winname, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
return point1, point2
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# parser.add_argument('--fps', type=int, default=10, help='frame per second')
# parser.add_argument('--total_time', type=int, default=15, help='video total time')
# parser.add_argument('--savename', type=str, default='video.mp4', help='save file name')
# parser.add_argument('--screen_type', default=0, type=int, choices=[0, 1], help='1: full screen, 0: region screen')
args = parser.parse_args()
print('等到3秒,请切换到录屏的页面')
time.sleep(3)
print('Press Esc to close window')
curScreen = ImageGrab.grab() # 获取屏幕对象
point1, point2 = select_roi(curScreen)
left_x = point1[0]
top_y = point1[1]
right_x = point2[0]
bottom_y = point2[1]
sender = Sender()
message = Message()
while True:
#bbox边界框是一个(left_x, top_y, right_x, bottom_y)元组
captureImage = ImageGrab.grab(bbox =(left_x, top_y, right_x, bottom_y)) # 抓取屏幕
frame = cv2.cvtColor(np.array(captureImage), cv2.COLOR_RGB2BGR)
text=pytesseract.image_to_string(frame,lang='chi_sim')
sender.send_message("大熊", message.get_message(text))
if ord('q') == cv2.waitKey(50):
break
cv2.destroyAllWindows()
'''
win32安装
pip install pypiwin32 -i https://mirrors.aliyun.com/pypi/simple/
'''
import win32gui
import win32api
import win32con
import win32clipboard as w
import time
class Sender(object):
def __init__(self):
self.handle = None
#把文字放入剪贴板
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT,aString)
w.CloseClipboard()
#模拟ctrl+V
def ctrlV():
win32api.keybd_event(17,0,0,0) #ctrl
win32api.keybd_event(86,0,0,0) #V
win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)#释放按键
win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
#模拟alt+s
def altS():
win32api.keybd_event(18,0,0,0)
win32api.keybd_event(83,0,0,0)
win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
# 模拟enter
def enter():
win32api.keybd_event(13,0,0,0)
win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)
#模拟单击
def click():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
#移动鼠标的位置
def movePos(x,y):
win32api.SetCursorPos((x,y))
'''
将消息复制到粘贴板上
'''
def set_text(self, text):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, text)
w.CloseClipboard()
'''
模拟按键发送消息
'''
def send_message(self, window, text):
self.set_text(text)
if not self.handle:
self.handle = win32gui.FindWindow(None, window)
# win32gui.SetForegroundWindow(handle)
# win32gui.GetDlgCtrlID(handle)
#填充消息
win32gui.SendMessage(self.handle, 770, 0, 0)
#回车发送消息
win32gui.SendMessage(self.handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
# time.sleep(0.5)
# #ctrl + v
# win32api.keybd_event(17, 0, 0, 0)
# win32api.keybd_event(86, 0, 0, 0)
# win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
# win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
# #enter
# time.sleep(0.5)
# win32api.keybd_event(0xD0, 0, 0, 0)
# win32api.keybd_event(0xD0, 0, win32con.KEYEVENTF_KEYUP, 0)
'''
这里可以请求图灵开放平台聊天的接口api回复对应的消息。也可以自定义回复的内容。
'''
class Message(object):
def get_message(self, input):
print(input)
return "test robot"
目前的代码还不是很完善后续会更新。