mirror of
				https://github.com/qurator-spk/ocrd-galley.git
				synced 2025-10-31 19:24:12 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			171 lines
		
	
	
	
		
			4.5 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable file
		
	
	
	
	
			
		
		
	
	
			171 lines
		
	
	
	
		
			4.5 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable file
		
	
	
	
	
| #!/bin/bash
 | |
| LOG_LEVEL='DEBUG'
 | |
| 
 | |
| 
 | |
| set -e  # Abort on error
 | |
| if [ "$LOG_LEVEL" = "DEBUG" -o "$LOG_LEVEL" = "TRACE" ]; then
 | |
|   set -x
 | |
| fi
 | |
| 
 | |
| 
 | |
| remove_filegrp() {
 | |
|   # Remove the given file group from the workspace
 | |
| 
 | |
|   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
 | |
| }
 | |
| 
 | |
| do_validate() {
 | |
|   ocrd workspace validate --page-strictness lax mets.xml
 | |
|   # XXX ocrd-tesserocr INCONSISTENCY in TextRegion → use "--page-strictness lax" for now
 | |
| }
 | |
| 
 | |
| 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-OCR-FONTIDENT mets.xml
 | |
|   ocrd-typegroups-classifier -l $LOG_LEVEL \
 | |
|     -m mets.xml -I OCR-D-IMG -O OCR-D-OCR-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
 | |
| }
 | |
| 
 | |
| 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-tesserocr-segment-region -l $LOG_LEVEL \
 | |
|     -m mets.xml -I OCR-D-IMG-BIN -O OCR-D-SEG-REGION
 | |
|   ocrd-tesserocr-segment-line -l $LOG_LEVEL \
 | |
|     -m mets.xml -I OCR-D-SEG-REGION -O OCR-D-SEG-LINE
 | |
| 
 | |
|   # XXX compare ocrd-tesserocr-segment* vs tesseract native
 | |
| }
 | |
| 
 | |
| do_ocr() {
 | |
|   # Perform OCR on the segmented lines
 | |
| 
 | |
|   ocrd_tesserocr_recognize_parameters='{ "model": "frk" }'  # TODO mods:language + fontident → model
 | |
|   remove_filegrp OCR-D-OCR-TESS mets.xml
 | |
|   ocrd-tesserocr-recognize -l $LOG_LEVEL \
 | |
|     -m mets.xml -I OCR-D-SEG-LINE -O OCR-D-OCR-TESS \
 | |
|     -p <(echo $ocrd_tesserocr_recognize_parameters)
 | |
| }
 | |
| 
 | |
| page_validate_xml() {
 | |
|   # Validate all PAGE XML against the XML schema
 | |
| 
 | |
|   filegrp=$1
 | |
| 
 | |
|   local file
 | |
|   for file in `ocrd workspace find -G $filegrp`; do
 | |
|     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
 | |
| }
 | |
| 
 | |
| page_workaround_remove_conf() {
 | |
|   # XXX Work around https://github.com/OCR-D/core/issues/269
 | |
| 
 | |
|   filegrp=$1
 | |
| 
 | |
|   local file
 | |
|   for file in `ocrd workspace find -G $filegrp`; do
 | |
|     xmlstarlet ed --inplace \
 | |
|     -N 'page=http://schema.primaresearch.org/PAGE/gts/pagecontent/2019-07-15' \
 | |
|     -d '//page:TextEquiv/@conf' $file
 | |
|   done
 | |
| }
 | |
| 
 | |
| page_downgrade_to_2018() {
 | |
|   filegrp=$1
 | |
| 
 | |
|   local file
 | |
|   for file in `ocrd workspace find -G $filegrp`; do
 | |
|     sed -i 's#pagecontent/[0-9-]*#pagecontent/2018-07-15#g' $file
 | |
|   done
 | |
| }
 | |
| 
 | |
| 
 | |
| do_fontident
 | |
| do_validate
 | |
| 
 | |
| do_binarization
 | |
| do_validate
 | |
| 
 | |
| do_linesegmentation
 | |
| page_validate_xml           OCR-D-SEG-REGION
 | |
| page_validate_xml           OCR-D-SEG-LINE
 | |
| do_validate
 | |
| 
 | |
| do_ocr
 | |
| page_validate_xml           OCR-D-OCR-TESS
 | |
| page_workaround_remove_conf OCR-D-OCR-TESS
 | |
| do_validate
 | |
| 
 | |
| page_fix_image_references   OCR-D-OCR-TESS
 | |
| page_validate_xml           OCR-D-OCR-TESS
 | |
| do_validate
 | |
| 
 | |
| # As a last step, downgrade to PAGE 2018 to support PAGE Viewer
 | |
| page_downgrade_to_2018      OCR-D-OCR-TESS
 | |
| do_validate
 | |
| 
 | |
| 
 | |
| if ocrd workspace list-group | grep -q OCR-D-GT-PAGE; then
 | |
|   for g in `ocrd workspace list-page`; do
 | |
|     echo "== $g"
 | |
|     gt=`ocrd workspace find -G OCR-D-GT-PAGE -g $g`
 | |
|     ocr=`ocrd workspace find -G OCR-D-OCR-TESS -g $g`
 | |
| 
 | |
|     if [ -n "$gt" ]; then
 | |
|       python3 /usr/local/ocr-eval/cli.py $gt $ocr
 | |
|       mv report.json report-$g.json
 | |
|       mv report.html report-$g.html
 | |
|     fi
 | |
|   done
 | |
| fi
 | |
| 
 | |
| # vim:tw=120:
 |