Skip to content

Commit

Permalink
新特性:无需本地缓存,直接识别剪贴板内存中的图片
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Sep 19, 2022
1 parent 4b7b1f5 commit c190f02
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ocr/msn_batch_paths.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 批量路径 任务处理器

from utils.config import Config
from ocr.engine import MsnFlag
from ocr.msn import Msn
Expand Down
55 changes: 55 additions & 0 deletions ocr/msn_quick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 单次快捷任务 任务处理器

from utils.config import Config
from ocr.engine import MsnFlag
from ocr.msn import Msn
from ocr.output_panel import OutputPanel # 输出器

import tkinter as tk
import time
from pyperclip import copy as pyperclipCopy

from utils.logger import GetLog
Log = GetLog()


class MsnQuick(Msn):

# __init__ 在主线程内初始化,其余方法在子线程内被调用
def __init__(self):
# 获取接口
self.progressbar = Config.main.progressbar # 进度条组件
self.setRunning = Config.main.setRunning # 设置运行状态接口
self.outputPanel = OutputPanel() # 输出到面板
Log.info(f'快捷文本处理器初始化完毕!')

def onStart(self, num):
# 重置进度提示
self.setRunning(MsnFlag.running) # 先设running,再设进度条动画来覆盖
self.progressbar["maximum"] = 50 # 重置进度条长度,值越小加载动画越快
self.progressbar['mode'] = 'indeterminate' # 进度条为来回动模式
self.progressbar.start() # 进度条开始加载动画
Config.set('tipsTop2', '快捷识图中……')
# 输出初始信息
startStr = f"\n快捷识图开始:{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))}\n\n"
self.outputPanel.print(startStr)

def onGet(self, numData, ocrData):
# ==================== 分析文块 ====================
if ocrData['code'] == 100: # 成功
tbList = ocrData['data'] # 获取文块
tbStr = ''
for tb in tbList:
tbStr += tb['text']+'\n'
self.outputPanel.print(tbStr+'\n') # 输出到面板
if Config.get('isNeedCopy'): # 需要复制
pyperclipCopy(tbStr) # 复制到剪贴板
self.outputPanel.print('文字已复制到剪贴板\n')
elif ocrData['code'] == 101: # 无文字
self.outputPanel.print('未发现文字\n')
else: # 识别失败
self.outputPanel.print(
f'识别失败,错误码:{ocrData["code"]}\n错误信息:{str(ocrData["data"])}\n')

def onStop(self, num):
self.setRunning(MsnFlag.none)
33 changes: 15 additions & 18 deletions ui/win_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
from ui.widget import Widget # 控件
from ui.pmw.PmwBalloon import Balloon # 气泡提示
from ocr.engine import OCRe, MsnFlag, EngFlag # 引擎
# 识图任务处理器
from ocr.msn_batch_paths import MsnBatch
from ocr.msn_quick import MsnQuick

import os
import time
import keyboard # 绑定快捷键
from PIL import Image, ImageGrab # 图像,剪贴板
import tkinter as tk
import tkinter.filedialog
Expand Down Expand Up @@ -242,7 +243,8 @@ def testFunc1():
testFunc1).pack(side='top', fill='x', padx=4)
Widget.hotkeyFrame(quickLabel, '读剪贴板', 'Screenshot',
self.runClipboard).pack(side='top', fill='x', padx=4)
keyboard.add_hotkey('9+0', testFunc1, suppress=False)
tk.Checkbutton(quickLabel, variable=Config.getTK('isNeedCopy'),
text='复制识别出的文字').pack(side='left', fill='x', padx=4)
quickOCR()

def initInFile(): # 输入设置
Expand Down Expand Up @@ -602,7 +604,7 @@ def setNone():
Config.set('tipsTop2', '已结束')
return 'normal'

def setIniting():
def initing():
self.btnRun['text'] = '停止任务'
self.btnRun['state'] = 'normal'
Config.set('tipsTop2', '初始化')
Expand All @@ -611,28 +613,25 @@ def setIniting():
self.progressbar.start() # 进度条开始加载动画
return 'disable'

def setRunning():
def running():
self.progressbar.stop() # 进度条停止加载动画
self.progressbar['mode'] = 'determinate' # 进度条静止模式
return ''

def setStopping():
def stopping():
self.btnRun['text'] = '正在停止'
self.btnRun['state'] = 'disable'
if str(self.progressbar["mode"]) == 'indeterminate':
self.progressbar.stop() # 进度条停止加载动画
self.progressbar['mode'] = 'determinate' # 进度条静止模式
return ''

def setDefault():
return ''

state = {
MsnFlag.none: setNone,
MsnFlag.initing: setIniting,
MsnFlag.running: setRunning,
MsnFlag.stopping: setStopping,
}.get(batFlag, setDefault)()
MsnFlag.initing: initing,
MsnFlag.running: running,
MsnFlag.stopping: stopping,
}.get(batFlag, '')()
if state:
for w in self.lockWidget: # 改变组件状态(禁用,启用)
if 'widget' in w.keys() and 'stateOFnormal' in w.keys():
Expand Down Expand Up @@ -681,20 +680,18 @@ def runClipboard(self, e=None): # 识别剪贴板
self.clearTable() # 清空主表
self.addImagesList(clipData) # 添加到主表
self.run() # 开始任务任务
return

# 剪贴板中是图片
elif isinstance(clipData, Image.Image):
# 初始化文本处理器
try:
msnBat = MsnBatch()
try: # 初始化快捷识图任务处理器
msnQui = MsnQuick()
except Exception as err:
tk.messagebox.showwarning('遇到了亿点小问题', f'{err}')
return # 未开始运行,终止本次运行
# 开始运行
OCRe.runMission(['clipboard'], msnBat)
OCRe.runMission(['clipboard'], msnQui)
self.notebook.select(self.notebookTab[1]) # 转到输出卡
self.gotoTop() # 主窗置顶
return # TODO

def onClose(self): # 关闭窗口事件
OCRe.stop() # 强制关闭引擎进程,加快子线程结束
Expand Down

0 comments on commit c190f02

Please sign in to comment.