diff --git a/README.md b/README.md index d4df76b..d175ac3 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ docker run -d -p 6666:6666 m986883511/extract_subtitles 后端接口容器地址[Docker Hub](https://hub.docker.com/repository/docker/m986883511/extract_subtitles) -此过程可能时间较长,您需要预先安装好好docker,并配置好docker加速器 +此过程可能时间较长,您需要预先安装好好docker,并配置好docker加速器,你可能需要先docker login ```shell docker run -d -p 6666:6666 m986883511/extract_subtitles diff --git a/config.json b/config.json new file mode 100644 index 0000000..8cf93fc --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "ip": "127.0.0.1", + "port": "6666" +} diff --git a/image/2-run-exe.png b/image/2-run-exe.png index e6d3762..ba5ccff 100644 Binary files a/image/2-run-exe.png and b/image/2-run-exe.png differ diff --git a/interface.py b/interface.py index fc4d427..8694803 100644 --- a/interface.py +++ b/interface.py @@ -1,5 +1,5 @@ +import json import os - import requests from requests_toolbelt.multipart.encoder import MultipartEncoder @@ -10,11 +10,22 @@ class ExtractSubtitleApi: debug = True def __init__(self): - self.ip = ExtractSubtitleApi.ip - self.port = ExtractSubtitleApi.port + self.set_ip_port() self.headers = {'content-type': 'application/json'} self.print_flag = True + def set_ip_port(self): + try: + with open(os.path.join(os.path.dirname(__file__), 'config.json')) as f: + content = f.read() + content = json.loads(content) + self.ip = content['ip'] + self.port = content['port'] + except Exception as e: + print('read config.json failed, err={}'.format(e)) + self.ip = ExtractSubtitleApi.ip + self.port = ExtractSubtitleApi.port + @property def base_url(self): return "http://{}:{}".format(self.ip, self.port) @@ -22,7 +33,7 @@ def base_url(self): def text_recognition(self, base64_img): url = '{}/text_recognition'.format(self.base_url) data = {"image": [base64_img]} - res = requests.post(url, data=data) + res = requests.post(url, data=data) return self.deal_with_response(res) def extract_human_voice_from_sound(self, local_mp3_filepath): @@ -43,6 +54,7 @@ def get_human_voice_time_point(self, remote_wav_filename): return self.deal_with_response(res) def detect_recognition_model(self): + self.set_ip_port() url = '{}/load_model'.format(self.base_url) response = requests.get(url, headers=self.headers) return self.deal_with_response(response) diff --git a/main.py b/main.py index 5a2a33e..1593723 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ import base64 current_dir = os.path.dirname(__file__) -sys.path.append(os.path.join(current_dir, 'ffmpeg')) +os.environ['PATH'] += ';' + os.path.join(current_dir, 'ffmpeg') import cv2 import srt @@ -340,15 +340,16 @@ def __deal_with_process_output_string(self, input_str): def test_connect_api(self): self.add_log('开始测试api接口是否连通') - value = self.api_interface.detect_recognition_model() - if isinstance(value, dict) and value.get('result') == 'load model success': - msg = '文字识别API接口已连通:{}'.format(self.api_interface.base_url) - self.add_log(msg) + try: + value = self.api_interface.detect_recognition_model() + if isinstance(value, dict) and value.get('result') == 'load model success': + msg = '文字识别API接口已连通:{}'.format(self.api_interface.base_url) + self.add_log(msg) + self.connect_api_lineedit.setText(msg) + except Exception as e: + msg = '无法连接文字识别API接口,检查容器状态后重试' + self.add_log('{}, err={}'.format(msg, e)) self.connect_api_lineedit.setText(msg) - return - msg = '无法连接文字识别API接口' - self.add_log(msg) - self.connect_api_lineedit.setText(msg) def update_progress_bar(self, progress): self.extract_progress.setValue(progress)