简单的文字识别

This commit is contained in:
awin-x 2025-01-15 21:54:17 +08:00
parent 67b54ce2b6
commit 00a984ac61
3 changed files with 24 additions and 16 deletions

View File

@ -1,6 +1,6 @@
import pinPicture import pinPicture
from PIL import Image
import tkinter as tk import tkinter as tk
import numpy as np
import pyautogui import pyautogui
import keyboard import keyboard
@ -14,9 +14,6 @@ def capture_screen():
root.mainloop() root.mainloop()
# 监听特定快捷键这里假设是Ctrl+Alt+S
keyboard.add_hotkey('F7', capture_screen) keyboard.add_hotkey('F7', capture_screen)
# 保持程序运行,持续监听快捷键 keyboard.wait()
keyboard.wait()

View File

@ -2,13 +2,15 @@ import io
import math import math
import tkinter as tk import tkinter as tk
import win32clipboard import win32clipboard
from PIL import Image, ImageTk, ImageChops, ImageDraw, ImageGrab from PIL import Image, ImageTk, ImageDraw
import threading import numpy as np
import easyocr
class PinPicture(tk.Frame): class PinPicture(tk.Frame):
def __init__(self, image, master=None): def __init__(self, image, master=None):
super().__init__(master) super().__init__(master)
self.detectBtn = None
self.pinBtn = None self.pinBtn = None
self.lineBtn = None self.lineBtn = None
self.arrowBtn = None self.arrowBtn = None
@ -34,9 +36,10 @@ class PinPicture(tk.Frame):
self.drawing = False self.drawing = False
self.drawMode = False self.drawMode = False
self.color = "red" self.color = "red"
self.image = image
if image.mode != "RGBA": if image.mode != "RGBA":
image = image.convert("RGBA") self.image = image.convert("RGBA")
else:
self.image = image
self.create_window() self.create_window()
self.penWidth = 3 self.penWidth = 3
self.arrowSize = 10 self.arrowSize = 10
@ -44,7 +47,6 @@ class PinPicture(tk.Frame):
# 创建窗口 # 创建窗口
def create_window(self): def create_window(self):
self.master.overrideredirect(True) self.master.overrideredirect(True)
self.master.attributes('-transparentcolor', 'white')
self.master.config(bg='white') self.master.config(bg='white')
self.master.attributes('-topmost', True) self.master.attributes('-topmost', True)
self.canvas = tk.Canvas(self.master, height=self.image.height, width=self.image.width) self.canvas = tk.Canvas(self.master, height=self.image.height, width=self.image.width)
@ -53,6 +55,8 @@ class PinPicture(tk.Frame):
self.arrowBtn = tk.Button(text='箭头', command=self.set_arrow) self.arrowBtn = tk.Button(text='箭头', command=self.set_arrow)
self.lineBtn = tk.Button(text='直线', command=self.set_line) self.lineBtn = tk.Button(text='直线', command=self.set_line)
self.pinBtn = tk.Button(text='确定', command=self.pin_mode_event) self.pinBtn = tk.Button(text='确定', command=self.pin_mode_event)
self.drawModeBtn = tk.Button(text="编辑", command=self.draw_mode_event)
self.detectBtn = tk.Button(text="识别", command=self.detect)
self.master.bind("<Escape>", self.close) self.master.bind("<Escape>", self.close)
self.master.bind("<Enter>", self.enter_canvas) self.master.bind("<Enter>", self.enter_canvas)
@ -69,7 +73,7 @@ class PinPicture(tk.Frame):
def on_save(self, event): def on_save(self, event):
output = io.BytesIO() output = io.BytesIO()
self.image.convert("RGB").save(output, "BMP") self.image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:] # BMP 文件头前14字节 data = output.getvalue()[14:] # BMP 文件头前14bit
output.close() output.close()
win32clipboard.OpenClipboard() win32clipboard.OpenClipboard()
try: try:
@ -112,16 +116,15 @@ class PinPicture(tk.Frame):
# 鼠标进入 # 鼠标进入
def enter_canvas(self, event): def enter_canvas(self, event):
self.canvas.focus_set() self.canvas.focus_set()
if self.drawModeBtn is None and not self.drawMode: if not self.drawMode:
self.drawModeBtn = tk.Button(text="编辑", command=self.draw_mode_event)
self.drawModeBtn.pack(side=tk.TOP, expand=True)
elif not self.drawMode:
self.drawModeBtn.pack(side=tk.TOP, expand=True) self.drawModeBtn.pack(side=tk.TOP, expand=True)
self.detectBtn.pack(side=tk.TOP, expand=True)
# 鼠标离开 # 鼠标离开
def leave(self, event): def leave(self, event):
if self.drawModeBtn is not None and not self.drawMode: if self.drawModeBtn is not None and not self.drawMode:
self.drawModeBtn.pack_forget() self.drawModeBtn.pack_forget()
self.detectBtn.pack_forget()
# 进入编辑模式 # 进入编辑模式
def draw_mode_event(self): def draw_mode_event(self):
@ -262,7 +265,7 @@ class PinPicture(tk.Frame):
cosa = (x2 - x1) / l cosa = (x2 - x1) / l
sina = (y2 - y1) / l sina = (y2 - y1) / l
d = self.arrowSize * (sina + cosa) d = self.arrowSize * (sina + cosa)
if (sina < cosa): if sina < cosa:
xa = x2 - d xa = x2 - d
ya = y2 + math.sqrt(2 * self.arrowSize * self.arrowSize - d * d) ya = y2 + math.sqrt(2 * self.arrowSize * self.arrowSize - d * d)
xb = x2 - math.sqrt(2 * self.arrowSize * self.arrowSize - d * d) xb = x2 - math.sqrt(2 * self.arrowSize * self.arrowSize - d * d)
@ -291,3 +294,11 @@ class PinPicture(tk.Frame):
self.image = Image.alpha_composite(self.image, self.pad) self.image = Image.alpha_composite(self.image, self.pad)
self.pad = Image.new('RGBA', self.image.size) self.pad = Image.new('RGBA', self.image.size)
self.drawPad = ImageDraw.Draw(self.pad) self.drawPad = ImageDraw.Draw(self.pad)
def detect(self):
reader = easyocr.Reader(['ch_sim', 'en'])
result = reader.readtext(np.array(self.image), detail=0)
text = ''
for str in result:
text += str
print (text)

Binary file not shown.