Repository navigation
German Konzilsprotokolle
The question whether Tesseract works for handwritten text recognition has been asked multiple times. In the following, an experiment which might help to answer this question is documented.
We used a data set which was created in the context of the READ project by Dirk Alvermann (Universitätsarchiv Greifswald) and has been published via Zenodo:
Tobias Grüning, Gundram Leifert, Johannes Michael, Tobias Strauß, Max Weidemann, Roger Labahn. (2016). read_dataset_german_konzilsprotokolle [Data set]. Zenodo. http://doi.org/10.5281/zenodo.215383
It contains 8 770 transcribed text lines of handwritten historical documents from the late 18th century. They are represented as image-PAGE-XML pairs.
Download and extraction result in the following directory structure:
── german_konzilsprotokolle
├── data
│ └── Greifswald_Alvermann
│ ├── Copy_of_1794-95
│ │ ├── page
│ │ └── tif
│ ├── Copy_of_1795-96
│ │ ├── page
│ │ └── tif
│ ├── Copy_of_1796-97
│ │ ├── page
│ │ └── tif
│ ├── Copy_of_AA_1794-95
│ │ ├── page
│ │ └── tif
└── lists
Since tesstrain expects text-image pairs in the line level, the first step is to extract them from the (page) images using the coordinates given in the PAGE XML. Luckily, ocrd_segment offers an ease-of-use processor for this task. Entering the OCR-D ecosphere also gives us access to various image preprocessing operations such as (superior) binarization and denoising.
We assume a working installation of OCR-D's core module and the OCR-D modules ocrd_cis, ocrd_olena and ocrd_segment. Navigate to a directory of your choice and run
ocrd workspace init .
Images and XML files may be added to the workspace via
mkdir IMG
mkdir PAGE
for i in `find /path/to/german_konzilsprotokolle/data/Greifswald_Alvermann/Copy_of_1794-95 -name "*.tif"`; do base=`basename $i .tif`; echo $base; mv $i IMG/1794-95_${base}.tif; mv /path/to/german_konzilsprotokolle/data/Greifswald_Alvermann/Copy_of_1794-95/page/${base}.xml PAGE/1794-95_${base}.xml; done
for i in `find IMG -name "*.tif"`; do base=`basename $i .tif`; ocrd workspace add $i -G IMG -i ${base}_img -g $base -m 'image/tiff'; done
for i in `find PAGE -name "*.xml"`; do base=`basename $i .xml`; ocrd workspace add $i -G PAGE -i ${base}_page -g $base -m 'application/vnd.prima.page+xml'; done
For technical reasons, the attribute imageFilename of the element Page in the PAGE XML file names has to be adjusted accordingly. E.g.,
cd PAGE/
for i in `find . -name "1794-95_*"`; do echo $i; sed -i 's/imageFilename="/imageFilename="IMG\/1794-95_/' $i; done
Repeat these steps for the other Copy_of_ directories as well.
As an initial setup, we choose binarization following Wolf et al. (2002) and denoising (aka. despeckling) as provided by ocropy. Both operations can be comfortably applied to the existing line annotations using the corresponding OCR-D interfaces:
ocrd-olena-binarize -I PAGE -O WOLF,WOLF-IMG -m mets.xml -p <(echo '{"impl":"wolf"}')
ocrd-cis-ocropy-denoise -I WOLF -O DENOISE,DENOISE-IMG -m mets.xml -p '{"level-of-operation": "line"}'
This results in shiny line images. E.g.,

ocrd-segment-extract-lines -I DENOISE -O LINES -m mets.xml
import fitz # PyMuPDF import pytesseract from PIL import Image import io, re import pandas as pd from reportlab.platypus import SimpleDocTemplate, Table, TableStyle from reportlab.lib import colors
PDF_INPUT = "5.pdf" # input scanned PDF (your CamScanner file) CSV_OUTPUT = "contacts.csv" XLSX_OUTPUT = "contacts.xlsx" PDF_OUTPUT = "clean_contacts.pdf" DPI = 250 # OCR quality (200–300 is usually good)
gst_regex = re.compile(r'\b[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z][0-9A-Z]Z[0-9A-Z]\b') phone_regex = re.compile(r'(?:+91|0)?[6-9]\d{9}')
def ocr_page(page): # convert PDF page → image pix = page.get_pixmap(dpi=DPI) img = Image.open(io.BytesIO(pix.tobytes("png"))) # OCR text = pytesseract.image_to_string(img, lang="eng")
consignee, description, gst, phone = None, None, None, None
lines = [ln.strip() for ln in text.splitlines() if ln.strip()]
for i, line in enumerate(lines):
up = line.upper()
if "CONSIGNEE" in up:
consignee = line.split(":", 1)[-1].strip() if ":" in line else line
if "DESCRIPTION" in up:
description = line.split(":", 1)[-1].strip() if ":" in line else line
# regex search for GST and Phone
gst = gst_regex.search(text)
phone = phone_regex.search(text)
gst = gst.group(0) if gst else None
phone = phone.group(0) if phone else None
return consignee, description, gst, phone
def main(): doc = fitz.open(PDF_INPUT) rows = []
for i, page in enumerate(doc, start=1):
consignee, description, gst, phone = ocr_page(page)
if consignee or description or gst or phone:
rows.append([consignee, description, gst, phone])
df = pd.DataFrame(rows, columns=["Consignee", "Description", "GST Number", "Phone Number"])
# remove duplicates (based on Phone + GST)
df = df.drop_duplicates(subset=["Phone Number", "GST Number"])
# save Excel/CSV
df.to_csv(CSV_OUTPUT, index=False)
df.to_excel(XLSX_OUTPUT, index=False)
# save as clean PDF table
data = [list(df.columns)] + df.values.tolist()
pdf = SimpleDocTemplate(PDF_OUTPUT)
table = Table(data)
table.setStyle(TableStyle([
("BACKGROUND", (0,0), (-1,0), colors.lightgrey),
("GRID", (0,0), (-1,-1), 0.5, colors.black),
("FONTNAME", (0,0), (-1,0), "Helvetica-Bold"),
("ALIGN", (0,0), (-1,-1), "LEFT"),
]))
pdf.build([table])
print(f"✅ Done! Extracted {len(df)} unique contacts.")
print(f"Saved: {CSV_OUTPUT}, {XLSX_OUTPUT}, {PDF_OUTPUT}")
if name == "main": main()