From 85bc1638610712ef9b248247fb601b4f81195de2 Mon Sep 17 00:00:00 2001 From: MoeShin Date: Fri, 31 Mar 2023 22:52:25 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9D=A5=E8=87=AA=E5=AE=9A=E4=B9=89=20OCR=20?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit ecc9e2554665cca8a1d23f469e17c74e54790b20) --- app.py | 15 ++++++++++++++- translator/ocr/dango.py | 2 +- ui/settin.py | 8 ++++---- utils/test.py | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 7dc0a1b6..e42724b6 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,5 @@ +import subprocess + from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * @@ -184,6 +186,17 @@ def clickSettin(self) : self.range_ui.close() self.settin_ui.show() + # 启动本地 OCR + def startLocalOCR(self): + args = ["START"] + cmd = self.yaml["ocr_cmd_path"] + if isinstance(cmd, list): + for arg in cmd: + args.append(str(arg)) + else: + args.append(str(cmd)) + args.append(str(self.yaml["port"])) + subprocess.Popen(args, shell=True) # 自动打开本地OCR def autoOpenOfflineOCR(self) : @@ -193,7 +206,7 @@ def autoOpenOfflineOCR(self) : if not utils.port.detectPort(self.yaml["port"]) : try : # 启动本地OCR - os.startfile(self.yaml["ocr_cmd_path"]) + self.startLocalOCR() except Exception : self.logger.error(format_exc()) diff --git a/translator/ocr/dango.py b/translator/ocr/dango.py index 995a00c4..3fbb5a49 100644 --- a/translator/ocr/dango.py +++ b/translator/ocr/dango.py @@ -324,7 +324,7 @@ def offlineOCR(object) : # except Exception : # object.logger.error(format_exc()) - url = "http://127.0.0.1:6666/ocr/api" + url = "http://127.0.0.1:%d/ocr/api" % object.yaml["port"] language = object.config["language"] body = { "ImagePath": image_path, diff --git a/ui/settin.py b/ui/settin.py index e0490a68..e51eb7a4 100644 --- a/ui/settin.py +++ b/ui/settin.py @@ -2215,13 +2215,13 @@ def runOfflineOCR(self) : utils.message.MessageBox("本地OCR运行失败", "本地OCR已启动, 请不要重复运行! ") else : - try : + try: # 启动本地OCR - os.startfile(self.object.yaml["ocr_cmd_path"]) - except FileNotFoundError : + self.object.startLocalOCR() + except FileNotFoundError: utils.message.MessageBox("本地OCR运行失败", "本地OCR还未安装, 请先安装! ") - except Exception : + except Exception: self.logger.error(format_exc()) utils.message.MessageBox("本地OCR运行失败", "原因: %s"%format_exc()) diff --git a/utils/test.py b/utils/test.py index 9fa9b121..3f032d99 100644 --- a/utils/test.py +++ b/utils/test.py @@ -25,7 +25,7 @@ def testOfflineOCR(object) : QApplication.processEvents() total_time = 0 - url = "http://127.0.0.1:6666/ocr/api" + url = "http://127.0.0.1:%d/ocr/api" % object.yaml["port"] body = { "ImagePath": TEST_IMAGE_PATH, "Language": "JAP" From 00a220b72a2abd9a22957327637e39c12a79434d Mon Sep 17 00:00:00 2001 From: MoeShin Date: Fri, 31 Mar 2023 23:25:47 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=9C=AC=E5=9C=B0=20OCR?= =?UTF-8?q?=20=E6=98=AF=E5=90=A6=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit b70f8afba58b396666309c79596482d571e7a89c) --- utils/port.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/utils/port.py b/utils/port.py index 2b27b2fa..817e37c0 100644 --- a/utils/port.py +++ b/utils/port.py @@ -1,15 +1,8 @@ -import socket +import requests -def detectPort(port=6666) : - - s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) +def detectPort(port=6666): try: - s.connect(("127.0.0.1", int(port))) - s.shutdown(2) - sign = True - except Exception : - sign = False - s.close() - - return sign \ No newline at end of file + return requests.head("http://127.0.0.1:%d/ocr/api" % port, timeout=2).headers.get("Dango-OCR") == "OK" + except Exception: + return False From 55b04cd83e716b216f8d62cfbd84a6c883d74aaf Mon Sep 17 00:00:00 2001 From: MoeShin Date: Sat, 1 Apr 2023 02:45:31 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9D=A5=E8=87=AA=E5=AE=9A=E4=B9=89=20OCR=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=92=8C=E5=90=AF=E5=8A=A8=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 4010251dcad3bc330d6d7a9a8ff8b690d3f45a8f) --- app.py | 45 ++++++++++++++++++++++++++++++++--------- translator/ocr/dango.py | 2 +- ui/settin.py | 16 ++++++--------- ui/switch.py | 2 +- utils/config.py | 6 ++++-- utils/offline_ocr.py | 21 ++++++++++++------- utils/port.py | 2 ++ utils/test.py | 2 +- 8 files changed, 65 insertions(+), 31 deletions(-) diff --git a/app.py b/app.py index e42724b6..58886189 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import subprocess +import urllib.parse from PyQt5.QtWidgets import * from PyQt5.QtCore import * @@ -19,6 +20,7 @@ import utils.port import utils.update import utils.hwnd +import utils.offline_ocr import ui.login import ui.register @@ -186,28 +188,53 @@ def clickSettin(self) : self.range_ui.close() self.settin_ui.show() + # 获取本地 OCR 的端口 + def get_offline_ocr_local_port(self): + try: + url = urllib.parse.urlparse(self.yaml["offline_ocr_url"]) + except BaseException: + return None + return url.port + + # 本地 OCR 是否运行 + def is_offline_ocr_running(self, port=None): + if not self.yaml["offline_ocr_cmd"]: + return False + if not port: + port = self.get_offline_ocr_local_port() + return utils.port.detectPort(port) + + # 如果本地 OCR 正运行,则杀死进程 + def kill_offline_ocr_if_running(self): + port = self.get_offline_ocr_local_port() + if not self.is_offline_ocr_running(port): + return False + utils.offline_ocr.killOfflineOCR(port) + return True + # 启动本地 OCR - def startLocalOCR(self): + def start_offline_ocr(self): + cmd = self.yaml["offline_ocr_cmd"] + if not cmd: + return args = ["START"] - cmd = self.yaml["ocr_cmd_path"] if isinstance(cmd, list): for arg in cmd: args.append(str(arg)) else: args.append(str(cmd)) - args.append(str(self.yaml["port"])) subprocess.Popen(args, shell=True) # 自动打开本地OCR - def autoOpenOfflineOCR(self) : + def autoOpenOfflineOCR(self): - if not self.config["offlineOCR"] : + if not self.config["offlineOCR"]: return - if not utils.port.detectPort(self.yaml["port"]) : - try : + if not self.is_offline_ocr_running(): + try: # 启动本地OCR - self.startLocalOCR() - except Exception : + self.start_offline_ocr() + except Exception: self.logger.error(format_exc()) diff --git a/translator/ocr/dango.py b/translator/ocr/dango.py index 3fbb5a49..e69a1053 100644 --- a/translator/ocr/dango.py +++ b/translator/ocr/dango.py @@ -324,7 +324,7 @@ def offlineOCR(object) : # except Exception : # object.logger.error(format_exc()) - url = "http://127.0.0.1:%d/ocr/api" % object.yaml["port"] + url = object.yaml["offline_ocr_url"] language = object.config["language"] body = { "ImagePath": image_path, diff --git a/ui/settin.py b/ui/settin.py index e51eb7a4..f34499a7 100644 --- a/ui/settin.py +++ b/ui/settin.py @@ -2207,17 +2207,14 @@ def resetSwitch(self, switch_type) : # 运行本地OCR def runOfflineOCR(self) : - # 杀死本地可能在运行ocr进程 - utils.offline_ocr.killOfflineOCR(self.object.yaml["port"]) - - # 检查端口是否被占用 - if utils.port.detectPort(self.object.yaml["port"]) : + # 杀死本地可能在运行 OCR 进程并检查端口是否被占用 + if self.object.kill_offline_ocr_if_running() and self.object.is_offline_ocr_running(): utils.message.MessageBox("本地OCR运行失败", "本地OCR已启动, 请不要重复运行! ") - else : + else: try: # 启动本地OCR - self.object.startLocalOCR() + self.object.start_offline_ocr() except FileNotFoundError: utils.message.MessageBox("本地OCR运行失败", "本地OCR还未安装, 请先安装! ") @@ -2226,12 +2223,11 @@ def runOfflineOCR(self) : utils.message.MessageBox("本地OCR运行失败", "原因: %s"%format_exc()) - # 测试本地OCR - def testOfflineOCR(self) : + def testOfflineOCR(self): # 检查端口是否被占用 - if not utils.port.detectPort(self.object.yaml["port"]) : + if not self.object.is_offline_ocr_running(): utils.message.MessageBox("测试失败", "本地OCR还没运行成功,不可以进行测试 \n" "请先启动本地OCR, 并保证其运行正常") diff --git a/ui/switch.py b/ui/switch.py index fabea140..23ce7952 100644 --- a/ui/switch.py +++ b/ui/switch.py @@ -227,7 +227,7 @@ def updateValue(self): def mousePressEvent(self, event) : # 通过检查端口占用校验本地OCR是否运行成功 - sign = detectPort(self.object.yaml["port"]) + sign = self.object.is_offline_ocr_running() if not self.checked and not sign : MessageBox("本地OCR使用失败", "请先运行本地OCR, 待运行成功后再打开此开关\n" "若运行时间较长或者运行失败, 可通过交流群联系客服协助 \n" diff --git a/utils/config.py b/utils/config.py index 4f7c8c44..8fe369ee 100644 --- a/utils/config.py +++ b/utils/config.py @@ -24,8 +24,10 @@ def openConfig(logger) : "user": "", "password": "", "dict_info_url": "https://dango.c4a15wh.cn/DangoTranslate/ShowDict", - "ocr_cmd_path": ".\ocr\startOCR.cmd", - "port": 6666, + # 本地 OCR 接口地址 + "offline_ocr_url": "http://127.0.0.1:6666/ocr/api", + # 本地 OCR 启动命令,可为:字符串、数组、False。当 False 时,表示不启动 + "offline_ocr_cmd": ".\\ocr\\startOCR.cmd", } # 2022.02.19 添加新参数 diff --git a/utils/offline_ocr.py b/utils/offline_ocr.py index 0a441691..ccd4af50 100644 --- a/utils/offline_ocr.py +++ b/utils/offline_ocr.py @@ -15,20 +15,27 @@ # 安装本地ocr -def install_offline_ocr(object) : +def install_offline_ocr(object): + + cmd = object.yaml["offline_ocr_cmd"] + if cmd is False: + return + + if isinstance(cmd, list) and len(cmd): + cmd = cmd[0] # 判断本地ocr是否已安装 - if os.path.exists(object.yaml["ocr_cmd_path"]) : + if os.path.exists(cmd): utils.message.MessageBox("安装失败", "本地OCR已安装, 不需要重复安装! ") return - try : + try: # 杀死本地ocr进程 - killOfflineOCR(object.yaml["port"]) + object.kill_offline_ocr_is_running() # 删除旧文件 shutil.rmtree(OCR_INSTALL_PATH) - except Exception : + except Exception: object.logger.error(format_exc()) object.settin_ui.progress_bar.finish_sign = False @@ -67,10 +74,10 @@ def whether_uninstall_offline_ocr(object) : # 卸载本地ocr -def uninstall_offline_ocr(object) : +def uninstall_offline_ocr(object): # 杀死本地ocr进程解除占用 - killOfflineOCR(object.yaml["port"]) + object.kill_offline_ocr_if_running() # 删除本地ocr try: diff --git a/utils/port.py b/utils/port.py index 817e37c0..6da17b88 100644 --- a/utils/port.py +++ b/utils/port.py @@ -2,6 +2,8 @@ def detectPort(port=6666): + if not port: + return False try: return requests.head("http://127.0.0.1:%d/ocr/api" % port, timeout=2).headers.get("Dango-OCR") == "OK" except Exception: diff --git a/utils/test.py b/utils/test.py index 3f032d99..221dcdc7 100644 --- a/utils/test.py +++ b/utils/test.py @@ -25,7 +25,7 @@ def testOfflineOCR(object) : QApplication.processEvents() total_time = 0 - url = "http://127.0.0.1:%d/ocr/api" % object.yaml["port"] + url = object.yaml["offline_ocr_url"] body = { "ImagePath": TEST_IMAGE_PATH, "Language": "JAP" From 00278382ce2d22490d97b767e945a870c7a29ca6 Mon Sep 17 00:00:00 2001 From: MoeShin Date: Sun, 2 Apr 2023 11:40:43 +0800 Subject: [PATCH 4/8] =?UTF-8?q?POST=20=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 100e77c158a053f5c8d423e6ab998e91cba7f60e) --- utils/http.py | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/utils/http.py b/utils/http.py index 94ce7242..84eeeb44 100644 --- a/utils/http.py +++ b/utils/http.py @@ -6,7 +6,7 @@ # 发送http请求 -def post(url, body, logger, headers=None, timeout=5) : +def post(url, body, logger, headers=None, timeout=5, files=None): proxies = { "http": None, @@ -14,34 +14,33 @@ def post(url, body, logger, headers=None, timeout=5) : } result = {} - try : + try: # 消除https警告 requests.packages.urllib3.disable_warnings() - except Exception : + except Exception: pass response = None - try : - if headers : - response = requests.post(url, headers=headers, data=json.dumps(body), proxies=proxies, verify=False, timeout=timeout) - else : - response = requests.post(url, data=json.dumps(body), proxies=proxies, verify=False, timeout=timeout) - try : + try: + if not files: + body = json.dumps(body) + response = requests.post(url, headers=headers, data=body, files=files, proxies=proxies, + verify=False, timeout=timeout) + try: response.encoding = "utf-8" result = json.loads(response.text) response.close() - except Exception : + except Exception: response.encoding = "gb18030" result = json.loads(response.text) - except json.decoder.JSONDecodeError : - try : - logger.error("post %s error, httpcode is %s, response is %s"%(url, response.status_code, response.text)) - except Exception : + except json.decoder.JSONDecodeError: + try: + logger.error("post %s error, httpcode is %s, response is %s" % (url, response.status_code, response.text)) + except Exception: pass - except Exception : + except Exception: logger.error(format_exc()) - return result From cb4dc1931ab724a9905320a768ec3178c37fdafe Mon Sep 17 00:00:00 2001 From: MoeShin Date: Sun, 2 Apr 2023 11:54:58 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=20OCR=20=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit bf499aab9939c5ae98accc79bdf0ca8358fd4c4e) --- app.py | 15 ++++++++++----- translator/ocr/dango.py | 13 ++++++++++--- utils/test.py | 23 +++++++++++++++++------ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index 58886189..2442efb4 100644 --- a/app.py +++ b/app.py @@ -196,13 +196,18 @@ def get_offline_ocr_local_port(self): return None return url.port + # 本地 OCR 是否本机运行 + def is_local_offline_ocr(self): + return not not self.yaml["offline_ocr_cmd"] + # 本地 OCR 是否运行 - def is_offline_ocr_running(self, port=None): - if not self.yaml["offline_ocr_cmd"]: + def is_offline_ocr_running(self, local_port=None): + require_local = not not local_port + if require_local and not self.is_local_offline_ocr(): return False - if not port: - port = self.get_offline_ocr_local_port() - return utils.port.detectPort(port) + if not local_port: + local_port = self.get_offline_ocr_local_port() + return utils.port.detectPort(local_port) # 如果本地 OCR 正运行,则杀死进程 def kill_offline_ocr_if_running(self): diff --git a/translator/ocr/dango.py b/translator/ocr/dango.py index e69a1053..f47a072b 100644 --- a/translator/ocr/dango.py +++ b/translator/ocr/dango.py @@ -315,7 +315,7 @@ def dangoOCR(object, test=False) : # 本地OCR -def offlineOCR(object) : +def offlineOCR(object): image_path = os.path.join(os.getcwd(), "config", "image.jpg") # try : @@ -327,11 +327,18 @@ def offlineOCR(object) : url = object.yaml["offline_ocr_url"] language = object.config["language"] body = { - "ImagePath": image_path, "Language": language } - res = utils.http.post(url, body, object.logger) + if object.is_local_offline_ocr(): + body["ImagePath"] = image_path + res = utils.http.post(url, body, object.logger) + else: + with open(image_path, 'rb') as file: + res = utils.http.post(url, body, object.logger, files={ + 'image': file + }) + if not res : return False, "本地OCR错误: 本地OCR所使用的端口可能被占用, 请重启电脑以释放端口后重试\n如果频繁出现, 建议切换其他OCR使用" diff --git a/utils/test.py b/utils/test.py index 221dcdc7..e0bd9794 100644 --- a/utils/test.py +++ b/utils/test.py @@ -1,3 +1,5 @@ +import urllib.parse + from PyQt5.QtWidgets import * import time import os @@ -15,7 +17,7 @@ # 测试本地OCR -def testOfflineOCR(object) : +def testOfflineOCR(object): # 测试信息显示窗 object.settin_ui.desc_ui = ui.desc.Desc(object) @@ -26,21 +28,30 @@ def testOfflineOCR(object) : total_time = 0 url = object.yaml["offline_ocr_url"] + image_path = TEST_IMAGE_PATH body = { - "ImagePath": TEST_IMAGE_PATH, - "Language": "JAP" + "Language": "JAP", } + is_local = object.is_local_offline_ocr() + if is_local: + body["ImagePath"] = image_path - for num in range(1, 6) : + for num in range(1, 6): start = time.time() # try : # translator.ocr.dango.imageBorder(TEST_IMAGE_PATH, NEW_TEST_IMAGE_PATH, "a", 20, color=(255, 255, 255)) # except Exception : # body["ImagePath"] = TEST_IMAGE_PATH - res = utils.http.post(url, body, object.logger) + if is_local: + res = utils.http.post(url, body, object.logger) + else: + with open(image_path, 'rb') as file: + res = utils.http.post(url, body, object.logger, files={ + 'image': file + }) end = time.time() total_time += end-start - if res.get("Code", -1) == 0 : + if res.get("Code", -1) == 0: object.settin_ui.desc_ui.desc_text.append("\n第{}次, 成功, 耗时{:.2f}s".format(num, (end - start))) else: object.settin_ui.desc_ui.desc_text.append("\n第{}次, 失败".format(num)) From 8aef5c678d2b9d228a9e424034d3e09096d65b74 Mon Sep 17 00:00:00 2001 From: MoeShin Date: Sun, 2 Apr 2023 12:21:08 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=81=A2=E5=A4=8D=20detectPort?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit c69524fae518224cfc0f0f4708da74e3e677a8b6) --- utils/port.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/utils/port.py b/utils/port.py index 6da17b88..f8683649 100644 --- a/utils/port.py +++ b/utils/port.py @@ -1,10 +1,15 @@ -import requests +import socket def detectPort(port=6666): - if not port: - return False + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: - return requests.head("http://127.0.0.1:%d/ocr/api" % port, timeout=2).headers.get("Dango-OCR") == "OK" + s.connect(("127.0.0.1", int(port))) + s.shutdown(2) + sign = True except Exception: - return False + sign = False + s.close() + + return sign From abfb60b47bae77e99992a843fe43030252ed455d Mon Sep 17 00:00:00 2001 From: MoeShin Date: Mon, 3 Apr 2023 20:32:27 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=20OCR=20.cmd=20=E6=96=87=E4=BB=B6=E5=92=8C?= =?UTF-8?q?=E6=9D=80=E6=AD=BB=E8=BF=9B=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 55848734111a8a960272b94fd52251c7a497f410) --- app.py | 68 ++++++++++++++++++-------------------------- ui/settin.py | 14 +++++---- utils/offline_ocr.py | 41 +++++++++++++++++++++----- 3 files changed, 71 insertions(+), 52 deletions(-) diff --git a/app.py b/app.py index 2442efb4..0eee67fa 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ import subprocess -import urllib.parse +import requests from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * @@ -188,60 +188,48 @@ def clickSettin(self) : self.range_ui.close() self.settin_ui.show() - # 获取本地 OCR 的端口 - def get_offline_ocr_local_port(self): - try: - url = urllib.parse.urlparse(self.yaml["offline_ocr_url"]) - except BaseException: - return None - return url.port - # 本地 OCR 是否本机运行 def is_local_offline_ocr(self): return not not self.yaml["offline_ocr_cmd"] # 本地 OCR 是否运行 - def is_offline_ocr_running(self, local_port=None): - require_local = not not local_port - if require_local and not self.is_local_offline_ocr(): - return False - if not local_port: - local_port = self.get_offline_ocr_local_port() - return utils.port.detectPort(local_port) - - # 如果本地 OCR 正运行,则杀死进程 - def kill_offline_ocr_if_running(self): - port = self.get_offline_ocr_local_port() - if not self.is_offline_ocr_running(port): + def is_offline_ocr_running(self): + try: + return requests.head(self.yaml["offline_ocr_url"], timeout=5).headers.get("Dango-OCR") == "OK" + except Exception: return False - utils.offline_ocr.killOfflineOCR(port) - return True # 启动本地 OCR def start_offline_ocr(self): - cmd = self.yaml["offline_ocr_cmd"] - if not cmd: + if not self.is_local_offline_ocr(): return - args = ["START"] - if isinstance(cmd, list): - for arg in cmd: - args.append(str(arg)) + args = ["START", "离线 OCR"] + cmd = self.yaml["offline_ocr_cmd"] + + is_list = isinstance(cmd, list) + if is_list: + file = str(cmd[0]) else: - args.append(str(cmd)) + file = str(cmd) + _, ext = os.path.splitext(file) + if ext.lower() == ".cmd": + args.append("ConHost") + args.append(file) + + if is_list: + for arg in cmd[1:]: + args.append(str(arg)) subprocess.Popen(args, shell=True) - # 自动打开本地OCR + # 自动打开本地 OCR def autoOpenOfflineOCR(self): - - if not self.config["offlineOCR"]: + if not self.config["offlineOCR"] or self.is_offline_ocr_running(): return - if not self.is_offline_ocr_running(): - try: - # 启动本地OCR - self.start_offline_ocr() - except Exception: - self.logger.error(format_exc()) - + try: + # 启动本地 OCR + self.start_offline_ocr() + except Exception: + self.logger.error(format_exc()) # 初始化资源 def InitLoadFile(self) : diff --git a/ui/settin.py b/ui/settin.py index f34499a7..fd27cc15 100644 --- a/ui/settin.py +++ b/ui/settin.py @@ -2203,17 +2203,21 @@ def resetSwitch(self, switch_type) : self.online_ocr_probation_switch.mousePressEvent(1) self.online_ocr_probation_switch.updateValue() - # 运行本地OCR - def runOfflineOCR(self) : + def runOfflineOCR(self): + if not self.object.is_local_offline_ocr(): + return + + # 杀死可能在运行的本地 OCR + utils.offline_ocr.killOfflineOCR(self.object) - # 杀死本地可能在运行 OCR 进程并检查端口是否被占用 - if self.object.kill_offline_ocr_if_running() and self.object.is_offline_ocr_running(): + # 本地 OCR 是否运行 + if self.object.is_offline_ocr_running(): utils.message.MessageBox("本地OCR运行失败", "本地OCR已启动, 请不要重复运行! ") else: try: - # 启动本地OCR + # 启动本地 OCR self.object.start_offline_ocr() except FileNotFoundError: utils.message.MessageBox("本地OCR运行失败", diff --git a/utils/offline_ocr.py b/utils/offline_ocr.py index ccd4af50..26962640 100644 --- a/utils/offline_ocr.py +++ b/utils/offline_ocr.py @@ -1,3 +1,5 @@ +import subprocess + from PyQt5.QtCore import * from traceback import format_exc import shutil @@ -51,12 +53,37 @@ def install_offline_ocr(object): # 杀死本地ocr进程 -def killOfflineOCR(port) : - - popen_content = os.popen("netstat -ano |findstr %s"%port).read() - if popen_content : - pid = popen_content.split(" ")[-1] - os.popen("taskkill /f /t /im %s"%pid) +def killOfflineOCR(object): + # path = ".\\ocr\\startOCR.cmd" + # path = os.path.realpath(path) + # path = path.replace("\\", "\\\\") + # subprocess.Popen(["WMIC", "PROCESS", "WHERE", "ExecutablePath='%s'" % path, "CALL", "Terminate"], shell=True)\ + # .wait() + + if not object.is_local_offline_ocr(): + return + cmd = object.yaml["offline_ocr_cmd"] + if not isinstance(cmd, list): + cmd = [cmd] + _, ext = os.path.splitext(str(cmd[0])) + is_con_host = ext.lower() == ".cmd" + + arr = [] + for v in cmd: + v = str(v) + if " " in v: + v = "\"" + v + "\"" + arr.append(v.replace("\\", "\\\\")) + + cl = arr[0] + if len(arr) > 1: + if is_con_host: + space = ' ' + else: + space = ' ' + cl += space + ' '.join(arr[1:]) + subprocess.Popen(["WMIC", "PROCESS", "WHERE", "CommandLine='%s'" % cl, "CALL", "Terminate"], shell=True)\ + .wait() # 是否卸载本地ocr @@ -77,7 +104,7 @@ def whether_uninstall_offline_ocr(object) : def uninstall_offline_ocr(object): # 杀死本地ocr进程解除占用 - object.kill_offline_ocr_if_running() + utils.offline_ocr.killOfflineOCR(object) # 删除本地ocr try: From c0d8124ebf9dd136f7c8faf8031a99cb9c1849c1 Mon Sep 17 00:00:00 2001 From: MoeShin Date: Tue, 4 Apr 2023 08:46:52 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=20OCR=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=82=E6=95=B0=E5=90=8D=E7=9A=84=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E4=B8=BA=E5=A4=A7=E9=A9=BC=E5=B3=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 106641e59121e185aa975cb8e15bff16a3ff46d1) --- translator/ocr/dango.py | 2 +- utils/test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/translator/ocr/dango.py b/translator/ocr/dango.py index f47a072b..30ac3391 100644 --- a/translator/ocr/dango.py +++ b/translator/ocr/dango.py @@ -336,7 +336,7 @@ def offlineOCR(object): else: with open(image_path, 'rb') as file: res = utils.http.post(url, body, object.logger, files={ - 'image': file + 'Image': file }) if not res : diff --git a/utils/test.py b/utils/test.py index e0bd9794..6ca37574 100644 --- a/utils/test.py +++ b/utils/test.py @@ -47,7 +47,7 @@ def testOfflineOCR(object): else: with open(image_path, 'rb') as file: res = utils.http.post(url, body, object.logger, files={ - 'image': file + 'Image': file }) end = time.time() total_time += end-start