Repository navigation
Training Fraktur
Tesseract comes with several models which are specialized for Fraktur texts.
- dan_frak (Danish Fraktur)
- deu_frak (German Fraktur)
- slk_frak (Slovakian Fraktur)
- frk (German Fraktur)
- script/Fraktur
Those models also support historic and modern Antiqua scripts.
Some other models are primarly made for modern Antiqua scripts, but have a very limited ability to recognize Fraktur and historic Antiqua, too.
- deu (German)
- script/Latin
Neither of the above models is really good as a general model for Fraktur and historic Antiqua texts because each of them has specific problems.
dan_frak, deu_frak and slk_frak are language specific, so they only support a limited set of characters.
They can only be used with the old legacy recognizer, not with the newer LSTM (neural network) recognizer.
Typically (not always!) the results from the legacy recognizer are worse than those from the LSTM recognizer.
frk supports the German character set, but important characters like for example § are missing and will never be recognized. In addition, some ligatures like ch and ck were trained wrongly and will therefore be recognized as < and >. script/Fraktur supports a larger international character set, but otherwise has the same issues as frk.
So to summarize, other models are needed for Fraktur and historic Antiqua. Such models can be trained either from scratch or based on one of the existing standard models.
This is a collection of sources for training OCR models which can be used to recognize Fraktur. A more complete list which is not restricted to Fraktur only can be found at https://github.com/cneud/ocr-gt.
Austrian Newspapers is a ground truth data set created from Austrian newspapers by the Austrian National Library (Österreichische Nationalbibliothek).See https://github.com/tesseract-ocr/tesstrain/wiki/AustrianNewspapers for more information.
GT4HistOCR is ground truth for training OCR engines on historical documents in German Fraktur and Early Modern Latin. See https://github.com/tesseract-ocr/tesstrain/wiki/GT4HistOCR for more information.
https://github.com/jze/ocropus-model_fraktur provides ground truth data, 3852 lines for training and 414 lines for testing, both of good quality.
- Some umlauts might be replaced by aͤ, oͤ, uͤ.
- It uses the minus sign instead of ⸗.
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()