Skip to content

Instantly share code, notes, and snippets.

@Jimx-
Created December 9, 2018 12:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Jimx-/4702dce72586f4e56177b20e7768f649 to your computer and use it in GitHub Desktop.
Save Jimx-/4702dce72586f4e56177b20e7768f649 to your computer and use it in GitHub Desktop.
Recognize the text on images and insert it in place as a quote block in Org-mode
;;; org-ocr.el --- Recognize the text on images and insert it in place as a quote block in Org-mode -*- lexical-binding: t; -*-
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Recognize the text on omages and insert it in place as a quote block in Org-mode
;;
;;; Code:
(require 'request)
(defvar org-ocr-api-url
"http://127.0.0.1:8080/ocr"
"URL of the OCR service provider. For instructions on setting up the local
OCR server, see https://github.com/chineseocr/chineseocr.")
(defun org-ocr--base64-encode-image (filename)
"Encode an image file as a base64 string."
(base64-encode-string
(with-temp-buffer
(insert-file-contents filename)
(buffer-string))
t))
(defun org-ocr--find-link ()
"Find the last image link before point."
(save-excursion
(let ((start (window-start)))
(if (re-search-backward "\\[\\[\\(.*\\)\\]\\]" start t)
(buffer-substring-no-properties
(match-beginning 1)
(match-end 1))))))
(defun org-ocr--get-json-request-payload (image-string)
"Generate the JSON format request payload for the image string."
(format "{\"imgString\":\"data:image/png;base64,%s\"}" image-string))
(defun org-ocr--render-result (buffer result)
"Render recognition result in a quote block."
(with-current-buffer buffer
(insert "\n#+BEGIN_QUOTE\n")
(mapcar (lambda (res)
(insert (format "%s\n" (assoc-default 'text res)))
(message "%s" (assoc-default 'text res)))
result)
(insert "#+END_QUOTE"))
(set-buffer-multibyte nil)
(set-buffer-multibyte t))
(defun org-ocr--query-ocr (image-string)
(let ((buffer (current-buffer)))
(request
org-ocr-api-url
:type "POST"
:data (org-ocr--get-json-request-payload image-string)
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(org-ocr--render-result buffer (assoc-default 'res data))))
:error
(cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown))))))
(defun org-ocr-to-quote-block ()
"Recognize the text on the last image before point and insert the
recognition result as a quote block at point."
(interactive)
(let ((link (org-ocr--find-link)))
(message "sending %s..." link)
(when link
(org-ocr--query-ocr (org-ocr--base64-encode-image link)))))
(provide 'org-ocr)
;;; org-ocr.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment