| 
									
										
										
										
											2019-06-19 12:22:41 +02:00
										 |  |  | #!/bin/bash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | set -e  # Abort on error | 
					
						
							| 
									
										
										
										
											2020-02-07 17:12:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-12 14:48:22 +01:00
										 |  |  | # Configuration | 
					
						
							| 
									
										
										
										
											2020-02-07 17:12:51 +01:00
										 |  |  | 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 | 
					
						
							| 
									
										
										
										
											2020-02-12 14:48:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 12:26:38 +01:00
										 |  |  | # Command line parameters | 
					
						
							| 
									
										
										
										
											2020-03-09 16:50:30 +01:00
										 |  |  | OPTS=`getopt -o I: --long input-file-grp:,skip-validation -- "$@"` | 
					
						
							| 
									
										
										
										
											2020-03-09 12:26:38 +01:00
										 |  |  | eval set -- "$OPTS" | 
					
						
							|  |  |  | INPUT_FILE_GRP=OCR-D-IMG | 
					
						
							| 
									
										
										
										
											2020-03-09 16:50:30 +01:00
										 |  |  | SKIP_VALIDATION=false | 
					
						
							| 
									
										
										
										
											2020-03-09 12:26:38 +01:00
										 |  |  | 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;; | 
					
						
							| 
									
										
										
										
											2020-03-09 12:26:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     --) shift; break;; | 
					
						
							|  |  |  |     *) break;; | 
					
						
							|  |  |  |   esac | 
					
						
							|  |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-12 14:48:22 +01:00
										 |  |  | # 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() { | 
					
						
							| 
									
										
										
										
											2020-02-10 19:25:08 +01:00
										 |  |  |   # 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=' | 
					
						
							| 
									
										
										
										
											2020-02-07 14:13:32 +01:00
										 |  |  |     --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" | 
					
						
							|  |  |  |   }' | 
					
						
							| 
									
										
										
										
											2020-02-07 14:26:20 +01:00
										 |  |  |   ocrd workspace remove-group -rf OCR-D-IMG-BINPAGE | 
					
						
							|  |  |  |   ocrd workspace remove-group -rf OCR-D-IMG-BIN | 
					
						
							| 
									
										
										
										
											2020-03-09 12:26:38 +01:00
										 |  |  |   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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 19:16:43 +02:00
										 |  |  | do_linesegmentation_tesserocr() { | 
					
						
							| 
									
										
										
										
											2019-07-03 12:22:55 +02:00
										 |  |  |   # Segment the lines in the binarized images | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 14:26:20 +01:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2019-06-19 12:51:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 19:16:43 +02:00
										 |  |  | 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" | 
					
						
							|  |  |  |   }' | 
					
						
							| 
									
										
										
										
											2020-02-07 14:26:20 +01:00
										 |  |  |   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-10-11 19:16:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							| 
									
										
										
										
											2020-02-12 14:48:22 +01:00
										 |  |  |     "textequiv_level": "'$TEXTEQUIV_LEVEL'" | 
					
						
							| 
									
										
										
										
											2020-02-07 18:46:33 +01:00
										 |  |  |   }' | 
					
						
							| 
									
										
										
										
											2020-02-07 14:26:20 +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-06-19 12:51:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 11:54:01 +02:00
										 |  |  | do_ocr_calamari() { | 
					
						
							| 
									
										
										
										
											2020-02-07 18:47:16 +01:00
										 |  |  |   # 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", | 
					
						
							| 
									
										
										
										
											2020-02-12 14:48:22 +01:00
										 |  |  |     "textequiv_level": "'$TEXTEQUIV_LEVEL'" | 
					
						
							| 
									
										
										
										
											2020-02-07 13:31:26 +01:00
										 |  |  |   }' | 
					
						
							| 
									
										
										
										
											2020-02-07 14:26:20 +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 | 
					
						
							| 
									
										
										
										
											2019-09-30 18:25:54 +02:00
										 |  |  |     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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 19:16:43 +02:00
										 |  |  | page_upgrade_to_2019() { | 
					
						
							| 
									
										
										
										
											2020-02-07 18:47:16 +01:00
										 |  |  |   # Upgrade PAGE files to 2019-07-15 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 19:16:43 +02:00
										 |  |  |   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-08-05 15:40:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-27 13:16:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  | main() { | 
					
						
							| 
									
										
										
										
											2020-06-19 19:27:32 +02:00
										 |  |  |   do_validate | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |   do_binarization | 
					
						
							|  |  |  |   do_validate | 
					
						
							| 
									
										
										
										
											2019-09-30 12:26:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 15:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01: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-09-30 12:26:14 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 15:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |   do_ocr_calamari | 
					
						
							| 
									
										
										
										
											2019-08-21 13:07:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-21 11:54:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |   do_ocr | 
					
						
							| 
									
										
										
										
											2019-08-21 13:07:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 15:27:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |   for ocr_filegrp in OCR-D-OCR-CALAMARI OCR-D-OCR-TESS; do | 
					
						
							| 
									
										
										
										
											2019-06-19 12:22:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |     page_validate_xml           $ocr_filegrp | 
					
						
							|  |  |  |     do_validate | 
					
						
							| 
									
										
										
										
											2019-08-05 18:46:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |     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 | 
					
						
							| 
									
										
										
										
											2019-08-21 13:07:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  |   done | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-13 15:02:15 +01:00
										 |  |  | if [ "$LOG_LEVEL" = "DEBUG" -o "$LOG_LEVEL" = "TRACE" ]; then | 
					
						
							|  |  |  |   pip3 list | 
					
						
							|  |  |  | fi | 
					
						
							| 
									
										
										
										
											2020-02-07 18:57:46 +01:00
										 |  |  | main | 
					
						
							| 
									
										
										
										
											2019-08-21 13:07:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 18:13:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 12:39:42 +02:00
										 |  |  | # vim:tw=120: |