基于百度AI识图做到根据聊天记录返回指定内容

需要pywin32模块,可自行百度下载安装

import win32gui
import win32con
import win32com.client
import win32clipboard as w
from PIL import ImageGrab
from aip import AipOcr
import time

# 以下三个常量可以在百度ai的识图接口中看到
APP_ID = '123456'
API_KEY = 'asdfghjkl'
SECRET_KEY = 'zxcvbnm'

wind = input("请输入你要查找的窗口测试名字:").strip()
search_key = input("请输入您要对比的文字(用-来连接多个要查找的内容):").strip()
send_ms = input("请输入您要发送的信息:").strip()

hwnd = win32gui.FindWindow(None, wind)
cl_name = win32gui.GetClassName(hwnd)

def print_sc(hwnd):
    """截图功能"""
    left, top, right, bot = win32gui.GetWindowRect(hwnd)
    win32gui.SendMessage(hwnd, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0)
    win32gui.SetForegroundWindow(hwnd)
    img_ready = ImageGrab.grab((left, top, right, bot))
    return left, top, right, bot, img_ready.save("sc.png")


def send_msg(send_ms,hwnd):
    """发送消息函数"""
    wind
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT, send_ms)
    w.CloseClipboard()
    win32gui.ShowWindow(hwnd, win32con.SW_SHOW)
    win32gui.SendMessage(hwnd, win32con.WM_PASTE, 0, 0)
    win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    time.sleep(1)  # 延缓进程

def get_img_content():
    """获取图片,并调用百度AI识别文字"""
    with open(r"C:\Users\Administrator\PycharmProjects\pythonProject1\sc.png", 'rb') as f:
        img2 = f.read()
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    ocr_res = client.basicGeneral(img2)
    # print(ocr_res)
    return ocr_res
#我的Android手机
def search_key_words(search_key, ocr_res):
    key_words = search_key.split('-')
    words_list = ocr_res['words_result']
    for i in words_list:
        for search_key in key_words:
            if search_key in i["words"]:
                print("找到了")
                return True


if __name__ == "__main__":
    win32gui.BringWindowToTop(hwnd)
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.SendKeys('%')
    win32gui.SetForegroundWindow(hwnd)
    while True:

        #需要一个截图的函数
        left,top,right,bot,sc = print_sc(hwnd)

        win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, 200, 200, 400, 400,
                              win32con.SWP_SHOWWINDOW)
        ocr_res = get_img_content()
        if search_key_words(search_key, ocr_res) is True:
            send_msg(send_ms,hwnd)
            break
        # time.sleep(1)```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值