def get_models(self):
     tesscmd = get_app().tesspath if get_app().tesspath != "" else "tesseract"
     try:
         return check_output([tesscmd, "--tessdata-dir", get_app().tessdatadir, "--list-langs"]).decode(
             'utf-8').splitlines()[1:]
     except:
         return []
Esempio n. 2
0
def evaluate_report(text, *args):
    def close_dialog(instance, *args):
        instance.parent.parent.parent.parent.dismiss()

    report = evaluate_text(text)
    dialog = MDDialog(title="Report",
                      type='custom',
                      auto_dismiss=False,
                      content_cls=TextInput(text=report,
                                            size_hint_y=None,
                                            height=get_app()._window.size[1]-150,
                                            readonly=True,
                                            font_name="RobotoMono-Regular",),
                      buttons=[
                          MDFlatButton(
                              text="DISCARD", on_release=close_dialog
                          ),
                      ],
                      )
    if get_app()._platform not in ['win32', 'win64']:
    # TODO: Focus function seems buggy in win
        dialog.content_cls.focused = True
    time.sleep(1)
    dialog.content_cls.cursor = (0, 0)
    dialog.open()
Esempio n. 3
0
    def select_model(self,
                     model_obj: Model = None,
                     model_dict: dict = None,
                     id: int = None,
                     if_empty: bool = False):
        """ Update model info display by either object, ID, partial record, or complete record """
        # Initialize from object, dict, or ID
        if if_empty and self.selected_model is not None:
            return
        if not any([model_obj, model_dict, id]):
            return
        if not model_obj:
            model_obj = Model.from_dict(
                model_dict) if model_dict else Model.from_id(int(id))
        # Don't need to do anything if this model is already selected
        if self.selected_model is not None and model_obj.id == self.selected_model.id:
            return

        logger.info(f'Model: Selecting model {model_obj.id}')
        # self.screen.basic_info.clear_widgets()
        self.selected_model = model_obj
        asyncio.run(self.load_model_info())

        # Add to model history, and update model id on image selector screen
        get_app().update_history(self.selected_model.id)
        get_app().select_model_from_photo(self.selected_model.id)
Esempio n. 4
0
def install_tesseract_win(instance, *args):
    toast('Download: Succesful')
    logger.info(f'Download: Succesful')
    from os import startfile
    startfile(Path(tempfile.gettempdir()).joinpath("tesseract.exe"))
    reset_tesspaths()
    get_app().stop()
 def save_tessprofile(self, instance):
     tessprofilename = instance.parent.parent.parent.parent.content_cls.text
     if tessprofilename != '':
         get_app().tessprofiles[tessprofilename] = {
             "model":
             self.screen.model.current_item.split(" ")[1]
             if self.screen.model.current_item.split(" ")[0] == "Model:"
             else "eng",
             "psm":
             "".join(
                 [char for char in self.screen.psm.text if char.isdigit()]),
             "oem":
             "".join(
                 [char for char in self.screen.oem.text if char.isdigit()]),
             "outputformat":
             self.active_outputformats(),
             "print_on_screen":
             str(self.screen.print_on_screen_chk.active),
             "outputdir":
             "" if self.screen.output.text.split(" ")[0] != "Selected" else
             self.screen.output.text.split(" ")[3],
             "groupfolder":
             self.screen.groupfolder.text,
             "subfolder":
             str(self.screen.subfolder_chk.active),
             "default":
             False
         }
     write_tessprofiles(get_app().tessprofiles)
     instance.parent.parent.parent.parent.dismiss()
Esempio n. 6
0
    def set_list(self, text="", search=False):
        '''Lists all installed models '''
        def add_item(model):
            item = OneLineAvatarIconListItem(
                text=model,
                secondary_text="",
                on_release=partial(self.set_model, model),
            )
            item.add_widget(LeftCheckbox(active=self.checked_models[model]))
            self.layout.add_widget(item)

        if self.checked_models is None:
            self.checked_models = {}
            for model in get_app().tesseract_controller.models:
                self.checked_models[model] = False
        else:
            self.chk_active_models()
        self.layout.clear_widgets()
        self.screen.modellist.clear_widgets()
        for model in get_app().tesseract_controller.models:
            if search:
                if self.screen.exact_match_chk.active:
                    if text == model[:len(text)]:
                        add_item(model)
                else:
                    textparts = text.split(" ")
                    if sum([
                            True
                            if textpart.lower() in model.lower() else False
                            for textpart in textparts
                    ]) == len(textparts):
                        add_item(model)
            else:
                add_item(model)
        self.screen.modellist.add_widget(self.layout)
