forked from tmbarchive/docker-ocropus
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun-ocropy
executable file
·85 lines (77 loc) · 2.67 KB
/
run-ocropy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
typeset -A ALLOWED_COMMANDS
ALLOWED_COMMANDS[dewarp]="dewarp lines as used for the recognizer"
ALLOWED_COMMANDS[econf]="Compute the edit distances between ground truth and recognizer output"
ALLOWED_COMMANDS[errs]="Compute the edit distances between ground truth and recognizer output"
ALLOWED_COMMANDS[gpageseg]="Segment a page"
ALLOWED_COMMANDS[gtedit]="Edit ground truth"
ALLOWED_COMMANDS[hocr]="Construct an HTML output file in hOCR format"
ALLOWED_COMMANDS[linegen]="Generate text line training data"
ALLOWED_COMMANDS[lpred]="run an RNN recognizer"
ALLOWED_COMMANDS[ltrain]="train an RNN recognizer"
ALLOWED_COMMANDS[nlbin]="Image binarization using non-linear processing"
ALLOWED_COMMANDS[rpred]="apply an RNN recognizer"
ALLOWED_COMMANDS[rtrain]="train an RNN recognizer"
ALLOWED_COMMANDS[visualize-results]="Generate HTML for debugging a book directory"
DOCKER_IMAGE=${DOCKER_IMAGE:-kbai/ocropy}
WORKING_DIR="$(realpath "$PWD")"
help() {
echo "$0 [--help] [--sudo] [--image <image-name>] [--working-dir <working-dir>] <cmd> [args...]"
echo ""
if [[ -n "$1" ]];then
printf "!! %s !!\n\n" "$1"
fi
echo "Options:"
echo " --sudo Run as sudo"
echo " --image <image-name> Use this image (Default: $DOCKER_IMAGE)"
echo " --working-dir <working-dir> Mount this dir (Default: $WORKING_DIR)"
echo " <cmd> Execute this ocropy-<cmd> command"
echo " [args...] Depend on the command. See ocropy docs."
echo ""
echo "Commands:"
for cmd in "${!ALLOWED_COMMANDS[@]}";do
echo -e " * $cmd\t- ${ALLOWED_COMMANDS[$cmd]}"
done
if [[ -z "$1" ]];then
exit 0
else
exit 1
fi
}
#
# Parse options
#
while [[ $1 =~ ^-- ]];do
case "$1" in
--image) DOCKER_IMAGE=$2; shift ;;
--working-dir) WORKING_DIR=$2; shift ;;
--sudo) SUDO="sudo" ;;
--help) help ;;
*) help "Unknown Option: $1" ;;
esac
shift;
done
#
# Command
#
CMD=$1
shift
if [[ -z "$CMD" ]];then
help 'Must pass <cmd>'
elif [[ -z "${ALLOWED_COMMANDS[$CMD]}" ]];then
help "Unknown command '$CMD'. Allowed: ${!ALLOWED_COMMANDS[*]}"
fi
declare -a ARGS=()
for maybeFile in "$@";do
if [[ -e "$maybeFile" ]];then
fullpath=$(realpath "$maybeFile")
if [[ "$fullpath" != "$WORKING_DIR"* ]];then
echo -e "\x1b[31;1mERROR\x1b[0m File '$maybeFile' not in WORKING_DIR '$WORKING_DIR'"
exit 1
fi
ARGS+=("$(realpath --relative-to="$WORKING_DIR" "$maybeFile")")
else
ARGS+=("$maybeFile")
fi
done
$SUDO docker run --rm -v "$WORKING_DIR:/work" "$DOCKER_IMAGE" "ocropus-$CMD" "${ARGS[@]}"