From 7f7779ce6f0f9a7913f5d6001b3d13b2fbbe5b73 Mon Sep 17 00:00:00 2001 From: thiswillbeyourgithub Date: Thu, 18 Nov 2021 16:57:08 +0100 Subject: [PATCH 1/4] feat: add button that finds missing OCR captions --- anki_ocr/gui.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/anki_ocr/gui.py b/anki_ocr/gui.py index 35cff44..5126c7f 100644 --- a/anki_ocr/gui.py +++ b/anki_ocr/gui.py @@ -128,11 +128,59 @@ def on_menu_setup(browser: Browser): act_rm_ocr_fields.triggered.connect(lambda b=browser: on_rm_ocr_fields(browser)) anki_ocr_menu.addAction(act_rm_ocr_fields) + act_missing_ocr = QAction(browser, text="Find missing captions") + act_missing_ocr.triggered.connect(lambda b=browser: find_missing_caption(browser)) + anki_ocr_menu.addAction(act_missing_ocr) + + browser_cards_menu = browser.form.menu_Cards browser_cards_menu.addSeparator() browser_cards_menu.addMenu(anki_ocr_menu) +def find_missing_caption(browser: Browser): + all_id_flds = mw.col.db.all("select id, mid, flds from notes") + models = mw.col.models.all() + IOCm = [] + for m in models: + if "IOC" in m["name"] or "image occlusion" in m["name"].lower(): + IOCm.append(m["id"]) + + to_OCR = [] + for aif in all_id_flds: + cid = aif[0] + m = aif[1] + f = aif[2].replace("\n", " ").replace("\r", " ") + + found = f.count(" 0: + title = f.count(" title=") + if m not in IOCm: + if found - title > 0: + to_OCR.append(cid) + else: + if found - title > 3: + to_OCR.append(cid) + + if to_OCR: + mw.requireReset() + try: + mw.col.tags.bulkAdd(to_OCR, "missing_OCR", True) + except Exception as e: + print(f"Error: {e}") + finally: + mw.maybeReset() + + browser.activateWindow() + browser.form.searchEdit.lineEdit().setText("tag:missing_OCR") + if hasattr(browser, 'onSearch'): + browser.onSearch() + else: + browser.onSearchActivated() + return True + + + def create_menu(): from anki.hooks import addHook addHook("browser.setupMenus", on_menu_setup) From e260aa47445ad5f87e38c7d371c5a8222885a568 Mon Sep 17 00:00:00 2001 From: thiswillbeyourgithub Date: Fri, 19 Nov 2021 19:55:54 +0100 Subject: [PATCH 2/4] new: remove previous 'missing_OCR' tag before adding it --- anki_ocr/gui.py | 1 + 1 file changed, 1 insertion(+) diff --git a/anki_ocr/gui.py b/anki_ocr/gui.py index 5126c7f..dde551c 100644 --- a/anki_ocr/gui.py +++ b/anki_ocr/gui.py @@ -165,6 +165,7 @@ def find_missing_caption(browser: Browser): if to_OCR: mw.requireReset() try: + mw.col.tags.bulkAdd([x[0] for x in all_id_flds], "missing_OCR", False) mw.col.tags.bulkAdd(to_OCR, "missing_OCR", True) except Exception as e: print(f"Error: {e}") From f14cce4540751c42ee636f54a84fd332e7baca42 Mon Sep 17 00:00:00 2001 From: thiswillbeyourgithub Date: Fri, 4 Feb 2022 15:50:56 +0100 Subject: [PATCH 3/4] feat: now button : find missing caption and OCR it directly + remove tag 'missing_OCR' --- anki_ocr/gui.py | 50 ++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/anki_ocr/gui.py b/anki_ocr/gui.py index dde551c..95f830e 100644 --- a/anki_ocr/gui.py +++ b/anki_ocr/gui.py @@ -16,10 +16,13 @@ logger = create_ocr_logger() -def on_run_ocr(browser: Browser): +def on_run_ocr(browser: Browser, nids=None): time_start = time.time() - selected_nids = browser.selectedNotes() + if nids is None: + selected_nids = browser.selectedNotes() + else: + selected_nids = nids config = mw.addonManager.getConfig(__name__) num_notes = len(selected_nids) num_batches = ceil(num_notes / config["batch_size"]) @@ -129,44 +132,37 @@ def on_menu_setup(browser: Browser): anki_ocr_menu.addAction(act_rm_ocr_fields) act_missing_ocr = QAction(browser, text="Find missing captions") - act_missing_ocr.triggered.connect(lambda b=browser: find_missing_caption(browser)) + act_missing_ocr.triggered.connect(lambda b=browser: find_missing_caption(browser, False)) anki_ocr_menu.addAction(act_missing_ocr) + act_auto_missing_ocr = QAction(browser, text="Run AnkiOCR on all missing captions") + act_auto_missing_ocr.triggered.connect(lambda b=browser: find_missing_caption(browser, True)) + anki_ocr_menu.addAction(act_auto_missing_ocr) browser_cards_menu = browser.form.menu_Cards browser_cards_menu.addSeparator() browser_cards_menu.addMenu(anki_ocr_menu) -def find_missing_caption(browser: Browser): +def find_missing_caption(browser: Browser, OCR): all_id_flds = mw.col.db.all("select id, mid, flds from notes") - models = mw.col.models.all() - IOCm = [] - for m in models: - if "IOC" in m["name"] or "image occlusion" in m["name"].lower(): - IOCm.append(m["id"]) - to_OCR = [] + id_missing_OCR = [] for aif in all_id_flds: cid = aif[0] - m = aif[1] f = aif[2].replace("\n", " ").replace("\r", " ") - found = f.count(" 0: title = f.count(" title=") - if m not in IOCm: - if found - title > 0: - to_OCR.append(cid) - else: - if found - title > 3: - to_OCR.append(cid) - - if to_OCR: + if found - title > 0: + id_missing_OCR.append(cid) + + if id_missing_OCR: mw.requireReset() try: mw.col.tags.bulkAdd([x[0] for x in all_id_flds], "missing_OCR", False) - mw.col.tags.bulkAdd(to_OCR, "missing_OCR", True) + mw.col.tags.bulkAdd(id_missing_OCR, "missing_OCR", True) except Exception as e: print(f"Error: {e}") finally: @@ -178,6 +174,18 @@ def find_missing_caption(browser: Browser): browser.onSearch() else: browser.onSearchActivated() + + if OCR is True: + on_run_ocr(browser, nids=id_missing_OCR) # OCR cards + + # remove tag "missing_OCR": + mw.requireReset() + try: + mw.col.tags.bulkAdd([x[0] for x in all_id_flds], "missing_OCR", False) + except Exception as e: + print(f"Error: {e}") + finally: + mw.maybeReset() return True From 6ef51c74244b52ee0c8cf09ffa60009d75239572 Mon Sep 17 00:00:00 2001 From: thiswillbeyourgithub Date: Mon, 7 Feb 2022 16:14:21 +0100 Subject: [PATCH 4/4] fix: (untested) don't need to add tag if OCR automatically --- anki_ocr/gui.py | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/anki_ocr/gui.py b/anki_ocr/gui.py index 95f830e..4ca1695 100644 --- a/anki_ocr/gui.py +++ b/anki_ocr/gui.py @@ -159,6 +159,9 @@ def find_missing_caption(browser: Browser, OCR): id_missing_OCR.append(cid) if id_missing_OCR: + if OCR is True: + on_run_ocr(browser, nids=id_missing_OCR) # OCR cards + return True mw.requireReset() try: mw.col.tags.bulkAdd([x[0] for x in all_id_flds], "missing_OCR", False) @@ -175,17 +178,6 @@ def find_missing_caption(browser: Browser, OCR): else: browser.onSearchActivated() - if OCR is True: - on_run_ocr(browser, nids=id_missing_OCR) # OCR cards - - # remove tag "missing_OCR": - mw.requireReset() - try: - mw.col.tags.bulkAdd([x[0] for x in all_id_flds], "missing_OCR", False) - except Exception as e: - print(f"Error: {e}") - finally: - mw.maybeReset() return True