Esempio n. 7
0
 def check_threads(self, dt):
     import threading
     active_threads = get_app().active_threads.copy()
     for active_thread, pm in active_threads.items():
         if active_thread not in threading.enumerate():
             del get_app().active_threads[active_thread]
             pm.parent.remove_widget(pm)
Esempio n. 8
0
def create_online_threadprocess(processname:str,func, *args, **kwargs):
    new_thread = threading.Thread(target=func, args=args, kwargs=kwargs)
    new_thread.setDaemon(True)
    new_thread.start()
    pm = processmanager(processname)
    get_app().active_threads[new_thread] = pm
    get_app().image_selection_online_controller.screen.process_list.add_widget(pm)
Esempio n. 9
0
def authenticate() -> dict:
    if get_app().token is not None and get_app().token.get('token_type',
                                                           None) is not None:
        return get_app().token
    user = get_app().settings_controller.username
    pwd = get_app().settings_controller.password
    headers = {
        'accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    data = {'username': user, 'password': pwd}
    try:
        r = requests.post(
            f'{get_app().settings_controller.tesseract["online_url"]}/token',
            headers=headers,
            data=data,
            verify=False)
    except:
        toast(f"Connection error to the host. Please try again later.")
        return {}
    if r is None or r.status_code == 401:
        toast('Please provide valid credentials or sign up!')
        return {}
    elif r.status_code == 503:
        toast('Server is currently unavailable. Please try again later.')
        return {}
    token = json.loads(r.text)
    return token
Esempio n. 10
0
    def show_jobdata_info(instance, cell, *args):
        def close_dialog(instance, *args):
            instance.parent.parent.parent.parent.dismiss()

        if (cell.index + 1) % 4 != 0: return
        jobindex = len(cell.parent.children) - 1 - cell.range[0]
        jobname = cell.parent.children[jobindex].text
        jobfile = Path(JOBS_DIR).joinpath(jobname + '.json')
        if jobfile.exists():
            with open(jobfile, 'r') as fin:
                jobinfo = json.load(fin)
            dialog = MDDialog(
                title="Job description",
                type='custom',
                auto_dismiss=False,
                content_cls=TextInput(text=json.dumps(jobinfo, indent=4),
                                      size_hint_y=None,
                                      height=get_app()._window.size[1] - 150,
                                      readonly=True),
                buttons=[
                    MDFlatButton(text="DISCARD", on_release=close_dialog),
                ],
            )
            if get_app()._platform not in ['win32', 'win64']:
                dialog.content_cls.focused = True
            dialog.open()
        return
    def recognize(self, instance, *args, file_list=None):
        """ Recognize image with tesseract """
        if file_list is None:
            file_list = get_app().image_selection_controller.file_list
        if not file_list:
            alert(f'Select images to recognize')
            self.enable_rec(instance)
            return
        if instance._ButtonBehavior__touch_time < self.last_rec_time:
            self.enable_rec(instance)
            return

        logger.info(f'Main: Recognize {len(file_list)} images')

        # metadata_settings = get_app().metadata
        # TODO: Handle write errors (like file locked) and show dialog
        # file_list = get_app().image_selection_controller
        model = "eng" if self.screen.model.current_item == '' else self.screen.model.current_item.split(": ")[1].strip()
        psm = "3" if self.screen.psm.current_item == '' else self.screen.psm.current_item.split(": ")[1].strip()
        oem = "3" if self.screen.oem.current_item == '' else self.screen.oem.current_item.split(": ")[1].strip()
        outputformats = self.active_outputformats()
        print_on_screen = self.screen.print_on_screen_chk.active
        groupfolder = self.screen.groupfolder.text
        subfolder = self.screen.subfolder_chk.active
        proc_files, outputnames = recognize(file_list, model=model, psm=psm, oem=oem, tessdatadir=get_app().tessdatadir,
                                            output_folder=self.selected_output_folder, outputformats=outputformats,
                                            print_on_screen=print_on_screen, subfolder=subfolder, groupfolder=groupfolder)
        toast(f'{proc_files} images recognized')
        self.last_rec_time = time.time() + 2
        get_app().image_selection_controller.file_chooser._update_files()
        self.enable_rec(instance)
 def select_output_folder(self):
     if get_app().image_selection_controller.file_list != []:
         self.output_manager.show(
             str(
                 Path(get_app().image_selection_controller.file_list[0]).
                 parent.resolve()))
     else:
         self.output_manager.show("/")
 def set_to_default(self, sel_profile, *args):
     """ Set selected profile as default profile"""
     for profile, profileparam in get_app().tessprofiles.items():
         if profile == sel_profile:
             profileparam['default'] = True
         else:
             profileparam['default'] = False
     write_tessprofiles(get_app().tessprofiles)
     self.set_profiles(text=self.screen.search_field.text)
Esempio n. 14
0
 def set_model(self, model, *args):
     selected_models = []
     if model != "": selected_models.append(model)
     self.chk_active_models()
     for chk_model, state in self.checked_models.items():
         if state and chk_model != model:
             selected_models.append(chk_model)
     get_app().tesseract_controller.screen.model.set_item(
         'Model: ' + '+'.join(selected_models))
     get_app().switch_screen('tesseract_xplore')
Esempio n. 15
0
def start_installing_loaderprogress():
    """ Start a progress and set the state to 50%"""
    pb = LoaderProgressBar(color=get_app().theme_cls.primary_color)
    pb.value = 0
    pb.max = 2
    status_bar = get_app().image_selection_controller.status_bar
    status_bar.clear_widgets()
    status_bar.add_widget(pb)
    pb.update(None, 1)
    return pb
Esempio n. 16
0
 def installed_models(self):
     try:
         tesscmd = get_app().settings_controller.tesseract['tesspath'] if \
             get_app().settings_controller.tesseract['tesspath'] != "" else "tesseract"
         return check_output(
             [tesscmd, "--tessdata-dir", self.current_tessdatadir, "--list-langs"], universal_newlines=True).splitlines()[1:]
     except:
         from kivymd.toast import toast
         toast("Please install Tesseract!")
         return {}
    def set_list(self, *args, text="", search=False):
        ''' Lists all installed models '''
        def add_item(model):
            description = self.modelinfos.get(model).get('description', '')
            description = 'No description' if description == '' else description
            item = TwoLineAvatarIconListItem(text=model,
                                             secondary_text=description,
                                             on_release=partial(
                                                 self.set_model, model),
                                             size_hint=(None, None),
                                             size=(600, 1))
            if model not in self.checked_models:
                self.checked_models[model] = False
            item.add_widget(LeftCheckbox(active=self.checked_models[model]))
            item.add_widget(
                IconRightWidget(icon='file-edit',
                                on_release=partial(self.edit_description,
                                                   model, description)))
            self.layout.add_widget(item)

        if self.checked_models is None:
            self.checked_models = {}
            for model in list(
                    get_app().modelinformations.get_modelinfos().keys()):
                self.checked_models[model] = False
        else:
            self.chk_active_models()
        self.layout.clear_widgets()
        self.screen.modellist.clear_widgets()
        self.modelinfos = get_app().modelinformations.get_modelinfos()

        for model in list(self.modelinfos.keys()):
            if model == "osd": continue
            if self.screen.show_all_chk.active and len(text) == 0:
                add_item(model)
            if search and len(text) > 1:
                if self.screen.exact_match_chk.active:
                    if text == model[:len(text)]:
                        add_item(model)
                else:
                    textparts = text.split(" ")
                    if sum([
                            True
                            if textpart.lower() in model.lower() else False
                            for textpart in textparts
                    ]) == len(textparts):
                        add_item(model)
                    elif sum([
                            True if textpart.lower() in " ".join(
                                self.modelinfos.get(model).get(
                                    'tags', [''])).lower() else False
                            for textpart in textparts
                    ]) == len(textparts):
                        add_item(model)
        self.screen.modellist.add_widget(self.layout)
 def save_description(self, model, model_instance, dialog_instance, *args):
     dialog_instance.parent.parent.parent.parent.dismiss()
     model_instance.parent.parent.children[2].children[
         1].text = dialog_instance.parent.parent.parent.children[
             2].children[0].text
     modelinfo = get_app().modelinformations.get_modelinfos().get(model)
     modelinfo[
         'description'] = dialog_instance.parent.parent.parent.children[
             2].children[0].text
     get_app().modelinformations.update_modelinformations(model, modelinfo)
     get_app().modelinformations.write_modelinfos()
Esempio n. 19
0
def install_tesseract_unix(instance=None):
    install_tesseract = Popen(
        ['sudo', '-S', 'apt-get', 'install', '-y', 'tesseract-ocr'],
        stdin=PIPE)
    install_tesseract.stdin.write(
        bytes(
            instance.parent.parent.parent.parent.content_cls.children[0].text,
            'utf-8'))
    install_tesseract.communicate()
    reset_tesspaths()
    get_app().stop()
    return
Esempio n. 20
0
def install_unix(instance, *args):
    pwd = instance.parent.parent.parent.parent.content_cls.children[0].text
    instance.parent.parent.parent.parent.dismiss()
    install_tesseract = Popen(
        ['sudo', '-S', 'ap-get', 'install', '-y', 'tesseract-ocr'],
        stdin=PIPE,
        stdout=DEVNULL,
        stderr=STDOUT)
    install_tesseract.stdin.write(bytes(pwd, 'utf-8'))
    install_tesseract.communicate()
    get_app().stop()
    return
Esempio n. 21
0
 def _dl_model(self, url, outputpath):
     try:
         r = requests.get(url)
         with open(outputpath, 'wb') as f:
             f.write(r.content)
         toast('Download: Succesful')
         logger.info(f'Download: Succesful')
         # Update Modelslist
         get_app().tesseract_controller.models = get_app(
         ).tesseract_controller.get_models()
     except:
         toast('Download: Error')
         logger.info(f'Download: Error while downloading')
Esempio n. 22
0
 def get_search_parameters(self):
     """ Get API-compatible search parameters from the input widgets """
     params = {
         'q': self.model_search_input.text_input.text.strip(),
         'model_id': [t.model_id for t in self.selected_iconic_taxa],
         'rank': self.exact_rank_input.text.strip(),
         'min_rank': self.min_rank_input.text.strip(),
         'max_rank': self.max_rank_input.text.strip(),
         'per_page': 30,
         'locale': get_app().locale,
         'preferred_place_id': get_app().preferred_place_id,
     }
     return {k: v for k, v in params.items() if v}
 def get_model(instance):
     get_app().modellist_controller.screen.show_all_chk.active = False
     get_app().modellist_controller.search = True
     get_app().modellist_controller.screen.search_field.text = ""
     get_app().modellist_controller.screen.find_model_btn.disabled = False
     get_app().modellist_controller.set_list("")
     get_app().switch_screen('modellist')
 def load_profile(self, profileparam, *args):
     """ Apply all settings of the choosen profile to the main window"""
     if get_app().is_online():
         get_app().tesseract_online_controller.load_tessprofile(profileparam)
     else:
         get_app().tesseract_controller.load_tessprofile(profileparam)
     get_app().switch_screen(get_app().home_screen)
 def on_starred_model_click(self, instance, touch):
     """ Event handler for clicking a item from starred taxa list """
     if not instance.collide_point(*touch.pos):
         return
     # Right-click: Open context menu
     elif touch.button == 'right':
         self.context_menu.show(*get_app().root_window.mouse_pos)
         self.context_menu.ref = instance
         # self.context_menu.ids.view_model_ctx.disabled = not instance.metadata.model_id
     # Middle-click: remove item
     elif touch.button == 'middle':
         self.remove_star(instance.model.id)
     # Left-cliok: select model
     else:
         get_app().select_model(instance.model)
Esempio n. 26
0
 def update_filechooser_filter(self):
     # TODO: Rework this
     self.file_chooser.filters = [
         "*" + filter.strip() for filter in get_app().settings_controller.
         controls['filetypes'].text[1:-1].replace("'", "").split(',')
     ]
     self.file_chooser._update_files()
Esempio n. 27
0
    def __init__(self, screen):
        super().__init__(screen)
        self.context_menu = screen.context_menu
        self.screen = screen
        self.image_previews = screen.image_previews
        self.file_chooser = screen.file_chooser
        self.update_filechooser_filter()
        self.file_list = []
        self.theme_cls = get_app().theme_cls

        # Context menu item events
        # For recgonize see tesseract_controller
        self.context_menu.ids.edit_fulltext_ctx.bind(
            on_release=self.edit_fulltext)
        self.context_menu.ids.edit_image_ctx.bind(on_release=self.edit_image)
        self.context_menu.ids.remove_ctx.bind(
            on_release=lambda x: self.remove_image(x.selected_image))
        self.context_menu.ids.open_pdf_ctx.bind(
            on_release=self.open_pdf_instance)
        self.context_menu.ids.diff_stdout_ctx.bind(on_release=diff_dialog)

        # Other widget events
        self.screen.clear_button.bind(on_release=self.clear)
        self.screen.load_button.bind(on_release=self.add_file_chooser_images)
        self.screen.delete_button.bind(
            on_release=self.delete_file_chooser_selection_dialog)
        self.screen.sort_button.bind(on_release=self.sort_previews)
        self.screen.zoomin_button.bind(on_release=self.zoomin)
        self.screen.zoomout_button.bind(on_release=self.zoomout)

        # Instead see tesseract_controller
        # self.inputs.recognize_button.bind(on_release=self.run)
        self.file_chooser.bind(on_submit=self.add_file_chooser_images)
        self.screen.image_scrollview.bind(on_touch_down=self.on_touch_down)
 def get_user_taxa():
     username = get_app().username
     # TODO: Show this alert only when clicking on tab instead
     if not username:
         alert('Please enter iNaturalist username on Settings page')
         return {}
     return ""
 def delete_file_chooser_selection_dialog(self, *args):
     def close_dialog(instance, *args):
         instance.parent.parent.parent.parent.dismiss()
     if self.file_chooser.multiselect == True:
         self.update_filechooser_multiselect()
     sellist = MDList()
     for sel in self.file_chooser.selection:
         sellist.add_widget(OneLineListItem(text=sel))
     dialog = MDDialog(title="The following selection will be deleted:",
                       type='custom',
                       auto_dismiss=False,
                       content_cls=sellist,
                       buttons=[
                           MDFlatButton(
                               text="DELETE", on_release=self.delete_file_chooser_selection
                           ),
                           MDFlatButton(
                               text="DISCARD", on_release=close_dialog
                           ),
                       ],
                       )
     if get_app()._platform not in ['win32', 'win64']:
         # TODO: Focus function seems buggy in win
         dialog.content_cls.focused = True
     dialog.open()
    def goto_folder_dialog(self, *args):
        def close_dialog(instance, *args):
            instance.parent.parent.parent.parent.dismiss()

        if self.file_chooser.multiselect == True:
            self.update_filechooser_multiselect()
        sellist = MDList()
        for sel in self.file_chooser.selection:
            sellist.add_widget(OneLineListItem(text=sel))
        dialog = MDDialog(title="Please insert the desired folderpath",
                          type='custom',
                          auto_dismiss=False,
                          content_cls=MDTextField(text=''),
                          buttons=[
                              MDFlatButton(
                                  text="GO TO FOLDER", on_release=self.goto_folder
                              ),
                              MDFlatButton(
                                  text="DISCARD", on_release=close_dialog
                              ),
                          ],
                          )
        if get_app()._platform not in ['win32', 'win64']:
            # TODO: Focus function seems buggy in win
            dialog.content_cls.focused = True
        dialog.open()