guix-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug#64356] [PATCH 2/4] marionette: Allow passing custom OCR arguments.


From: Bruno Victal
Subject: [bug#64356] [PATCH 2/4] marionette: Allow passing custom OCR arguments.
Date: Thu, 29 Jun 2023 21:44:20 +0100

* gnu/build/marionette.scm (invoke-ocrad-ocr, invoke-tesseract-ocr)
(marionette-screen-text): New 'ocr-arguments' argument.
---
 gnu/build/marionette.scm | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/gnu/build/marionette.scm b/gnu/build/marionette.scm
index b8fba61d06..5621896198 100644
--- a/gnu/build/marionette.scm
+++ b/gnu/build/marionette.scm
@@ -287,23 +287,30 @@ (define (marionette-control command marionette)
      ;; The "quit" command terminates QEMU immediately, with no output.
      (unless (string=? command "quit") (wait-for-monitor-prompt monitor)))))
 
-(define* (invoke-ocrad-ocr image #:key (ocrad "ocrad"))
+(define* (invoke-ocrad-ocr image #:key (ocrad "ocrad") ocr-arguments)
   "Invoke the OCRAD command on image, and return the recognized text."
-  (let* ((pipe (open-pipe* OPEN_READ ocrad "-i" "-s" "10" image))
+  (let* ((arguments (or ocr-arguments
+                        "--invert --scale 10"))
+         (command (string-join (list ocrad ocr-arguments image)))
+         (pipe (open-input-pipe command))
          (text (get-string-all pipe)))
     (unless (zero? (close-pipe pipe))
       (error "'ocrad' failed" ocrad))
     text))
 
-(define* (invoke-tesseract-ocr image #:key (tesseract "tesseract"))
+(define* (invoke-tesseract-ocr image #:key (tesseract "tesseract")
+                               ocr-arguments)
   "Invoke the TESSERACT command on IMAGE, and return the recognized text."
   (let* ((output-basename (tmpnam))
-         (output-basename* (string-append output-basename ".txt")))
+         (output-basename* (string-append output-basename ".txt"))
+         (arguments (cons* image output-basename
+                           (or (and=> ocr-arguments list)
+                               '()))))
     (dynamic-wind
       (const #t)
       (lambda ()
         (let ((exit-val (status:exit-val
-                         (system* tesseract image output-basename))))
+                         (apply system* tesseract arguments))))
           (unless (zero? exit-val)
             (error "'tesseract' failed" tesseract))
           (call-with-input-file output-basename* get-string-all)))
@@ -311,7 +318,8 @@ (define* (invoke-tesseract-ocr image #:key (tesseract 
"tesseract"))
         (false-if-exception (delete-file output-basename))
         (false-if-exception (delete-file output-basename*))))))
 
-(define* (marionette-screen-text marionette #:key (ocr "ocrad"))
+(define* (marionette-screen-text marionette #:key (ocr "ocrad")
+                                 ocr-arguments)
   "Take a screenshot of MARIONETTE, perform optical character
 recognition (OCR), and return the text read from the screen as a string, along
 the screen dump image used.  Do this by invoking OCR, which should be the file
@@ -324,14 +332,19 @@ (define* (marionette-screen-text marionette #:key (ocr 
"ocrad"))
   ;; Process it via the OCR.
   (cond
    ((string-contains ocr "ocrad")
-    (values (invoke-ocrad-ocr image #:ocrad ocr) image))
+    (values (invoke-ocrad-ocr image
+                              #:ocrad ocr
+                              #:ocr-arguments ocr-arguments) image))
    ((string-contains ocr "tesseract")
-    (values (invoke-tesseract-ocr image #:tesseract ocr) image))
+    (values (invoke-tesseract-ocr image
+                                  #:tesseract ocr
+                                  #:ocr-arguments ocr-arguments) image))
    (else (error "unsupported ocr command"))))
 
 (define* (wait-for-screen-text marionette predicate
                                #:key
                                (ocr "ocrad")
+                               ocr-arguments
                                (timeout 30)
                                pre-action
                                post-action)
@@ -359,7 +372,10 @@ (define* (wait-for-screen-text marionette predicate
                  'ocr-text: last-text
                  'screendump: screendump-backup))
         (let* ((_ (and (procedure? pre-action) (pre-action)))
-               (text screendump (marionette-screen-text marionette #:ocr ocr))
+               (text screendump
+                     (marionette-screen-text marionette
+                                             #:ocr ocr
+                                             #:ocr-arguments ocr-arguments))
                (_ (and (procedure? post-action) (post-action)))
                (result (predicate text)))
           (cond (result
-- 
2.39.2






reply via email to

[Prev in Thread] Current Thread [Next in Thread]