| """ |
| Upload Odia OCR Benchmark Dataset to HuggingFace Hub |
| |
| Converts local images + metadata.csv to HuggingFace Dataset format and pushes. |
| """ |
|
|
| import argparse |
| from pathlib import Path |
|
|
| from datasets import Dataset, Features, Value, Image |
| from huggingface_hub import HfApi |
| import pandas as pd |
|
|
|
|
| BENCHMARK_DIR = Path(__file__).parent.parent / "benchmark_dataset" |
| CSV_PATH = BENCHMARK_DIR / "final_hf.csv" |
|
|
|
|
| def resolve_image_path(raw_path: str) -> Path: |
| """Resolve image paths from metadata across common path styles.""" |
| p = Path(str(raw_path).strip()) |
|
|
| |
| if p.is_absolute(): |
| return p |
|
|
| |
| if p.parts and p.parts[0] == "benchmark_dataset": |
| return (BENCHMARK_DIR.parent / p).resolve() |
|
|
| |
| return (BENCHMARK_DIR / p).resolve() |
|
|
|
|
| def load_local_dataset() -> Dataset: |
| """Load local images and metadata into a HuggingFace Dataset.""" |
| print(f"Loading metadata from {CSV_PATH}...") |
| |
| if not CSV_PATH.exists(): |
| raise FileNotFoundError(f"Metadata CSV not found: {CSV_PATH}") |
| |
| df = pd.read_csv(CSV_PATH) |
| print(f"Found {len(df)} samples in metadata") |
| |
| |
| |
| |
| |
| df["image_path"] = df["image_path"].apply(lambda p: str(resolve_image_path(p))) |
| |
| |
| missing = [] |
| for idx, row in df.iterrows(): |
| if not Path(row["image_path"]).exists(): |
| missing.append(row["image_path"]) |
| |
| if missing: |
| print(f"Warning: {len(missing)} images not found:") |
| for p in missing[:5]: |
| print(f" - {p}") |
| if len(missing) > 5: |
| print(f" ... and {len(missing) - 5} more") |
| |
| |
| df = df[df["image_path"].apply(lambda p: Path(p).exists())] |
| print(f"Continuing with {len(df)} valid samples") |
| |
| if "id" not in df.columns: |
| raise ValueError( |
| f"Required column 'id' not found in {CSV_PATH}. " |
| "Please add an 'id' column before upload." |
| ) |
|
|
| |
| features = Features({ |
| "id": Value("int64"), |
| "image": Image(), |
| "ground_truth": Value("string"), |
| "category": Value("string"), |
|
|
| }) |
| |
| |
| data = { |
| "id": df["id"].tolist(), |
| "image": df["image_path"].tolist(), |
| "ground_truth": df["ground_truth"].tolist(), |
| "category": df["category"].tolist(), |
|
|
| } |
| |
| dataset = Dataset.from_dict(data, features=features) |
| print(f"Created HuggingFace Dataset with {len(dataset)} samples") |
| |
| return dataset |
|
|
|
|
| def push_to_hub(dataset: Dataset, repo_id: str, private: bool = False): |
| """Push dataset to HuggingFace Hub.""" |
| print(f"\nPushing to HuggingFace Hub: {repo_id}") |
| print(f"Private: {private}") |
| |
| dataset.push_to_hub( |
| repo_id, |
| private=private, |
| commit_message="Upload Odia OCR benchmark dataset", |
| ) |
| |
| print(f"\nDataset uploaded to: https://huggingface.co/datasets/{repo_id}") |
|
|
|
|
| def push_dataset_card(repo_id: str, card_content: str): |
| """Upload dataset card as README.md to HuggingFace Hub.""" |
| api = HfApi() |
| api.upload_file( |
| path_or_fileobj=card_content.encode("utf-8"), |
| path_in_repo="README.md", |
| repo_id=repo_id, |
| repo_type="dataset", |
| commit_message="Add dataset card README", |
| ) |
| print(f"Dataset card uploaded: https://huggingface.co/datasets/{repo_id}/blob/main/README.md") |
|
|
|
|
| def create_dataset_card(repo_id: str): |
| """Create a dataset card (README.md) for HuggingFace.""" |
| card_content = f"""--- |
| license: cc-by-4.0 |
| task_categories: |
| - image-to-text |
| language: |
| - or |
| tags: |
| - ocr |
| - odia |
| - oriya |
| - indic |
| - benchmark |
| size_categories: |
| - n<1K |
| --- |
| |
| # Odia OCR Benchmark Dataset |
| |
| ## Description |
| |
| A curated benchmark dataset for evaluating OCR models on Odia (Oriya) text recognition. |
| Contains handwritten, printed, scene text, newspaper, books, and digital categories, |
| including both short samples and long-text examples for OCR evaluation. |
| |
| ## Dataset Structure |
| |
| - **id**: Unique identifier for each sample |
| - **image**: The input image (PIL Image) |
| - **ground_truth**: The correct Odia text transcription |
| - **category**: Type of text (handwritten, printed, scene_text, newspaper, books, digital) |
| |
| |
| |
| ## Usage |
| |
| ```python |
| from datasets import load_dataset |
| |
| dataset = load_dataset("{repo_id}") |
| |
| # Access a sample |
| sample = dataset["train"][0] |
| sample_id = sample["id"] |
| image = sample["image"] |
| text = sample["ground_truth"] |
| ``` |
| |
| ## Categories |
| |
| | Category | Description | |
| | ------------- | ----------------------------------------------------- | |
| | handwritten | Handwritten Odia text (word/short phrase level) | |
| | printed | Printed/typed Odia text | |
| | scene_text | Text in natural scenes (signboards, posters, etc.) | |
| | newspaper | Odia newspaper clippings (including long text) | |
| | books | Scanned Odia book pages (including long text) | |
| | digital | Screenshots from Odia digital content | |
| |
| ## Sources |
| |
| - `OdiaGenAIOCR/odia-ocr-merged` (handwritten) |
| - `darknight054/indic-mozhi-ocr` with config `oriya` (printed) |
| - `darknight054/indicstr12-crops` with config `odia` (scene_text) |
| - `newspaper`: Odia newspaper scans/clippings |
| - `books`: Odia book page images |
| - `digital`: odia digital content |
| |
| ## Notes |
| |
| - Includes long-text samples for paragraph-level OCR evaluation. |
| - The `source` field records origin for each sample. |
| |
| ## License |
| |
| CC-BY-4.0 |
| """ |
| return card_content |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Upload Odia OCR benchmark dataset to HuggingFace Hub" |
| ) |
| parser.add_argument( |
| "--repo", |
| type=str, |
| required=True, |
| help="HuggingFace repo ID (e.g., 'username/odia-ocr-benchmark')", |
| ) |
| parser.add_argument( |
| "--private", |
| action="store_true", |
| help="Make the dataset private", |
| ) |
| parser.add_argument( |
| "--dry-run", |
| action="store_true", |
| help="Load and validate dataset without uploading", |
| ) |
| |
| args = parser.parse_args() |
| |
| print("=" * 60) |
| print("Upload Odia OCR Benchmark to HuggingFace") |
| print("=" * 60) |
| |
| |
| dataset = load_local_dataset() |
| |
| |
| print("\nSample from dataset:") |
| sample = dataset[0] |
| print(f" id: {sample['id']}") |
| print(f" ground_truth: {sample['ground_truth']}") |
| print(f" category: {sample['category']}") |
|
|
| |
| if args.dry_run: |
| print("\n[DRY RUN] Dataset validated. Not uploading.") |
| return |
| |
| |
| push_to_hub(dataset, args.repo, private=args.private) |
|
|
| |
| card_content = create_dataset_card(args.repo) |
| push_dataset_card(args.repo, card_content) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|