File size: 3,687 Bytes
0303b0a
 
 
 
 
 
 
 
 
213e631
5f4b87d
 
213e631
0303b0a
 
 
 
 
213e631
 
 
 
 
 
 
 
 
0303b0a
a7fec02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213e631
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
---
dataset_info:
  features:
  - name: image
    dtype: image
  - name: text
    dtype: string
  splits:
  - name: train
    num_bytes: 508398188
    num_examples: 441
  download_size: 506925962
  dataset_size: 508398188
configs:
- config_name: default
  data_files:
  - split: train
    path: data/train-*
license: cc-by-4.0
task_categories:
- image-to-text
language:
- sa
tags:
- ocr
size_categories:
- n<1K
---

# Sanskrit Dataset

## Overview

This dataset is a conversion of the Pracalit for Sanskrit and Newar MSS 16th to 19th C., Ground Truth dataset, originally published on [Zenodo](https://zenodo.org/records/6967421). The dataset contains pairs of images and corresponding plain text extracted from XML files. This dataset specifically includes the ground truth data from the original repository.

## Dataset Description

- **Images**: The original images from the dataset.
- **Text**: The text is extracted from XML files using the PAGE XML schema.

## Potential Use for VLM-based OCR Training

The existing ground truth OCR data can be particularly useful for bootstrapping datasets for Vision Language Model (VLM) based OCR training. By leveraging the high-quality annotations provided in this dataset, researchers can train models to recognize and transcribe text from images more accurately. This can be especially beneficial for languages and scripts that are underrepresented in existing OCR datasets.

## Processing Script

The dataset was processed using the following script:

```python
import os
import xml.etree.ElementTree as ET
from pathlib import Path
from PIL import Image as PILImage
import datasets
from datasets import Image, Features, Dataset
from tqdm import tqdm

def extract_text_from_xml(xml_path):
    """Extract plain text from XML file."""
    tree = ET.parse(xml_path)
    root = tree.getroot()
    text_lines = []
    for textline in root.findall('.//{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}TextLine'):
        text = textline.find('.//{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}TextEquiv/{http://schema.primaresearch.org/PAGE/gts/pagecontent/2013-07-15}Unicode')
        if text is not None and text.text:
            text_lines.append(text.text.strip())
    return ' '.join(text_lines)

def create_dataset():
    base_dir = Path("Copy_of_HTR_Train_Set_'Pracalit_for_Sanskrit_and_Newar_MSS_16th_to_19th_C_'")
    page_dir = base_dir / "page"
    images = []
    texts = []
    image_files = list(base_dir.glob("*.jpg"))
    for img_file in tqdm(image_files, desc="Processing images"):
        xml_file = page_dir / f"{img_file.stem}.xml"
        if not xml_file.exists():
            print(f"Warning: No matching XML found for {img_file.name}")
            continue
        try:
            text = extract_text_from_xml(xml_file)
            img = PILImage.open(img_file)
            img.verify()
            images.append(str(img_file))
            texts.append(text)
        except Exception as e:
            print(f"Error processing {img_file.name}: {str(e)}")
            continue
    dataset = Dataset.from_dict({
        "image": images,
        "text": texts
    }, features=Features({
        "image": Image(),
        "text": datasets.Value("string")
    }))
    print(f"Dataset created with {len(dataset)} examples")
    print("Saved to sanskrit_dataset.parquet")
    dataset.push_to_hub("davanstrien/sanskrit_dataset", private=True)

if __name__ == "__main__":
    create_dataset()
```

## Citation

If you use this dataset, please cite the original dataset on Zenodo:
[OCR model for Pracalit for Sanskrit and Newar MSS 16th to 19th C., Ground Truth](https://zenodo.org/records/6967421)