-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
170 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#!/usr/bin/env bash | ||
|
||
# BSD 3-Clause License | ||
|
||
# Copyright (c) 2017,2018,2019,2020, Maxim Konakov | ||
# All rights reserved. | ||
|
||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
|
||
# * Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
|
||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
|
||
# * Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
|
||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
|
||
# This script checks parameters before invoking the "tesseract" tool, because | ||
# the error messages from the tool are rather cryptic. | ||
|
||
# error code | ||
declare -ri CODE=255 # this stops xargs | ||
|
||
# error exit | ||
die() { | ||
echo >&2 "$0: file \"$FILE\": $*" | ||
exit $CODE | ||
} | ||
|
||
# error exit with message prefix cut off | ||
die_() { | ||
die "$(cut -d ' ' -f 2- <<< "$*")" | ||
} | ||
|
||
# display usage string and exit | ||
exit_with_usage() { | ||
cat <<-EOF >&2 | ||
Usage: $(basename "$0") FILE [OPTION]... | ||
Run OCR on the given image FILE. Recognised text is written to STDOUT. | ||
All the given options are passed down to the "tesseract" tool. | ||
EOF | ||
|
||
exit $CODE | ||
} | ||
|
||
# check input parameters | ||
(( $# > 0 )) || exit_with_usage | ||
|
||
# input file name | ||
FILE="$1" | ||
shift | ||
|
||
# tesseract version check | ||
TV="$(tesseract -v | head -q -n 1 | cut -s -d ' ' -f 2)" || exit $CODE | ||
|
||
(( ${TV%%.[0-9]*} >= 4 )) || die "supported \"tesseract\" version is 4.0.0 or later, this one is \"$TV\"" | ||
|
||
# find and check the parameter for tesseract -l option, because otherwise | ||
# the error message will be really confusing. First, find '-l' option | ||
for (( i = 1; i <= $#; i++ )); do | ||
[[ "${!i}" == '-l' ]] && break | ||
done | ||
|
||
(( i == $# )) && die "option error: missing parameter for \"-l\" option" | ||
|
||
# if found, then check the option parameter | ||
if (( i < $# )); then | ||
# get option parameter and split on '+' character | ||
i=$(( i + 1 )) | ||
IFS='+' read -ra LOPT <<< "${!i}" | ||
|
||
# list of supported languages | ||
mapfile -s 1 -t LANGS < <(tesseract --list-langs) | ||
|
||
# match parameters to supported languages | ||
for l in "${LOPT[@]}"; do | ||
for (( i = 0; i < ${#LANGS[@]}; i++ )); do | ||
[[ "$l" == "${LANGS[i]}" ]] && break | ||
done | ||
|
||
(( i < ${#LANGS[@]} )) || die "option error: language \"$l\" is not supported" | ||
done | ||
fi | ||
|
||
# check the input file MIME type | ||
T="$(file -ELb --mime-type "$FILE")" || die_ "$T" | ||
|
||
[[ "$T" =~ ^image/(.{,4}) && "${BASH_REMATCH[1]}" != 'vnd.' ]] \ | ||
|| die "unsupported MIME type \"$T\"" | ||
|
||
# temp file for OCR errors | ||
ERR="$(mktemp --tmpdir)" || exit $CODE | ||
trap 'rm -f "$ERR"' EXIT | ||
|
||
# OCR | ||
tesseract "$FILE" - -c page_separator='' "$@" 2>"$ERR" \ | ||
|| die "tesseract error: $(grep -m 1 -iE '^Error\>' "$ERR")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters