使用OCR文字識別技術(shù)QQ俯画、微信等發(fā)送來的消息內(nèi)容析桥,然后自動匹配內(nèi)容實(shí)現(xiàn)縣自動回復(fù)消息功能,這是使用Python實(shí)現(xiàn)艰垂。
安裝pillow
直接使用pip install Pillow
安裝tesseract-ocr
pip install pytesseract
window上需要自己下載對應(yīng)的識別庫泡仗,安裝完成之后需要后加入環(huán)境變量里面。tesseract是ocr識別系統(tǒng)猜憎,用于訓(xùn)練圖片娩怎,可以自己訓(xùn)練哦,官方提供的識別率不是很高胰柑,需要自己按需要訓(xùn)練模型截亦。
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: # 左鍵點(diǎn)擊
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)
#移動鼠標(biāo)的位置
def movePos(x,y):
win32api.SetCursorPos((x,y))
'''
將消息復(fù)制到粘貼板上
'''
def set_text(self, text):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, text)
w.CloseClipboard()
'''
模擬按鍵發(fā)送消息
'''
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)
#回車發(fā)送消息
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回復(fù)對應(yīng)的消息柬讨。也可以自定義回復(fù)的內(nèi)容崩瓤。
'''
class Message(object):
def get_message(self, input):
print(input)
return "test robot"
目前的代碼還不是很完善后續(xù)會更新。