Skip to content

Commit

Permalink
fix: ocr翻译服务密钥设置
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhldr committed Sep 20, 2024
1 parent f350dfb commit 3c1e97a
Show file tree
Hide file tree
Showing 18 changed files with 487 additions and 425 deletions.
8 changes: 8 additions & 0 deletions data/cool.ldr.lfy.in.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
<default>"API Key | Secret Key"</default>
<description>Baidu API Key and Secret Key for ocr, used in the middle | split</description>
</key>
<key type="s" name="server-sk-pytesseract-ocr">
<default>"chi_sim+eng"</default>
<description>The lang parameters of the pytesseract module, such as Chinese and English by default</description>
</key>
<key type="s" name="server-sk-easyocr-ocr">
<default>"ch_sim+en"</default>
<description>The lang parameters of the easyocr module, such as Chinese and English by default, split by +</description>
</key>
<key type="s" name="ocr-baidu-token">
<default>""</default>
<description>Baidu OCR access_token</description>
Expand Down
31 changes: 16 additions & 15 deletions data/resources/lfy.cmb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
(2,92,"GtkListBox","glb_compare",91,None,None,None,None,None,None),
(2,93,"AdwPreferencesGroup",None,5,None,None,None,1,None,None),
(2,94,"AdwComboRow","acr_server_ocr",93,None,None,None,-1,None,None),
(2,95,"GtkSeparator",None,94,None,None,None,None,None,None),
(2,96,"GtkButton","btn_config_server_ocr",94,None,None,None,1,None,None),
(6,1,"GtkShortcutsWindow","help_overlay",None,None,None,None,None,None,None),
(6,2,"GtkShortcutsSection",None,1,None,None,None,None,None,None),
(6,3,"GtkShortcutsGroup","general",2,None,None,None,None,None,None),
Expand All @@ -89,10 +91,6 @@
(10,20,"GtkStack","api_key_stack",19,None,None,None,None,None,None),
(10,21,"GtkStackPage",None,20,None,None,None,None,None,None),
(10,22,"GtkSpinner","api_key_spinner",21,None,None,None,None,None,None),
(10,24,"AdwEntryRow","api_key_ocr_entry",18,None,None,None,2,None,None),
(10,25,"GtkStack","api_key_ocr_stack",24,None,None,None,None,None,None),
(10,26,"GtkStackPage",None,25,None,None,None,None,None,None),
(10,27,"GtkSpinner","api_key_ocr_spinner",26,None,None,None,None,None,None),
(10,28,"GtkLinkButton","api_key_link",18,None,None,None,2,None,None),
(12,1,"GtkBox","ThemeSwitcher",None,None,None,None,None,None,None),
(12,2,"GtkBox","box",1,None,None,None,None,None,None),
Expand Down Expand Up @@ -164,7 +162,7 @@
(2,15,"AdwActionRow","subtitle","setup API Key for translation Server, click on the right to the details",1,None,None,None,None,None,None,None,None),
(2,15,"AdwActionRow","subtitle-lines","3",None,None,None,None,None,None,None,None,None),
(2,15,"AdwActionRow","title-lines","1",None,None,None,None,None,None,None,None,None),
(2,15,"AdwPreferencesRow","title","API Key",1,None,None,None,None,None,None,None,None),
(2,15,"AdwPreferencesRow","title","Translation keys",1,None,None,None,None,None,None,None,None),
(2,16,"GtkWidget","margin-bottom","12",None,None,None,None,None,None,None,None,None),
(2,16,"GtkWidget","margin-start","12",None,None,None,None,None,None,None,None,None),
(2,16,"GtkWidget","margin-top","12",None,None,None,None,None,None,None,None,None),
Expand Down Expand Up @@ -200,6 +198,12 @@
(2,92,"GtkListBox","selection-mode","multiple",None,None,None,None,None,None,None,None,None),
(2,94,"AdwActionRow","subtitle","local or cloud, text recognition",1,None,None,None,None,None,None,None,None),
(2,94,"AdwPreferencesRow","title","OCR server",1,None,None,None,None,None,None,None,None),
(2,95,"GtkWidget","margin-bottom","12",None,None,None,None,None,None,None,None,None),
(2,95,"GtkWidget","margin-start","12",None,None,None,None,None,None,None,None,None),
(2,95,"GtkWidget","margin-top","12",None,None,None,None,None,None,None,None,None),
(2,96,"GtkButton","icon-name","open-menu-symbolic",None,None,None,None,None,None,None,None,None),
(2,96,"GtkWidget","margin-start","8",None,None,None,None,None,None,None,None,None),
(2,96,"GtkWidget","valign","center",None,None,None,None,None,None,None,None,None),
(6,1,"GtkWindow","modal","True",None,None,None,None,None,None,None,None,None),
(6,2,"GtkShortcutsSection","max-height","10",None,None,None,None,None,None,None,None,None),
(6,2,"GtkShortcutsSection","section-name","shortcuts",None,None,None,None,None,None,None,None,None),
Expand Down Expand Up @@ -240,12 +244,6 @@
(10,21,"GtkStackPage","child",None,None,None,None,None,22,None,None,None,None),
(10,21,"GtkStackPage","name","spinner",None,None,None,None,None,None,None,None,None),
(10,22,"GtkWidget","valign","3",None,None,None,None,None,None,None,None,None),
(10,24,"AdwEntryRow","show-apply-button","true",None,None,None,None,None,None,None,None,None),
(10,24,"AdwPreferencesRow","title","API Key for OCR,Pay attention to whether there are dividing lines |",1,None,None,None,None,None,None,None,None),
(10,24,"GtkWidget","tooltip-text","Enter an API Key for the Server with OCR.",1,None,None,None,None,None,None,None,None),
(10,26,"GtkStackPage","child",None,None,None,None,None,27,None,None,None,None),
(10,26,"GtkStackPage","name","spinner",None,None,None,None,None,None,None,None,None),
(10,27,"GtkWidget","valign","3",None,None,None,None,None,None,None,None,None),
(10,28,"GtkButton","label","how to get API Key",1,None,None,None,None,None,None,None,None),
(10,28,"GtkLinkButton","uri","https://github.com/ldrfy/lfy",None,None,None,None,None,None,None,None,None),
(12,1,"GtkWidget","hexpand","true",None,None,None,None,None,None,None,None,None),
Expand Down Expand Up @@ -276,7 +274,6 @@
(77,12,5,"GObject","notify","_on_color_scheme_changed","active",None,None,None,None),
(78,10,16,"GtkButton","clicked","_on_back",None,None,None,None,None),
(79,10,19,"AdwEntryRow","apply","_on_api_key_apply",None,None,None,None,None),
(105,10,24,"AdwEntryRow","apply","_on_api_key_ocr_apply",None,None,None,None,None),
(106,2,45,"AdwEntryRow","apply","_on_vpn_apply",None,None,None,None,None),
(132,1,168,"GtkTextView","copy-clipboard","_set_tv_copy",None,None,None,None,None),
(133,1,168,"GtkTextView","cut-clipboard","_set_tv_copy",None,None,None,None,None),
Expand All @@ -287,7 +284,8 @@
(140,2,87,"GtkButton","clicked","_export_config",None,None,None,None,None),
(141,2,88,"GtkButton","clicked","_import_config",None,None,None,None,None),
(144,2,91,"GtkPopover","closed","_on_popover_closed",None,None,None,None,None),
(145,2,94,"GObject","notify","_config_select_server_ocr","selected",None,None,None,None)
(145,2,94,"GObject","notify","_config_select_server_ocr","selected",None,None,None,None),
(146,2,96,"GtkButton","clicked","_open_server_ocr",None,None,None,None,None)
</object_signal>
<object_data>
(2,17,"GtkWidget",1,1,None,None,None,None,None,None),
Expand All @@ -304,7 +302,9 @@
(12,1,"GtkWidget",1,1,None,None,None,None,None,None),
(12,1,"GtkWidget",2,2,None,1,None,None,None,None),
(1,113,"(item)",3,1,None,None,None,None,None,None),
(1,113,"(item)",1,2,"theme",1,None,None,None,None)
(1,113,"(item)",1,2,"theme",1,None,None,None,None),
(2,96,"GtkWidget",1,1,None,None,None,None,None,None),
(2,96,"GtkWidget",2,2,None,1,None,None,None,None)
</object_data>
<object_data_arg>
(2,17,"GtkWidget",2,2,"name","flat"),
Expand All @@ -315,6 +315,7 @@
(12,5,"GtkWidget",2,2,"name","theme-selector"),
(12,5,"GtkWidget",2,3,"name","dark"),
(12,1,"GtkWidget",2,2,"name","themeswitcher"),
(1,113,"(item)",1,2,"name","custom")
(1,113,"(item)",1,2,"name","custom"),
(2,96,"GtkWidget",2,2,"name","flat")
</object_data_arg>
</cambalache-project>
20 changes: 19 additions & 1 deletion data/resources/preference.ui
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<object class="AdwComboRow" id="acr_server">
<property name="subtitle" translatable="yes">setup API Key for translation Server, click on the right to the details</property>
<property name="subtitle-lines">3</property>
<property name="title" translatable="yes">API Key</property>
<property name="title" translatable="yes">Translation keys</property>
<property name="title-lines">1</property>
<signal name="notify::selected" handler="_config_select_server"/>
<child>
Expand Down Expand Up @@ -44,6 +44,24 @@
<property name="subtitle" translatable="yes">local or cloud, text recognition</property>
<property name="title" translatable="yes">OCR server</property>
<signal name="notify::selected" handler="_config_select_server_ocr"/>
<child>
<object class="GtkSeparator">
<property name="margin-bottom">12</property>
<property name="margin-start">12</property>
<property name="margin-top">12</property>
</object>
</child>
<child>
<object class="GtkButton" id="btn_config_server_ocr">
<property name="icon-name">open-menu-symbolic</property>
<property name="margin-start">8</property>
<property name="valign">center</property>
<signal name="clicked" handler="_open_server_ocr"/>
<style>
<class name="flat"/>
</style>
</object>
</child>
</object>
</child>
</object>
Expand Down
22 changes: 0 additions & 22 deletions data/resources/server-preferences.ui
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,6 @@
</child>
</object>
</child>
<child>
<object class="AdwEntryRow" id="api_key_ocr_entry">
<property name="show-apply-button">true</property>
<property name="title" translatable="yes">API Key for OCR,Pay attention to whether there are dividing lines |</property>
<property name="tooltip-text" translatable="yes">Enter an API Key for the Server with OCR.</property>
<signal name="apply" handler="_on_api_key_ocr_apply"/>
<child>
<object class="GtkStack" id="api_key_ocr_stack">
<child>
<object class="GtkStackPage">
<property name="child">
<object class="GtkSpinner" id="api_key_ocr_spinner">
<property name="valign">3</property>
</object>
</property>
<property name="name">spinner</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkLinkButton" id="api_key_link">
<property name="label" translatable="yes">how to get API Key</property>
Expand Down
27 changes: 10 additions & 17 deletions lfy/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@
"""
from gi.repository import Gtk

from lfy.api.constant import SERVERS
from lfy.api.constant import SERVERS_O, SERVERS_T
from lfy.api.server import Lang, Server

servers_t = None
servers_o = None


def get_servers_t():
"""翻译的服务
Returns:
dict: _description_
"""
global servers_t # pylint:disable=W0603
if servers_t is None:
servers_t = [s for s in SERVERS if s.can_translate]
return servers_t
return SERVERS_T


def get_servers_o():
Expand All @@ -28,10 +22,7 @@ def get_servers_o():
Returns:
dict: _description_
"""
global servers_o # pylint:disable=W0603
if servers_o is None:
servers_o = [s for s in SERVERS if s.can_ocr]
return servers_o
return SERVERS_O


