Repository navigation
Akkadian Cuneiform
Akkadian is the language spoken and written in Ancient Mesopotamia, from around 2600 to 600 BCE, and had academic/liturgical use up to 100 AD. It was written using the Cuneiform script. Two most important variants of Akkadian are Babylonian and Assyrian. Cuneiform signs have different forms in these two variants, which becomes important below in the training section.
The data set was scraped from the website of the ORACC (Open Richly Annotated Cuneiform Corpus). Only those ORACC projects having texts written in Cuneiform were scraped: RIBo: Royal Inscriptions of Babylonia online, RINAP: Royal Inscriptions of the Neo-Assyrian Period, and SAAo: State Archives of Assyria Online.
The scraper was written in Python 3.5 and is uploaded here. For each scraped document, it opens the popup with cuneiform text, extracts its contents, cleans them up and saves them into text files. Then all the saved text files were simply concatenated to a single file. This file is the Tesseract training text corpus.
Text corpus was then processed with the create_dictdata tool from the pytesstrain package, which created additional files needed for training a Tesseract model. These training files were added both to the LSTM-based language data repository (here) and to the legacy language data repository (here).
The data set was used for training Tesseract models for Akkadian language. Below you can read about training the LSTM-based Tesseract model and then, for the sake of completeness, about training the legacy Tesseract model.
This setup was first created by Shreeshrii here and updated by the author here.
The training runs on the Ubuntu 22.04, or any other Linux distribution with Tesseract 4 or above.
First, update the package database and install the required packages (replace vim with your favourite editor):
apt update
apt install -y tesseract-ocr git vim bc python3-pip python3-venvThen, check out the language data and the training repository:
git clone https://github.com/wincentbalin/tesstrain-akk
cd tesstrain-akk
# Get a cup of tea after executing the next line
git clone https://github.com/tesseract-ocr/langdata_lstmCreate Python virtual environment, activate it and install required packages:
python3 -m venv venv
. ./venv/bin/activate
pip3 install kraken pytesstrainAdd ISRI Analytic Tools for OCR Evaluation with UTF-8 support:
apt install -y build-essential libutf8proc-dev
git clone https://github.com/eddieantonio/ocreval
cd ocreval
make
make install
cd ..Then install the ocrevalUAtion tool:
apt install -y wget default-jre
mkdir ~/ocrevaluation
wget -O ~/ocrevaluation/ocrevaluation.jar https://github.com/impactcentre/ocrevalUAtion/releases/download/v1.3.4/ocrevalUAtion-1.3.4-jar-with-dependencies.jarNow, install the fonts — just copy the *.[ot]tf from the following list to /usr/share/fonts:
- Akkadian (Old Babylonian, by George Douros)
- Assurbanipal (Neo-Assyrian, by Sylvie Vanséveren)
- Assyrian (Neo-Assyrian, by George Douros)
- CuneiformComposite (Neo-Assyrian, from ORACC)
- CuneiformNA (Neo-Assyrian, from ORACC)
- CuneiformOB (Old Babylonian, from ORACC)
- Santakku (Old Babylonian, by Sylvie Vanséveren)
- SantakkuM (Old Babylonian, by Sylvie Vanséveren)
- Segoe UI Historic (Neo-Assyrian, from Microsoft)
List the real font names and, if needed, update the font list (you can use any installed editor instead of vim):
text2image --fonts_dir /usr/share/fonts --list_available_fonts
vim akk.fontslist.txtCopy the language data for Akkadian:
cp -a langdata_lstm/akk langdataIf needed, adjust Tesseract configuration by adding the file akk.config to the language data:
cp akk.config langdataTo adjust the iteration count, go to the last line in the script
trainlayer.sh, which starts withMAX_ITERATIONS=, and change the value.
Then execute the scripts of Shreeshrii:
# Prepare training data
bash txt2img.sh | tee txt2img.log
bash img2lstmf.sh | tee img2lstmf.log
# Perform training
bash trainlayer.sh | tee trainlayer.log
# Evaluate results
bash checkpointeval.sh | tee reports/checkpointeval.txtThe models are created from checkpoint files while training and are copied to the directories data/akk/tessdata_best and data/akk/tessdata_fast. Study the evaluation results in the file reports/checkpointeval-summary.txt (more information in the file reports/checkpointeval.txt) and choose the most suitable model for your needs.
Training for Tesseract 3 was done using this Makefile. It performs a conventional Tesseract 3 training workflow. The fonts used are CuneiformNA, CuneiformOB and CuneiformComposite (all three downloaded from ORACC), as well as Segoe UI Historic (supplied with current Windows OS). The exposures used ranged from -3 to 3.
The training runs on the Ubuntu 16.04, or any other Linux distribution with Tesseract 3.04 or 3.05.
First, update the package database and install the required packages (replace vim with your favourite editor):
apt update
apt install -y tesseract-ocr git vim wget python3-pip python3-venvCreate Python virtual environment, activate it and install required packages (package versions are adjusted for Ubuntu 16.04, because of Python 3.5):
python3 -m venv venv
. ./venv/bin/activate
pip3 install wheel
pip3 install Pillow==7.2 pytesseract==0.3.6 pytesstrainTo be able to calculate additional font metrics, install Nick White's tools:
apt install -y libpango1.0-dev
git clone http://ancientgreekocr.org/grctraining.git
cd grctraining
make tools/addmetrics tools/xheight
cp tools/addmetrics tools/xheight /usr/local/bin
cd ..Add fonts like described in the previous section and, additionally, list their names:
text2image --fonts_dir /usr/share/fonts --list_available_fontsGet the Makefile:
wget https://gist.githubusercontent.com/wincentbalin/9329a6e994852ed477ba30ef4c29e71c/raw/0d2a8a76eea42698ede299406accca8b361c00fe/MakefileClone language data:
git clone https://github.com/tesseract-ocr/langdata.git ../langdataTo change the fonts to train with, edit the variables
FONTSandFONTSJOINED(the latter one is needed only becausemakedoes not process values with spaces correctly). Add complementing font rules at the bottom of theMakefile.
To change the training corpus, edit the variable
CORPUSor set it in the command line (i.e.make CORPUS=another_textfile.txt).
Run make or, to utilise 4 processors, make -j 4.
The training lasts ~1.5 days with supplied training text and 4 fonts. The resulting file is akk.traineddata in the current directory.
The .zip archive with models for legacy Tesseract and LSTM-based Tesseract both in best and in fast variant is available here. All models within this archive were trained with 9 fonts.
The models for LSTM-based Tesseract with 4 fonts are available here.
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()