You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ocrd-galley/my_ocrd_workflow

140 lines
4.1 KiB
Plaintext

5 years ago
#!/bin/bash
LOG_LEVEL='DEBUG'
5 years ago
set -e # Abort on error
if [ "$LOG_LEVEL" = "DEBUG" -o "$LOG_LEVEL" = "TRACE" ]; then
set -x
fi
5 years ago
remove_filegrp() {
# Remove the given file group from the workspace
5 years ago
filegrp_use=$1
mets=$2
xmlstarlet ed --inplace \
-N mets=http://www.loc.gov/METS/ \
-d "//mets:fileGrp[@USE='$filegrp_use']" $mets
# XXX See also https://github.com/OCR-D/core/issues/245
# XXX This should also delete the files (after checking if they are indeed inside the workspace) and the directory
5 years ago
}
do_binarization() {
# Binarize the images
remove_filegrp OCR-D-IMG-BIN mets.xml
ocrd-kraken-binarize -l $LOG_LEVEL \
-m mets.xml -I OCR-D-IMG -O OCR-D-IMG-BIN
}
do_fontident() {
# Identify fonts in the images
network=`python3 -c "import ocrd_typegroups_classifier, os; print(os.path.join(os.path.dirname(ocrd_typegroups_classifier.__file__), 'models', 'classifier.tgc'))"`
ocrd_typegroups_classifier_parameters="
{
\"network\": \"$network\",
\"stride\": 143
}"
remove_filegrp OCR-D-FONTIDENT mets.xml
ocrd-typegroups-classifier -l $LOG_LEVEL \
-m mets.xml -I OCR-D-IMG -O OCR-D-FONTIDENT \
-p <(echo $ocrd_typegroups_classifier_parameters)
# XXX Check if ocrd-typegroups-classifier uses the whole image
# XXX does DEFAULT have any meaning? /buerger_gedichte_1778.ocrd does not have
# any DEFAULT, yet -I DEFAULT seems to work for ocrd-typegroups-classifier
#ocrd workspace validate mets.xml
# XXX <error>Unspecified USE category 'FONTIDENT' in fileGrp 'OCR-D-FONTIDENT'</error>
# XXX <error>File 'OCR-D-FONTIDENT_OCR-D-IMG_0002' does not manifest any physical page.</error>
# XXX <notice>Won't download remote image <OCR-D-IMG/OCR-D-IMG_0002></notice>
}
do_linesegmentation() {
# Segment the lines in the binarized images
remove_filegrp OCR-D-SEG-REGION mets.xml
remove_filegrp OCR-D-SEG-LINE mets.xml
#ocrd-ocropy-segment -l $LOG_LEVEL \
# -m mets.xml -I OCR-D-IMG-BIN -O OCR-D-SEG-LINE
# XXX ocrd-ocropy-segment throws an exception for buerger_gedichte_1778.ocrd
#ocrd workspace validate mets.xml
ocrd-tesserocr-segment-region -l $LOG_LEVEL \
-m mets.xml -I OCR-D-IMG-BIN -O OCR-D-SEG-REGION
#ocrd workspace validate mets.xml
ocrd-tesserocr-segment-line -l $LOG_LEVEL \
-m mets.xml -I OCR-D-SEG-REGION -O OCR-D-SEG-LINE
#ocrd workspace validate mets.xml
# XXX compare ocrd-tesserocr-segment* vs tesseract native
}
5 years ago
do_ocr() {
# Perform OCR on the segmented lines
5 years ago
ocrd_tesserocr_recognize_parameters='{ "model": "frk" }' # TODO mods:language + fontident → model
5 years ago
remove_filegrp OCR-D-OCR-TESS mets.xml
ocrd-tesserocr-recognize -l $LOG_LEVEL \
5 years ago
-m mets.xml -I OCR-D-SEG-LINE -O OCR-D-OCR-TESS \
-p <(echo $ocrd_tesserocr_recognize_parameters)
#ocrd workspace validate mets.xml
# XXX <error>INCONSISTENCY in TextRegion ID 'dummy'
# (The whitespace is different)
5 years ago
}
page_fix_xml() {
# Fix the PAGE XML generated by OCR-D core
#
# XXX core does not produce valid XML (See https://github.com/OCR-D/core/issues/242), fix it by setting the correct
# PAGE XML version. This makes PAGE Viewer open the file.
filegrp=$1
local file
for file in `ocrd workspace find -G $filegrp`; do
5 years ago
sed -i 's#pagecontent/2017-07-15#pagecontent/2019-07-15#g' $file
sed -i 's#pagecontent/2018-07-15#pagecontent/2019-07-15#g' $file
done
}
page_validate_xml() {
# Validate all PAGE XML against the XML schema
filegrp=$1
local file
for file in `ocrd workspace find -G $filegrp`; do
5 years ago
xmllint --noout --schema `dirname $0`/xsd/pagecontent.2019-07-15.xsd $file
done
}
page_fix_image_references() {
# Make image references relative to the PAGE XML file
#
# The rest of OCR-D probably isn't going to like it, but it is a. correct and b. makes PAGE Viewer open the image file
# automatically.
filegrp=$1
local file
for file in `ocrd workspace find -G $filegrp`; do
sed -i 's#imageFilename="OCR-D-IMG#imageFilename="../OCR-D-IMG#g' $file
done
}
do_fontident
do_binarization
do_linesegmentation
5 years ago
do_ocr
page_fix_xml OCR-D-OCR-TESS
page_validate_xml OCR-D-OCR-TESS
page_fix_image_references OCR-D-OCR-TESS
5 years ago
# vim:tw=120: