python 鼠标选择区域截图、识别文字、模糊搜索答案

import pyautogui
import tkinter as tk
import cv2 
import keyboard
from pystray import Icon, MenuItem, Menu
from PIL import Image
import paddlehub as hub
import pyperclip

from fuzzywuzzy import fuzz
from collections import defaultdict
import os

# 搜索关键词
# search_text = "PI主要工作内容"

def onesearch(search_text):
    # 文本文件路径
    file_path = '1.txt'

    # 用于存储结果的字典,键是相似度,值是匹配行内容
    results = defaultdict(list)

    # 逐行读取文件
    with open(file_path, 'r', encoding='utf-8') as f:
        for line_number, line in enumerate(f, 1):
            # 使用fuzz.ratio计算相似度,这里也可以替换为其他相似度计算方法
            similarity = fuzz.partial_ratio(search_text, line)
            # similarity = fuzz.ratio(search_text, line.strip())
            # 只保留相似度大于某个阈值的结果,这里设置为50,可以根据实际情况调整
            if similarity > 0:
                results[similarity].append((line_number, line))
                # results[similarity].append((line_number, line.strip()))

    # 对结果按照相似度降序排列,并只取前10个
    sorted_results = sorted(results.items(), reverse=True)[:1]

    # 打印相似度最高的10个结果
    for similarity, matched_lines in sorted_results:
        for line_number, line in matched_lines:
            # print(similarity, line_number, line)
            print(f"相似度: {similarity}%, 第 {line_number} 行: {line}")
            # print(f"相似度: {similarity}%, 第 {line_number} 行: {line}".encode('utf-8').decode('utf-8'))


ocr = hub.Module(name="chinese_ocr_db_crnn_server") 
img = pyautogui.screenshot(region=[1,1,1,1])

# 定义退出函数
def on_quit(icon, item):
    icon.stop()

# 创建托盘图标
image = Image.open("icon.png")  # 替换成你自己的图标文件路径
icon = Icon("example", image, "Example App", menu=Menu(MenuItem('Quit', on_quit)))




# 开始监听键盘事件
# keyboard.wait('Esc')

def one():
    root = tk.Tk()
    root.overrideredirect(True)         # 隐藏窗口的标题栏
    root.attributes("-alpha", 0.1)      # 窗口透明度10%
    root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
    root.configure(bg="black")

    cv = tk.Canvas(root)
    canvas = tk.Canvas(root)
    canvas.configure(width=300)
    canvas.configure(height=100)
    canvas.configure(bg="yellow")
    canvas.configure(highlightthickness=0)  # 高亮厚度
    # canvas.place(x=(root.winfo_screenwidth()-500),y=(root.winfo_screenheight()-300))
    # canvas.create_text(150, 50,font='Arial -20 bold',text='ESC退出,假装工具条')

    def move(event):
        global x, y ,xstart,ystart
        new_x = (event.x-x)+canvas.winfo_x()
        new_y = (event.y-y)+canvas.winfo_y()
        s = "300x200+" + str(new_x)+"+" + str(new_y)    
        canvas.place(x = new_x - xstart,y = new_y -ystart)   
        print("s = ", s)
        print(root.winfo_x(), root.winfo_y())
        print(event.x, event.y)

    def button_1(event):
        global x, y ,xstart,ystart
        global rec
        x, y = event.x, event.y
        xstart,ystart = event.x, event.y
        print("event.x, event.y = ", event.x, event.y)
        xstart,ystart = event.x, event.y  
        cv.configure(height=1)
        cv.configure(width=1)
        cv.config(highlightthickness=0) # 无边框
        cv.place(x=event.x, y=event.y)
        rec = cv.create_rectangle(0,0,0,0,outline='red',width=8,dash=(4, 4))

    def b1_Motion(event):
        global x, y,xstart,ystart
        x, y = event.x, event.y
        print("event.x, event.y = ", event.x, event.y)
        cv.configure(height = event.y - ystart)
        cv.configure(width = event.x - xstart)
        cv.coords(rec,0,0,event.x-xstart,event.y-ystart)

    def buttonRelease_1(event):
        global xend,yend
        xend, yend = event.x, event.y

    def ocrone(img):
        ctext=''
        results = ocr.recognize_text(
                            images=[img],         # 图片数据,ndarray.shape 为 [H, W, C],BGR格式;
                            use_gpu=False,            # 是否使用 GPU;若使用GPU,请先设置CUDA_VISIBLE_DEVICES环境变量
                            output_dir='ocr_result',  # 图片的保存路径,默认设为 ocr_result;
                            visualization=False,       # 是否将识别结果保存为图片文件;
                            box_thresh=0.5,           # 检测文本框置信度的阈值;
                            text_thresh=0.5)          # 识别中文文本置信度的阈值;

        for result in results:
            data = result['data']
            save_path = result['save_path']
            for infomation in data:
                
                ctext += infomation['text']
                print('text: ', infomation['text'], '\nconfidence: ', infomation['confidence'], '\ntext_box_position: ', infomation['text_box_position'])
            pyperclip.copy(ctext)
            print(ctext)
            onesearch(ctext)


    def button_3(event):  # 鼠标右键截图退出截图模式
        global xstart,ystart,xend,yend
        cv.delete(rec)
        cv.place_forget()
        img = pyautogui.screenshot(region=[xstart+3,ystart+3,xend-xstart-6,yend-ystart-6]) # x,y,w,h
        img.save('screenshot.png')
        # 将截图图像转换为OpenCV格式并进行预处理
        img = cv2.imread("screenshot.png")
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # PaddleOCR需要RGB格式
        ocrone(img)
        sys_out(None)

    def sys_out(even):
        root.destroy()


    # 绑定事件
    canvas.bind("<B1-Motion>", move)   # 鼠标左键移动->显示当前光标位置
    root.bind('<Escape>',sys_out)      # 键盘Esc键->退出
    root.bind("<Button-1>", button_1)  # 鼠标左键点击->显示子窗口 
    root.bind("<B1-Motion>", b1_Motion)# 鼠标左键移动->改变子窗口大小
    root.bind("<ButtonRelease-1>", buttonRelease_1) # 鼠标左键释放->记录最后光标的位置
    root.bind("<Button-3>",button_3)   #鼠标右键点击->截屏并保存图片
    root.mainloop()

# 注册全局热键,按下Ctrl+Shift+A时触发
keyboard.add_hotkey('Alt+Q', one)

# 定义退出函数
def on_wquit():
    icon.stop()

keyboard.add_hotkey('Alt+W', on_wquit)
print('Alt+Q to screenshot, Alt+W to quit, esc to exit capture, right click to capture')

# 运行托盘图标
icon.run()

# one()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值