示例#1
0
 def click_menu(self, menu_id, state):
     if menu_id == "quit":
         qApp.quit()
     elif menu_id == "wizard":
         pass
     elif menu_id == "about":
         pass
     elif menu_id == "settings":
         self.showSettingView.emit()
     elif menu_id == "lang":
         src_lang = setting_config.get_translate_config("src_lang")
         dst_lang = setting_config.get_translate_config("dst_lang")
         setting_config.update_translate_config("src_lang", dst_lang)
         setting_config.update_translate_config("dst_lang", src_lang)
         
         self.menu.setItemText("lang", self.get_lang_value())
         
         setting_view.updateLang.emit()
     else:
         if menu_id == "pause":
             if not state:
                 delete_selection()
             
             self.set_menu_active(state)    
             
         setting_config.update_trayicon_config(menu_id, state)
         
         self.set_trayicon()
 def update_translate_engine(self, option_type):
     global word_translate_model
     global words_translate_model
     global translate_local_simple
     global translate_local_long
     
     word_engines = dict_plugin.get_word_engines(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
     words_engines = dict_plugin.get_words_engines(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
     
     word_engine_names = map(lambda (name, display_name): name, word_engines)
     words_engine_names = map(lambda (name, display_name): name, words_engines)
     
     current_word_engine = setting_config.get_translate_config("word_engine")
     current_words_engine = setting_config.get_translate_config("words_engine")
     
     word_translate_model.setAll(word_engines)    
     words_translate_model.setAll(words_engines)    
     
     if current_word_engine not in word_engine_names:
         setting_config.update_translate_config("word_engine", word_engine_names[0])
         self.update_word_module()
         
     if current_words_engine not in words_engine_names:
         setting_config.update_translate_config("words_engine", words_engine_names[0])
         self.update_words_module()
         
     translate_local_simple = get_translate_local_simple()
     translate_local_long = get_translate_local_long()
示例#3
0
    def click_menu(self, menu_id, state):
        if menu_id == "quit":
            qApp.quit()
        elif menu_id == "wizard":
            pass
        elif menu_id == "about":
            pass
        elif menu_id == "settings":
            self.showSettingView.emit()
        elif menu_id == "lang":
            src_lang = setting_config.get_translate_config("src_lang")
            dst_lang = setting_config.get_translate_config("dst_lang")
            setting_config.update_translate_config("src_lang", dst_lang)
            setting_config.update_translate_config("dst_lang", src_lang)

            self.menu.setItemText("lang", self.get_lang_value())

            setting_view.updateLang.emit()
        else:
            if menu_id == "pause":
                if not state:
                    delete_selection()

                self.set_menu_active(state)

            setting_config.update_trayicon_config(menu_id, state)

            self.set_trayicon()
示例#4
0
 def get_translate(self, text):
     self.translate_info.voices = self.get_google_voices(
         text,
         tl=setting_config.get_translate_config("src_lang"),
         )
     self.translate_info.translate = self.google_translate(
         text,
         tl=setting_config.get_translate_config("dst_lang"),
         )
示例#5
0
 def get_translate(self, text):
     self.translate_info.voices = self.get_google_voices(
         text,
         tl=setting_config.get_translate_config("src_lang"),
     )
     self.translate_info.translate = self.google_translate(
         text,
         tl=setting_config.get_translate_config("dst_lang"),
     )
示例#6
0
def get_translate_local_long():
    words_engines = dict_plugin.get_words_engines(
        setting_config.get_translate_config("src_lang"),
        setting_config.get_translate_config("dst_lang"), False)
    words_engine_names = map(lambda (name, display_name): name, words_engines)
    if len(words_engine_names) > 0:
        local_long = imp.load_source(
            "local_long",
            dict_plugin.get_plugin_file(words_engine_names[0])).Translate()
        return local_long
    else:
        return None
 def update_voice_with_src_lang(self):
     voice_engines = tts_plugin.get_voice_engines(setting_config.get_translate_config("src_lang"))
     voice_engine_names = map(lambda (name, display_name): name, voice_engines)
     word_voice_model.setAll(voice_engines)
     words_voice_model.setAll(voice_engines)
     current_word_voice_engine = setting_config.get_translate_config("word_voice_engine")
     current_words_voice_engine = setting_config.get_translate_config("words_voice_engine")
         
     if current_word_voice_engine not in voice_engine_names:
         setting_config.update_translate_config("word_voice_engine", voice_engine_names[0])
         self.update_word_voice_module()
     
     if current_words_voice_engine not in voice_engine_names:
         setting_config.update_translate_config("words_voice_engine", voice_engine_names[0])
         self.update_words_voice_module()
示例#8
0
 def update_word_module(self):
     global translate_simple
     translate_simple = imp.load_source(
         "translate_simple",
         plugin.get_plugin_file(
             setting_config.get_translate_config(
                 "word_engine"))).Translate()
示例#9
0
 def update_words_module(self):
     global translate_long
     translate_long = imp.load_source(
         "translate_long",
         plugin.get_plugin_file(
             setting_config.get_translate_config(
                 "words_engine"))).Translate()
示例#10
0
 def get_translate(self, text):
     self.translate_info.text = text
     self.translate_info.voices = get_voice_long(text)
     self.translate_info.phonetic = get_phonetic_symbol(text)
     self.translate_info.translate = self.google_translate(
         text,
         tl=setting_config.get_translate_config("dst_lang"),
     )
示例#11
0
 def get_translate(self, text):
     self.translate_info.text = text
     self.translate_info.voices = get_voice_simple(text)
     self.translate_info.phonetic = get_phonetic_symbol(text)
     self.translate_info.translate = self.google_translate(
         text,
         tl=setting_config.get_translate_config("dst_lang"),
         )
def get_translate_local_long():
    words_engines = dict_plugin.get_words_engines(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"), False)
    words_engine_names = map(lambda (name, display_name): name, words_engines)
    if len(words_engine_names) > 0:
        local_long = imp.load_source("local_long", dict_plugin.get_plugin_file(words_engine_names[0])).Translate()
        return local_long
    else:
        return None
def get_voice_modules():
    if not is_network_connected() or setting_config.get_trayicon_config("local_translate"):
        voice_engines = tts_plugin.get_voice_engines(setting_config.get_translate_config("src_lang"), False)
        voice_engine_names = map(lambda (name, display_name): name, voice_engines)
        if len(voice_engine_names) > 0:
            local_simple = imp.load_source("local_simple", tts_plugin.get_plugin_file(voice_engine_names[0]))
            return [local_simple]
        else:
            return []
    else:
        return [voice_simple, voice_long]
示例#14
0
def get_voice(text):
    tl = setting_config.get_translate_config("src_lang")
    
    if not isinstance(text, unicode):
        text = text.decode("utf-8", "ignore")
        
    results = []    
    contents = group(text, 54)
    for c in contents:
        results.append(google_voice("".join(c), tl=tl))
    return results    
示例#15
0
def get_voice(text):
    tl = setting_config.get_translate_config("src_lang")
    
    if not isinstance(text, unicode):
        text = text.decode("utf-8", "ignore")
        
    results = []    
    contents = group(text, 54)
    for c in contents:
        results.append(google_voice("".join(c), tl=tl))
    return results    
示例#16
0
def get_voice(text, voice):
    if is_network_connected():
        return voice.get_voice(text)
    else:
        voice_engines = tts_plugin.get_voice_engines(setting_config.get_translate_config("src_lang"), False)
        voice_engine_names = map(lambda (name, display_name): name, voice_engines)
        if len(voice_engine_names) > 0:
            local_simple = imp.load_source("local_simple", tts_plugin.get_plugin_file(voice_engine_names[0]))
            return local_simple.get_voice(text)
        else:
            return []
示例#17
0
    def update_voice_with_src_lang(self):
        voice_engines = tts_plugin.get_voice_engines(
            setting_config.get_translate_config("src_lang"))
        voice_engine_names = map(lambda (name, display_name): name,
                                 voice_engines)
        word_voice_model.setAll(voice_engines)
        words_voice_model.setAll(voice_engines)
        current_word_voice_engine = setting_config.get_translate_config(
            "word_voice_engine")
        current_words_voice_engine = setting_config.get_translate_config(
            "words_voice_engine")

        if current_word_voice_engine not in voice_engine_names:
            setting_config.update_translate_config("word_voice_engine",
                                                   voice_engine_names[0])
            self.update_word_voice_module()

        if current_words_voice_engine not in voice_engine_names:
            setting_config.update_translate_config("words_voice_engine",
                                                   voice_engine_names[0])
            self.update_words_voice_module()
示例#18
0
 def update_translate_engine(self):
     global translate_simple
     global translate_long
     global word_translate_model
     global words_translate_model
     
     word_engines = plugin.get_word_engines(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
     words_engines = plugin.get_words_engines(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
     word_engine_names = map(lambda (name, display_name): name, word_engines)
     words_engine_names = map(lambda (name, display_name): name, words_engines)
     
     current_word_engine = setting_config.get_translate_config("word_engine")
     current_words_engine = setting_config.get_translate_config("words_engine")
     
     word_translate_model.setAll(word_engines)    
     words_translate_model.setAll(words_engines)    
     
     if current_word_engine not in word_engine_names:
         setting_config.update_translate_config("word_engine", word_engine_names[0])
         translate_simple = imp.load_source("translate_simple", plugin.get_plugin_file(setting_config.get_translate_config("word_engine"))).Translate()
         
     if current_words_engine not in words_engine_names:
         setting_config.update_translate_config("words_engine", words_engine_names[0])
         translate_long = imp.load_source("translate_long", plugin.get_plugin_file(setting_config.get_translate_config("words_engine"))).Translate()
示例#19
0
def get_voice(text, voice):
    if not is_network_connected() or setting_config.get_trayicon_config(
            "local_translate"):
        voice_engines = tts_plugin.get_voice_engines(
            setting_config.get_translate_config("src_lang"), False)
        voice_engine_names = map(lambda (name, display_name): name,
                                 voice_engines)
        if len(voice_engine_names) > 0:
            local_simple = imp.load_source(
                "local_simple",
                tts_plugin.get_plugin_file(voice_engine_names[0]))
            return local_simple.get_voice(text)
        else:
            return []
    else:
        return voice.get_voice(text)
示例#20
0
    def update_translate_engine(self):
        global translate_simple
        global translate_long
        global word_translate_model
        global words_translate_model

        word_engines = plugin.get_word_engines(
            setting_config.get_translate_config("src_lang"),
            setting_config.get_translate_config("dst_lang"))
        words_engines = plugin.get_words_engines(
            setting_config.get_translate_config("src_lang"),
            setting_config.get_translate_config("dst_lang"))
        word_engine_names = map(lambda (name, display_name): name,
                                word_engines)
        words_engine_names = map(lambda (name, display_name): name,
                                 words_engines)

        current_word_engine = setting_config.get_translate_config(
            "word_engine")
        current_words_engine = setting_config.get_translate_config(
            "words_engine")

        word_translate_model.setAll(word_engines)
        words_translate_model.setAll(words_engines)

        if current_word_engine not in word_engine_names:
            setting_config.update_translate_config("word_engine",
                                                   word_engine_names[0])
            translate_simple = imp.load_source(
                "translate_simple",
                plugin.get_plugin_file(
                    setting_config.get_translate_config(
                        "word_engine"))).Translate()

        if current_words_engine not in words_engine_names:
            setting_config.update_translate_config("words_engine",
                                                   words_engine_names[0])
            translate_long = imp.load_source(
                "translate_long",
                plugin.get_plugin_file(
                    setting_config.get_translate_config(
                        "words_engine"))).Translate()
示例#21
0
    def update_translate_engine(self, option_type):
        global word_translate_model
        global words_translate_model
        global translate_local_simple
        global translate_local_long

        word_engines = dict_plugin.get_word_engines(
            setting_config.get_translate_config("src_lang"),
            setting_config.get_translate_config("dst_lang"))
        words_engines = dict_plugin.get_words_engines(
            setting_config.get_translate_config("src_lang"),
            setting_config.get_translate_config("dst_lang"))

        word_engine_names = map(lambda (name, display_name): name,
                                word_engines)
        words_engine_names = map(lambda (name, display_name): name,
                                 words_engines)

        current_word_engine = setting_config.get_translate_config(
            "word_engine")
        current_words_engine = setting_config.get_translate_config(
            "words_engine")

        word_translate_model.setAll(word_engines)
        words_translate_model.setAll(words_engines)

        if current_word_engine not in word_engine_names:
            setting_config.update_translate_config("word_engine",
                                                   word_engine_names[0])
            self.update_word_module()

        if current_words_engine not in words_engine_names:
            setting_config.update_translate_config("words_engine",
                                                   words_engine_names[0])
            self.update_words_module()

        translate_local_simple = get_translate_local_simple()
        translate_local_long = get_translate_local_long()
示例#22
0
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import pyqtSlot, QObject
import imp
from tts_plugin import TtsPlugin
from config import setting_config
from deepin_utils.net import is_network_connected

tts_plugin = TtsPlugin()
voice_simple = imp.load_source(
    "voice_simple",
    tts_plugin.get_plugin_file(
        setting_config.get_translate_config("word_voice_engine")))
voice_long = imp.load_source(
    "voice_long",
    tts_plugin.get_plugin_file(
        setting_config.get_translate_config("words_voice_engine")))
word_voice_model = tts_plugin.get_voice_model(
    setting_config.get_translate_config("src_lang"))
words_voice_model = tts_plugin.get_voice_model(
    setting_config.get_translate_config("src_lang"))


def get_voice_modules():
    if not is_network_connected() or setting_config.get_trayicon_config(
            "local_translate"):
        voice_engines = tts_plugin.get_voice_engines(
            setting_config.get_translate_config("src_lang"), False)
示例#23
0
 def get_lang_value(self):
     return (dict(LANGUAGES))[setting_config.get_translate_config("src_lang")] + " <=> " + (dict(LANGUAGES))[setting_config.get_translate_config("dst_lang")]
示例#24
0
 def get_lang_value(self):
     return (dict(LANGUAGES))[setting_config.get_translate_config(
         "src_lang")] + " <=> " + (dict(LANGUAGES))[
             setting_config.get_translate_config("dst_lang")]
示例#25
0
        translate_long = imp.load_source("translate_long", plugin.get_plugin_file(setting_config.get_translate_config("words_engine"))).Translate()
    
if __name__ == "__main__":
    uniqueService = UniqueService(APP_DBUS_NAME, APP_OBJECT_NAME)

    app = QApplication(sys.argv)  
    tray_icon = SystemTrayIcon(QIcon(os.path.join(get_parent_dir(__file__), "image", "trayicon.png")), app)
    tray_icon.show()
    (constant.TRAYAREA_TOP, constant.TRAYAREA_BOTTOM) = tray_icon.get_trayarea()
    
    plugin = Plugin()
    
    source_lang_model = LanguageModel()
    dest_lang_model = LanguageModel()

    word_engine_name = setting_config.get_translate_config("word_engine")
    words_engine_name = setting_config.get_translate_config("words_engine")
    translate_simple = imp.load_source("translate_simple", plugin.get_plugin_file(word_engine_name)).Translate()
    translate_long = imp.load_source("translate_long", plugin.get_plugin_file(words_engine_name)).Translate()
    word_translate_model = plugin.get_word_model(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
    words_translate_model = plugin.get_words_model(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
    
    translate_info = TranslateInfo()
    
    setting_view = Window()
    setting_view.qml_context.setContextProperty("sourceLangModel", source_lang_model)
    setting_view.qml_context.setContextProperty("destLangModel", dest_lang_model)
    setting_view.qml_context.setContextProperty("plugin", plugin)
    setting_view.qml_context.setContextProperty("wordTranslateModel", word_translate_model)
    setting_view.qml_context.setContextProperty("wordsTranslateModel", words_translate_model)
    setting_view.qml_context.setContextProperty("screenWidth", screen_width)
示例#26
0
def ocr_word(mouse_x, mouse_y):
    # Return None if occur unsupported language.
    src_lang = setting_config.get_translate_config("src_lang")
    if not LANGUAGE_OCR_DICT.has_key(src_lang):
        show_message("对不起, 屏幕取词当前还不支持%s" % _(src_lang), "取消", "我知道了", lambda : ocr_log(src_lang))
        return None

    # Return None if found any ocr package need install before continue. 
    ocr_pkg_name = LANGUAGE_OCR_DICT[src_lang]
    pkg_names = get_install_packages([ocr_pkg_name])
    if len(pkg_names):
        show_message("需要安装OCR语言包以启用翻译功能", "取消", "安装", lambda : install_packages(pkg_names))
        return None
    
    # Return None if mouse at trayicon area.
    if constant.TRAYAREA_TOP < mouse_y < constant.TRAYAREA_BOTTOM:
        return None

    # Ocr word under cursor.
    lang = ocr_pkg_name.split("tesseract-ocr-")[1]
    x = max(mouse_x - screenshot_width / 2, 0) 
    y = max(mouse_y - screenshot_height / 2, 0)
    width = min(mouse_x + screenshot_width / 2, screen_width) - x
    height = min(mouse_y + screenshot_height / 2, screen_height) - y
                    
    scale = 2
    tool = pyocr.get_available_tools()[0]
        
    output_format = xcb.xproto.ImageFormat.ZPixmap
    plane_mask = 2**32 - 1
    
    reply = conn.core.GetImage(
        output_format, 
        root, 
        x,
        y,
        width,
        height,
        plane_mask).reply()
    image_data = reply.data.buf()
    image = Image.frombuffer("RGBX", (width, height), image_data, "raw", "BGRX").convert("RGB")
    
    word_boxes = tool.image_to_string(
        image.convert("L").resize((width * scale, height * scale)),
        lang=lang,
        builder=pyocr.builders.WordBoxBuilder())
    
    cursor_x = (mouse_x - x) * scale
    cursor_y = (mouse_y - y) * scale
    
    for word_box in word_boxes[::-1]:
        ((left_x, left_y), (right_x, right_y)) = word_box.position
        if (left_x <= cursor_x <= right_x and left_y <= cursor_y <= right_y):
            word = filter_punctuation(word_box.content)
            # Return None if ocr word is space string.
            if word.isspace():
                return None
            else:
                return word
        
    return None    
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import pyqtSlot, QObject
from dict_plugin import DictPlugin
from model import LanguageModel
import imp
from config import setting_config
from deepin_utils.net import is_network_connected

dict_plugin = DictPlugin()

source_lang_model = LanguageModel()
dest_lang_model = LanguageModel()

translate_simple = imp.load_source("translate_simple", dict_plugin.get_plugin_file(setting_config.get_translate_config("word_engine"))).Translate()
translate_long = imp.load_source("translate_long", dict_plugin.get_plugin_file(setting_config.get_translate_config("words_engine"))).Translate()

word_translate_model = dict_plugin.get_word_model(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))
words_translate_model = dict_plugin.get_words_model(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"))

def get_translate_local_simple():
    word_engines = dict_plugin.get_word_engines(setting_config.get_translate_config("src_lang"), setting_config.get_translate_config("dst_lang"), False)
    word_engine_names = map(lambda (name, display_name): name, word_engines)
    if len(word_engine_names) > 0:
        local_simple = imp.load_source("local_simple", dict_plugin.get_plugin_file(word_engine_names[0])).Translate()
        return local_simple
    else:
        return None

def get_translate_local_long():
 def update_words_module(self):
     global translate_long
     translate_long.hide_translate()
     translate_long = imp.load_source("translate_long", dict_plugin.get_plugin_file(setting_config.get_translate_config("words_engine"))).Translate()
示例#29
0
 def update_words_voice_module(self):
     global voice_long
     voice_long = imp.load_source(
         "voice_long",
         tts_plugin.get_plugin_file(
             setting_config.get_translate_config("words_voice_engine")))
示例#30
0
    uniqueService = UniqueService(APP_DBUS_NAME, APP_OBJECT_NAME)

    app = QApplication(sys.argv)
    tray_icon = SystemTrayIcon(
        QIcon(os.path.join(get_parent_dir(__file__), "image", "trayicon.png")),
        app)
    tray_icon.show()
    (constant.TRAYAREA_TOP,
     constant.TRAYAREA_BOTTOM) = tray_icon.get_trayarea()

    plugin = Plugin()

    source_lang_model = LanguageModel()
    dest_lang_model = LanguageModel()

    word_engine_name = setting_config.get_translate_config("word_engine")
    words_engine_name = setting_config.get_translate_config("words_engine")
    translate_simple = imp.load_source(
        "translate_simple",
        plugin.get_plugin_file(word_engine_name)).Translate()
    translate_long = imp.load_source(
        "translate_long",
        plugin.get_plugin_file(words_engine_name)).Translate()
    word_translate_model = plugin.get_word_model(
        setting_config.get_translate_config("src_lang"),
        setting_config.get_translate_config("dst_lang"))
    words_translate_model = plugin.get_words_model(
        setting_config.get_translate_config("src_lang"),
        setting_config.get_translate_config("dst_lang"))

    translate_info = TranslateInfo()
示例#31
0
 def update_words_voice_module(self):
     global voice_long
     voice_long = imp.load_source("voice_long", tts_plugin.get_plugin_file(setting_config.get_translate_config("words_voice_engine")))
示例#32
0
def filter_punctuation(text):
    src_lang = setting_config.get_translate_config("src_lang")
    if src_lang in ["en"]:
        return re.sub("[^A-Za-z_-]", " ", text).strip().split(" ")[0]
    else:
        return text
示例#33
0
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import pyqtSlot, QObject
import imp
from tts_plugin import TtsPlugin
from config import setting_config
from deepin_utils.net import is_network_connected

tts_plugin = TtsPlugin()
voice_simple = imp.load_source("voice_simple", tts_plugin.get_plugin_file(setting_config.get_translate_config("word_voice_engine")))
voice_long = imp.load_source("voice_long", tts_plugin.get_plugin_file(setting_config.get_translate_config("words_voice_engine")))
word_voice_model = tts_plugin.get_voice_model(setting_config.get_translate_config("src_lang"))
words_voice_model = tts_plugin.get_voice_model(setting_config.get_translate_config("src_lang"))

def get_voice(text, voice):
    if is_network_connected():
        return voice.get_voice(text)
    else:
        voice_engines = tts_plugin.get_voice_engines(setting_config.get_translate_config("src_lang"), False)
        voice_engine_names = map(lambda (name, display_name): name, voice_engines)
        if len(voice_engine_names) > 0:
            local_simple = imp.load_source("local_simple", tts_plugin.get_plugin_file(voice_engine_names[0]))
            return local_simple.get_voice(text)
        else:
            return []
示例#34
0
def ocr_word(mouse_x, mouse_y):
    # Return None if occur unsupported language.
    src_lang = setting_config.get_translate_config("src_lang")
    if not LANGUAGE_OCR_DICT.has_key(src_lang):
        show_message(_("Sorry, select-and-translate does not support %s yet") % _(src_lang), _("Cancel"), _("Ok, I understand"), lambda : ocr_log(src_lang))
        return None

    # Return None if found any ocr package need install before continue. 
    ocr_pkg_name = LANGUAGE_OCR_DICT[src_lang]
    pkg_names = get_install_packages([ocr_pkg_name])
    if len(pkg_names):
        show_message(_("An OCR package is required to enable word recognition"), _("Cancel"), _("Install"), lambda : install_packages(pkg_names))
        return None
    
    # Return None if mouse at trayicon area.
    if constant.TRAYAREA_TOP < mouse_y < constant.TRAYAREA_BOTTOM:
        return None

    # Ocr word under cursor.
    lang = ocr_pkg_name.split("tesseract-ocr-")[1].replace("-", "_")
    x = max(mouse_x - screenshot_width / 2, 0) 
    y = max(mouse_y - screenshot_height / 2, 0)
    width = min(mouse_x + screenshot_width / 2, screen_width) - x
    height = min(mouse_y + screenshot_height / 2, screen_height) - y
                    
    scale = 2
    tool = pyocr.get_available_tools()[0]
        
    output_format = xcb.xproto.ImageFormat.ZPixmap
    plane_mask = 2**32 - 1
    
    reply = conn.core.GetImage(
        output_format, 
        root, 
        x,
        y,
        width,
        height,
        plane_mask).reply()
    
    # Get screenshot image data.
    image_data = reply.data.buf()
    image = Image.frombuffer("RGBX", (width, height), image_data, "raw", "BGRX").convert("RGB")
    
    # First make image grey and scale bigger.
    image = image.convert("L").resize((width * scale, height * scale))
    
    # image.save("old.png")       # debug
    # Second enhance image with contrast and sharpness.
    image = ImageEnhance.Contrast(image).enhance(1.5)
    
    # I found uncomment below code have better result. ;)
    # image = ImageEnhance.Sharpness(image).enhance(2.0)
    
    # image.save("new.png")       # debug
    
    word_boxes = tool.image_to_string(
        image,
        lang=lang,
        builder=pyocr.builders.WordBoxBuilder())
    
    cursor_x = (mouse_x - x) * scale
    cursor_y = (mouse_y - y) * scale
    
    for word_box in word_boxes[::-1]:
        ((left_x, left_y), (right_x, right_y)) = word_box.position
        if (left_x <= cursor_x <= right_x and left_y <= cursor_y <= right_y):
            word = filter_punctuation(word_box.content)
            # Return None if ocr word is space string.
            if word.isspace():
                return None
            else:
                return word
        
    return None    
示例#35
0
from PyQt5.QtCore import pyqtSlot, QObject
from dict_plugin import DictPlugin
from model import LanguageModel
import imp
from config import setting_config
from deepin_utils.net import is_network_connected

dict_plugin = DictPlugin()

source_lang_model = LanguageModel()
dest_lang_model = LanguageModel()

translate_simple = imp.load_source(
    "translate_simple",
    dict_plugin.get_plugin_file(
        setting_config.get_translate_config("word_engine"))).Translate()
translate_long = imp.load_source(
    "translate_long",
    dict_plugin.get_plugin_file(
        setting_config.get_translate_config("words_engine"))).Translate()

word_translate_model = dict_plugin.get_word_model(
    setting_config.get_translate_config("src_lang"),
    setting_config.get_translate_config("dst_lang"))
words_translate_model = dict_plugin.get_words_model(
    setting_config.get_translate_config("src_lang"),
    setting_config.get_translate_config("dst_lang"))


def get_translate_local_simple():
    word_engines = dict_plugin.get_word_engines(
 def update_word_module(self):
     global translate_simple
     translate_simple.hide_translate()
     translate_simple = imp.load_source("translate_simple", dict_plugin.get_plugin_file(setting_config.get_translate_config("word_engine"))).Translate()