jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1361541?usp=email )

Change subject: Add Transkribus OCR support for pywikibot
......................................................................

Add Transkribus OCR support for pywikibot

Currently, WikimediaOCR supports three OCR engines: Google, Tesseract and 
Transkribus.
proofreadpage.py only supports the first two. This change will have the side 
effect of being
able to select the language of the OCR, regardless of the engine.
Currently it always defaults to site.lang

Bug: T443330
Change-Id: I1f56d0c69c1defe3235486d15564c4efedc9b8dc
---
M pywikibot/comms/http.py
M pywikibot/proofreadpage.py
2 files changed, 34 insertions(+), 10 deletions(-)

Approvals:
  jenkins-bot: Verified
  Mahveotm: Looks good to me, but someone else must approve
  Xqt: Looks good to me, approved




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index d6ef262..60b28be 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -519,7 +519,10 @@

         auth = requests_oauthlib.OAuth1(*auth)

-    timeout = config.socket_timeout
+    if 'timeout' in kwargs:
+        timeout = kwargs.pop('timeout')
+    else:
+        timeout = config.socket_timeout

     try:
         # Note that the connections are pooled which mean that a future
diff --git a/pywikibot/proofreadpage.py b/pywikibot/proofreadpage.py
index 741dca4..369370a 100644
--- a/pywikibot/proofreadpage.py
+++ b/pywikibot/proofreadpage.py
@@ -453,15 +453,25 @@
     _WMFOCR_CMD = ('https://ocr.wmcloud.org/api.php?engine=tesseract&;'
                    'langs[]={lang}&image={url_image}&uselang={lang}')

-    # googleOCR ocr utility
+    # Google OCR utility
     _GOCR_CMD = ('https://ocr.wmcloud.org/api.php?engine=google&;'
                  'langs[]={lang}&image={url_image}')

+    # Transkribus OCR utility
+    _TRANSKRIBUS_CMD = ('https://ocr.wmcloud.org/api.php?engine=transkribus&;'
+                        'langs[]={lang}&image={url_image}')
+
     _MULTI_PAGE_EXT = ['djvu', 'pdf']

     _WMFOCR = 'wmfOCR'
     _GOOGLE_OCR = 'googleOCR'
-    _OCR_CMDS = {_WMFOCR: _WMFOCR_CMD, _GOOGLE_OCR: _GOCR_CMD}
+    _TRANSKRIBUS = 'transkribus'
+
+    _OCR_CMDS = {
+        _WMFOCR: _WMFOCR_CMD,
+        _GOOGLE_OCR: _GOCR_CMD,
+        _TRANSKRIBUS: _TRANSKRIBUS_CMD,
+    }
     _OCR_METHODS = list(_OCR_CMDS)

     def __init__(self, source: PageSourceType, title: str = '') -> None:
@@ -939,7 +949,8 @@
         for retry in range(5, 30, 5):
             pywikibot.debug(f'{ocr_tool}: get URI {cmd_uri!r}')
             try:
-                response = http.fetch(cmd_uri)
+                # OCR sometimes takes more than 45 seconds
+                response = http.fetch(cmd_uri, timeout=120)
             except ReadTimeout as e:
                 pywikibot.warning(f'ReadTimeout {cmd_uri}: {e}')
             except Exception as e:
@@ -970,8 +981,11 @@

         return error, parser_func(_text)

-    def _do_ocr(self, ocr_tool: str | None = None
-                ) -> tuple[bool, str | Exception]:
+    def _do_ocr(
+        self,
+        ocr_tool: str | None = None,
+        lang: str | None = None,
+    ) -> tuple[bool, str | Exception]:
         """Do ocr using specified ocr_tool method."""
         try:
             url_image = self.url_image
@@ -992,14 +1006,18 @@

         params = {
             'url_image': url_image,
-            'lang': self.site.lang,
+            'lang': lang or self.site.lang,
             'user': self.site.user(),
         }
         cmd_uri = cmd_fmt.format_map(params)

         return self._ocr_callback(cmd_uri, ocr_tool=ocr_tool)

-    def ocr(self, ocr_tool: str | None = None) -> str:
+    def ocr(
+        self,
+        ocr_tool: str | None = None,
+        lang: str | None = None,
+    ) -> str:
         """Do OCR of ProofreadPage scan.

         The text returned by this function shall be assigned to
@@ -1014,7 +1032,10 @@
         .. version-removed:: 9.2
            `phetools` support is not available anymore.

-        :param ocr_tool: Either 'wmfOCR' or 'googleOCR'; default is 'wmfOCR'
+        :param ocr_tool: Either 'wmfOCR', 'googleOCR' or 'transkribus';
+            default is 'wmfOCR'.
+        :param lang: Language code (for tesseract/google) or model name
+            (for transkribus). Defaults to site language.
         :return: OCR text for the page.
         :raises TypeError: Wrong ocr_tool keyword arg.
         :raises ValueError: Something went wrong with OCR process.
@@ -1026,7 +1047,7 @@
             raise TypeError(
                 f'ocr_tool must be in {self._OCR_METHODS}, not {ocr_tool!r}.')

-        error, text = self._do_ocr(ocr_tool=ocr_tool)
+        error, text = self._do_ocr(ocr_tool=ocr_tool, lang=lang)
         if not error and isinstance(text, str):
             return text


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1361541?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I1f56d0c69c1defe3235486d15564c4efedc9b8dc
Gerrit-Change-Number: 1361541
Gerrit-PatchSet: 7
Gerrit-Owner: Ignacio Rodríguez <[email protected]>
Gerrit-Reviewer: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to