def get_server_names_t():
Expand Down Expand Up @@ -68,7 +59,7 @@ def get_servers_api_key():
list: ["百度", "腾讯", ...]
"""

return [s for s in SERVERS if s.get_api_key_s() is not None]
return [s for s in SERVERS_T if s.get_api_key_s() is not None]


def get_server_names_api_key():
Expand All @@ -89,9 +80,10 @@ def get_server(i: int) -> Server:
Returns:
_type_: _description_
"""
if i >= len(SERVERS):
return SERVERS[0]
return SERVERS[i]
ss = SERVERS_T + SERVERS_O
if i >= len(ss):
return ss[0]
return ss[i]


def create_server_t(key) -> Server:
Expand All @@ -115,6 +107,7 @@ def create_server_o(key) -> Server:
_type_: _description_
"""
for s in get_servers_o():
print(s)
if s.key == key:
return s

Expand Down Expand Up @@ -145,7 +138,7 @@ def server_key2i(key: str):
Returns:
int: 在 servers 是第几个
"""
for i, te in enumerate(SERVERS):
for i, te in enumerate(SERVERS_T):
if te.key == key:
return i
return 0
Expand Down
10 changes: 8 additions & 2 deletions lfy/api/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
from lfy.api.server.com import AllServer
from lfy.api.server.google import GoogleServer
from lfy.api.server.huoshan import HuoShanServer
from lfy.api.server.ocr.baidu import BaiduServer as BaiduOCRServer
from lfy.api.server.ocr.easyocr import EasyOcrServer
from lfy.api.server.ocr.pytesseract import PytesseractServer
from lfy.api.server.tencent import TencentServer

SERVERS = [
# 翻译服务
SERVERS_T = [
AllServer(),
GoogleServer(),
BingServer(),
BaiduServer(),
TencentServer(),
AliYunServer(),
HuoShanServer(),
# ocr
]

# OCR文本识别服务
SERVERS_O = [
BaiduOCRServer(),
PytesseractServer(),
EasyOcrServer(),
]
Expand Down
6 changes: 3 additions & 3 deletions lfy/api/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ def translate_text(self, text: str, lang_to: str, lang_from: str = "auto"):
return True, "test"

def get_api_key_s(self):
"""字符串apikey
"""字符串apikey,翻译的
Returns:
_type_: _description_
"""
return None

def ocr_image(self, img_path: str, lang_keys=None):
def ocr_image(self, img_path: str):
"""图片识别
Args:
Expand All @@ -170,7 +170,7 @@ def check_ocr(self, api_key_ocr_s):
"""
return True, "success"

def get_ocr_api_key_s(self):
def get_api_key_s_ocr(self):
"""图片识别的字符串apikey
Returns:
Expand Down
Loading

0 comments on commit 3c1e97a

Please sign in to comment.