ocrd-galley/my_ocrd_workflow

177 lines
4.3 KiB
Text
Raw Normal View History

2019-06-19 12:22:41 +02:00
#!/bin/bash
set -e # Abort on error
# Configuration
export LOG_LEVEL=${LOG_LEVEL:-DEBUG} # /etc/ocrd_logging.py uses this to set level for all OCR-D modules
2020-02-13 16:13:24 +01:00
export TEXTEQUIV_LEVEL=glyph
# Command line parameters
2020-03-09 16:50:30 +01:00
OPTS=`getopt -o I: --long input-file-grp:,skip-validation -- "$@"`
eval set -- "$OPTS"
INPUT_FILE_GRP=OCR-D-IMG
2020-03-09 16:50:30 +01:00
SKIP_VALIDATION=false
while true; do
case "$1" in
-I|--input-file-grp) INPUT_FILE_GRP=$2; shift 2;;
2020-03-09 16:50:30 +01:00
--skip-validation) SKIP_VALIDATION=true; shift;;
--) shift; break;;
*) break;;
esac
done
# Set up logging
2019-06-19 17:48:38 +02:00
if [ "$LOG_LEVEL" = "DEBUG" -o "$LOG_LEVEL" = "TRACE" ]; then
set -x
fi
2019-06-19 12:22:41 +02:00
2019-08-05 15:27:38 +02:00
do_validate() {
# Validate the workspace
# Both ocrd_tesserocr + ocrd_calamari produce segment coordinates that are not strictly within their parent's
# coordinates:
#
# INCONSISTENCY in [...] coords [...] not within parent coords
#
# → --page-coordinate-consistency off
#
# ocrd_tesserocr sometimes produces segment text results that aren't concatenating as expected by the validator:
#
# INCONSISTENCY in [...]: text results '[...]' != concatenated '[...]'
#
# → --page-strictness lax
#
2020-02-07 18:46:33 +01:00
validate_options='
--skip dimension
--skip pixel_density
--page-strictness lax
2020-02-07 18:46:33 +01:00
--page-coordinate-consistency off'
2020-03-09 16:50:30 +01:00
if [ "$SKIP_VALIDATION" = false ]; then
ocrd workspace validate $validate_options
fi
2019-08-05 15:27:38 +02:00
}
2019-06-24 12:34:08 +02:00
do_binarization() {
2019-07-03 12:22:55 +02:00
# Binarize the images
2020-02-07 18:46:33 +01:00
ocrd_olena_binarize_parameters='{
"impl": "sauvola-ms-split"
}'
ocrd workspace remove-group -rf OCR-D-IMG-BINPAGE
ocrd workspace remove-group -rf OCR-D-IMG-BIN
ocrd-olena-binarize -I $INPUT_FILE_GRP -O OCR-D-IMG-BINPAGE,OCR-D-IMG-BIN -p "$ocrd_olena_binarize_parameters"
2019-06-24 12:34:08 +02:00
}
do_linesegmentation_tesserocr() {
2019-07-03 12:22:55 +02:00
# Segment the lines in the binarized images
ocrd workspace remove-group -rf OCR-D-SEG-REGION
ocrd workspace remove-group -rf OCR-D-SEG-LINE
2020-02-07 18:46:33 +01:00
ocrd-tesserocr-segment-region -I OCR-D-IMG-BINPAGE -O OCR-D-SEG-REGION
ocrd-tesserocr-segment-line -I OCR-D-SEG-REGION -O OCR-D-SEG-LINE
}
do_linesegmentation_sbb() {
# Segment the lines in the images
2020-02-07 18:46:33 +01:00
ocrd_sbb_textline_detector_parameters='{
"model": "/var/lib/textline_detection"
}'
ocrd workspace remove-group -rf OCR-D-SEG-REGION
ocrd workspace remove-group -rf OCR-D-SEG-LINE
2020-02-07 18:46:33 +01:00
ocrd-sbb-textline-detector -I OCR-D-IMG-BINPAGE -O OCR-D-SEG-LINE -p "$ocrd_sbb_textline_detector_parameters"
}
2019-06-19 13:02:54 +02:00
do_ocr() {
2019-07-03 12:22:55 +02:00
# Perform OCR on the segmented lines
2020-02-07 18:46:33 +01:00
ocrd_tesserocr_recognize_parameters='{
2020-02-10 19:26:04 +01:00
"model": "GT4HistOCR_2000000",
"textequiv_level": "'$TEXTEQUIV_LEVEL'"
2020-02-07 18:46:33 +01:00
}'
ocrd workspace remove-group -rf OCR-D-OCR-TESS
2020-02-07 18:46:33 +01:00
ocrd-tesserocr-recognize -I OCR-D-SEG-LINE -O OCR-D-OCR-TESS -p "$ocrd_tesserocr_recognize_parameters"
2019-06-19 13:02:54 +02:00
}
2019-08-21 11:54:01 +02:00
do_ocr_calamari() {
# Perform OCR on the segmented lines
2020-02-07 13:31:26 +01:00
ocrd_calamari_recognize_parameters='{
"checkpoint": "/var/lib/calamari-models/GT4HistOCR/2019-07-22T15:49+0200/*.ckpt.json",
"textequiv_level": "'$TEXTEQUIV_LEVEL'"
2020-02-07 13:31:26 +01:00
}'
ocrd workspace remove-group -rf OCR-D-OCR-CALAMARI
2020-02-07 18:46:33 +01:00
ocrd-calamari-recognize -I OCR-D-SEG-LINE -O OCR-D-OCR-CALAMARI -p "$ocrd_calamari_recognize_parameters"
2019-08-21 11:54:01 +02:00
}
2019-06-19 14:55:50 +02:00
page_validate_xml() {
2019-07-03 12:22:55 +02:00
# Validate all PAGE XML against the XML schema
2019-06-19 14:55:50 +02:00
filegrp=$1
local file
for file in `ocrd workspace find -G $filegrp`; do
XSD_DIR=`dirname $0`/xsd
if [ ! -d "$XSD_DIR" ]; then
XSD_DIR=/usr/share/xml
fi
xmllint --noout --schema $XSD_DIR/pagecontent.2019-07-15.xsd $file
2019-06-19 14:55:50 +02:00
done
}
page_upgrade_to_2019() {
# Upgrade PAGE files to 2019-07-15
filegrp=$1
local file
for file in `ocrd workspace find -G $filegrp`; do
sed -i 's#pagecontent/[0-9-]*#pagecontent/2019-07-15#g' $file
done
}
2019-09-27 13:16:14 +02:00
main() {
do_validate
do_binarization
do_validate
2019-08-05 15:27:38 +02:00
do_linesegmentation_sbb
page_upgrade_to_2019 OCR-D-SEG-LINE
page_validate_xml OCR-D-SEG-REGION
page_validate_xml OCR-D-SEG-LINE
do_validate
2019-08-05 15:27:38 +02:00
do_ocr_calamari
2019-08-21 11:54:01 +02:00
do_ocr
2019-08-05 15:27:38 +02:00
for ocr_filegrp in OCR-D-OCR-CALAMARI OCR-D-OCR-TESS; do
2019-06-19 12:22:41 +02:00
page_validate_xml $ocr_filegrp
do_validate
if ocrd workspace list-group | grep -q OCR-D-GT-PAGE; then
ocrd workspace remove-group -rf $ocr_filegrp-EVAL
ocrd-dinglehopper -I OCR-D-GT-PAGE,$ocr_filegrp -O $ocr_filegrp-EVAL
fi
done
}
if [ "$LOG_LEVEL" = "DEBUG" -o "$LOG_LEVEL" = "TRACE" ]; then
pip3 list
fi
main
2019-08-13 18:13:49 +02:00
2019-06-19 12:39:42 +02:00
# vim:tw=120: