forked from knaw-huc/loghi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathna-pipeline.sh
executable file
·216 lines (174 loc) · 7.62 KB
/
na-pipeline.sh
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# Stop on error, if set to 1 will exit program if any of the docker commands fail
STOPONERROR=1
# set to 1 if you want to enable, 0 otherwise, select just one
BASELINELAYPA=1
#
#LAYPAMODEL=/home/rutger/src/laypa-models/general/baseline/config.yaml
#LAYPAMODELWEIGHTS=/home/rutger/src/laypa-models/general/baseline/model_best_mIoU.pth
LAYPAMODEL=INSERT_FULL_PATH_TO_YAML_HERE
LAYPAMODELWEIGHTS=INSERT_FULLPATH_TO_PTH_HERE
# set to 1 if you want to enable, 0 otherwise, select just one
HTRLOGHI=1
#HTRLOGHIMODEL=/home/rutger/src/loghi-htr-models/republic-2023-01-02-base-generic_new14-2022-12-20-valcer-0.0062/
HTRLOGHIMODEL=INSERT_FULL_PATH_TO_LOGHI_HTR_MODEL_HERE
# set this to 1 for recalculating reading order, line clustering and cleaning.
RECALCULATEREADINGORDER=1
# if the edge of baseline is closer than x pixels...
RECALCULATEREADINGORDERBORDERMARGIN=50
# clean if 1
RECALCULATEREADINGORDERCLEANBORDERS=0
# how many threads to use
RECALCULATEREADINGORDERTHREADS=4
#detect language of pagexml, set to 1 to enable, disable otherwise
DETECTLANGUAGE=1
#interpolate word locations
SPLITWORDS=1
#used gpu ids, set to "-1" to use CPU, "0" for first, "1" for second, etc
GPU=0
DOCKERLOGHITOOLING=loghi/docker.loghi-tooling
DOCKERLAYPA=loghi/docker.laypa
DOCKERLOGHIHTR=loghi/docker.htr
# DO NO EDIT BELOW THIS LINE
if [ -z $1 ]; then echo "please provide path to images to be HTR-ed" && exit 1; fi;
tmpdir=$(mktemp -d)
echo $tmpdir
DOCKERGPUPARAMS=""
if [[ $GPU -gt -1 ]]; then
DOCKERGPUPARAMS="--gpus device=${GPU}"
echo "using GPU ${GPU}"
fi
SRC=$1
mkdir $tmpdir/imagesnippets/
mkdir $tmpdir/linedetection
mkdir $tmpdir/output
find $SRC -name '*.done' -exec rm -f "{}" \;
if [[ $BASELINELAYPA -eq 1 ]]
then
echo "starting Laypa baseline detection"
input_dir=$SRC
output_dir=$SRC
LAYPADIR="$(dirname "${LAYPAMODEL}")"
if [[ ! -d $input_dir ]]; then
echo "Specified input dir (${input_dir}) does not exist, stopping program"
exit 1
fi
if [[ ! -d $output_dir ]]; then
echo "Could not find output dir (${output_dir}), creating one at specified location"
mkdir -p $output_dir
fi
docker run $DOCKERGPUPARAMS --rm -it -u $(id -u ${USER}):$(id -g ${USER}) -m 32000m -v $LAYPADIR:$LAYPADIR -v $input_dir:$input_dir -v $output_dir:$output_dir $DOCKERLAYPA \
python run.py \
-c $LAYPAMODEL \
-i $input_dir \
-o $output_dir \
--opts MODEL.WEIGHTS "" TEST.WEIGHTS $LAYPAMODELWEIGHTS
# > /dev/null
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v $output_dir:$output_dir $DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionExtractBaselines \
-input_path_png $output_dir/page/ \
-input_path_page $output_dir/page/ \
-output_path_page $output_dir/page/ \
-as_single_region true
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
fi
# #HTR option 1 LoghiHTR
if [[ $HTRLOGHI -eq 1 ]]
then
echo "starting Loghi HTR"
# #pylaia takes 3 channels, rutgerhtr 4channels png or 3 with new models
docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm \
-v $SRC/:$SRC/ \
-v $tmpdir:$tmpdir \
$DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionCutFromImageBasedOnPageXMLNew \
-input_path $SRC \
-outputbase $tmpdir/imagesnippets/ \
-output_type png \
-channels 4 \
-threads 4
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
find $tmpdir/imagesnippets/ -type f -name '*.png' > $tmpdir/lines.txt
LOGHIDIR="$(dirname "${HTRLOGHIMODEL}")"
docker run $DOCKERGPUPARAMS -u $(id -u ${USER}):$(id -g ${USER}) --rm -m 32000m --shm-size 10240m -ti -v /tmp:/tmp -v $tmpdir:$tmpdir -v $LOGHIDIR:$LOGHIDIR $DOCKERLOGHIHTR \
bash -c "LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4 python3 /src/src/main.py \
--do_inference \
--existing_model $HTRLOGHIMODEL \
--batch_size 64 \
--use_mask \
--inference_list $tmpdir/lines.txt \
--results_file $tmpdir/results.txt \
--charlist $HTRLOGHIMODEL/charlist.txt \
--gpu $GPU \
--output $tmpdir/output/ \
--config_file_output $tmpdir/output/config.json \
--beam_width 10 "
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v $SRC/:$SRC/ -v $tmpdir:$tmpdir $DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionLoghiHTRMergePageXML \
-input_path $SRC/page \
-results_file $tmpdir/results.txt \
-config_file $tmpdir/output/config.json
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
fi
if [[ $RECALCULATEREADINGORDER -eq 1 ]]
then
echo "recalculating reading order"
if [[ $RECALCULATEREADINGORDERCLEANBORDERS -eq 1 ]]
then
echo "and cleaning"
docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v $SRC/:$SRC/ -v $tmpdir:$tmpdir $DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionRecalculateReadingOrderNew \
-input_dir $SRC/page/ \
-border_margin $RECALCULATEREADINGORDERBORDERMARGIN \
-clean_borders \
-threads $RECALCULATEREADINGORDERTHREADS
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
else
docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v $SRC/:$SRC/ -v $tmpdir:$tmpdir $DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionRecalculateReadingOrderNew \
-input_dir $SRC/page/ \
-border_margin $RECALCULATEREADINGORDERBORDERMARGIN \
-threads $RECALCULATEREADINGORDERTHREADS
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
fi
fi
if [[ $DETECTLANGUAGE -eq 1 ]]
then
echo "detecting language..."
docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v $SRC/:$SRC/ -v $tmpdir:$tmpdir $DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionDetectLanguageOfPageXml \
-page $SRC/page/
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
fi
if [[ $SPLITWORDS -eq 1 ]]
then
echo "MinionSplitPageXMLTextLineIntoWords..."
docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v $SRC/:$SRC/ -v $tmpdir:$tmpdir $DOCKERLOGHITOOLING /src/loghi-tooling/minions/target/appassembler/bin/MinionSplitPageXMLTextLineIntoWords \
-input_path $SRC/page/
if [[ $STOPONERROR && $? -ne 0 ]]; then
echo "Command has errored has errored, stopping program"
exit 1
fi
fi
# cleanup results
rm -rf $tmpdir