Skip to content

Commit

Permalink
将超时判定时间的常量替换为可修改的配置项
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Jun 19, 2023
1 parent 43116f0 commit ae1dd18
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
10 changes: 4 additions & 6 deletions ocr/api_ppocr_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@
from sys import platform as sysPlatform # popen静默模式
from json import loads as jsonLoads, dumps as jsonDumps

InitTimeout = 15 # 初始化超时时间,秒


class OcrAPI:
"""调用OCR"""

def __init__(self, exePath, configPath="", argsStr=""):
def __init__(self, exePath, configPath="", argsStr="", initTimeout=20):
"""初始化识别器。\n
:exePath: 识别器`PaddleOCR_json.exe`的路径。\n
:configPath: 配置文件`PaddleOCR_json_config_XXXX.txt`的路径。\n
:argument: 启动参数,字符串。参数说明见\n
:initTimeout: 初始化超时时间,秒\n
`https://github.com/hiroi-sora/PaddleOCR-json#5-%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF%E8%AF%B4%E6%98%8E`\n
"""
cwd = os.path.abspath(os.path.join(exePath, os.pardir)) # 获取exe父文件夹
Expand Down Expand Up @@ -55,9 +53,9 @@ def cancelTimeout():
checkTimer.cancel()

def checkTimeout():
self.initErrorMsg = f'OCR init timeout: {InitTimeout}s.\n{exePath}'
self.initErrorMsg = f'OCR init timeout: {initTimeout}s.\n{exePath}'
self.ret.kill() # 关闭子进程
checkTimer = threading.Timer(InitTimeout, checkTimeout)
checkTimer = threading.Timer(initTimeout, checkTimeout)
checkTimer.start()

# 循环读取,检查成功标志
Expand Down
2 changes: 1 addition & 1 deletion ocr/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def start(self):
try:
Log.info(f'启动引擎,参数:{info}')
self.__setEngFlag(EngFlag.initing) # 通知启动中
self.ocr = OcrAPI(*self.__ocrInfo) # 启动引擎
self.ocr = OcrAPI(*self.__ocrInfo, initTimeout=Config.get('ocrInitTimeout')) # 启动引擎
# 检查启动引擎这段时间里,引擎有没有被叫停
if not self.engFlag == EngFlag.initing: # 状态被改变过了
Log.info(f'初始化后,引擎被叫停!{self.engFlag}')
Expand Down
13 changes: 12 additions & 1 deletion ui/win_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,20 @@ def initOcrUI(): # OCR引擎设置
# 隐藏高级选项:引擎管理策略
Widget.comboboxFrame(fr1, '引擎管理策略:', 'ocrRunMode', self.lockWidget
).grid(column=0, row=6, columnspan=2, sticky='we')
# 隐藏高级选项:引擎启动超时
fInit = tk.Frame(fr1)
fInit.grid(column=0, row=7, columnspan=2,
sticky='we', pady=2)
self.balloon.bind(
fInit, '引擎启动时,超过该时限未完成初始化,判定为启动失败')
tk.Label(fInit, text='初始化超时判定:').pack(side='left')
tk.Entry(fInit, width=5,
textvariable=Config.getTK('ocrInitTimeout')).pack(side='left')
tk.Label(fInit, text='秒').pack(side='left')

# 隐藏高级选项:自动清理内存
fRam = tk.Frame(fr1)
fRam.grid(column=0, row=7, columnspan=2,
fRam.grid(column=0, row=8, columnspan=2,
sticky='we', pady=2)
tk.Label(fRam, text='自动清理内存: 占用超过').pack(side='left')
wid = tk.Entry(
Expand Down
5 changes: 5 additions & 0 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ class WindowTopModeFlag():
'isSave': True,
'isTK': True,
},
'ocrInitTimeout': { # 初始化超时时间,秒
'default': 20.0,
'isSave': True,
'isTK': True,
},
'imageSuffix': { # 图片后缀
'default': '.jpg .jpe .jpeg .jfif .png .webp .bmp .tif .tiff',
'isSave': True,
Expand Down

0 comments on commit ae1dd18

Please sign in to comment.