File size: 6,172 Bytes
d0e30af
 
 
 
 
 
 
 
 
b8d8121
d0e30af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8d8121
 
d0e30af
 
 
 
b8d8121
d0e30af
 
 
 
b8d8121
 
 
 
 
 
 
d0e30af
 
 
 
 
 
 
 
b8d8121
 
 
 
 
 
 
 
 
d0e30af
 
 
 
 
 
b8d8121
d0e30af
 
 
 
 
 
 
b8d8121
d0e30af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
Screenshot & Terminal Text Reader using GOT-OCR 2.0
====================================================
Extracts text from screenshots of:
- Terminal/console windows
- Log files
- Computer application windows

Model: stepfun-ai/GOT-OCR-2.0-hf (560M params, Apache 2.0)
Requirements: pip install transformers torch Pillow accelerate huggingface_hub tqdm

Usage:
    python ocr_reader.py image.png
    python ocr_reader.py image1.png image2.png image3.png
    python ocr_reader.py ./screenshots_folder/
    python ocr_reader.py image.png --output result.txt
"""

import sys
import os
import argparse
import warnings
import torch
from pathlib import Path
from PIL import Image
from huggingface_hub import snapshot_download
from tqdm import tqdm

# Suppress harmless tokenizer warning
os.environ["TOKENIZERS_PARALLELISM"] = "false"
warnings.filterwarnings("ignore", message=".*clean_up_tokenization_spaces.*")
warnings.filterwarnings("ignore", category=UserWarning, module="transformers")

from transformers import AutoProcessor, AutoModelForImageTextToText


# =============================================================
# Model: GOT-OCR 2.0 (General OCR Theory)
# HuggingFace: https://huggingface.co/stepfun-ai/GOT-OCR-2.0-hf
# Paper: https://arxiv.org/abs/2409.01704
# Size: 560M parameters (~1.1GB download)
# License: Apache 2.0
# =============================================================
MODEL_ID = "stepfun-ai/GOT-OCR-2.0-hf"


def load_model(device=None):
    """Load GOT-OCR 2.0 model and processor."""
    if device is None:
        device = "cuda" if torch.cuda.is_available() else "cpu"

    print(f"\n{'='*60}", file=sys.stderr)
    print(f"  Model: GOT-OCR 2.0 (stepfun-ai/GOT-OCR-2.0-hf)", file=sys.stderr)
    print(f"  Size:  560M parameters (~1.1GB)", file=sys.stderr)
    print(f"  Device: {device}", file=sys.stderr)
    print(f"{'='*60}\n", file=sys.stderr)

    # Download model files with progress bar
    print("Downloading model (if not cached)...", file=sys.stderr)
    snapshot_download(MODEL_ID, local_dir=None)  # uses HF cache with progress bar

    if device == "cuda":
        dtype = torch.bfloat16
    else:
        dtype = torch.float32

    print("Loading model into memory...", file=sys.stderr)
    model = AutoModelForImageTextToText.from_pretrained(
        MODEL_ID,
        dtype=dtype,
        device_map=device,
    )

    processor = AutoProcessor.from_pretrained(MODEL_ID)
    print("✓ Model ready!\n", file=sys.stderr)
    return model, processor, device


def extract_text(image_path, model, processor, max_tokens=4096):
    """Extract text from a single image."""
    image = Image.open(image_path).convert("RGB")

    # Prepare inputs
    inputs = processor(images=image, return_tensors="pt")
    inputs = {k: v.to(model.device) for k, v in inputs.items()}

    # Generate text
    with torch.no_grad():
        generate_ids = model.generate(
            **inputs,
            do_sample=False,
            tokenizer=processor.tokenizer,
            stop_strings=["<|im_end|>"],
            max_new_tokens=max_tokens,
        )

    # Decode - skip the input tokens
    input_len = inputs["input_ids"].shape[1]
    generated_ids = generate_ids[0, input_len:]
    text = processor.decode(generated_ids, skip_special_tokens=True)
    return text.strip()


def process_images(paths, output_file=None, max_tokens=4096):
    """Process one or more images or directories."""
    # Collect all image paths
    image_paths = []
    supported_exts = {".png", ".jpg", ".jpeg", ".bmp", ".tiff", ".webp", ".gif"}

    for path in paths:
        p = Path(path)
        if p.is_dir():
            for ext in supported_exts:
                image_paths.extend(sorted(p.glob(f"*{ext}")))
                image_paths.extend(sorted(p.glob(f"*{ext.upper()}")))
        elif p.is_file() and p.suffix.lower() in supported_exts:
            image_paths.append(p)
        else:
            print(f"Warning: Skipping '{path}' (not a supported image or directory)", file=sys.stderr)

    if not image_paths:
        print("No valid images found!", file=sys.stderr)
        return

    # Load model once
    model, processor, device = load_model()

    # Prepare output
    output_handle = open(output_file, "w") if output_file else sys.stdout
    
    try:
        for i, img_path in enumerate(image_paths, 1):
            if len(image_paths) > 1:
                header = f"\n{'='*60}\n[{i}/{len(image_paths)}] {img_path.name}\n{'='*60}"
                if output_file:
                    output_handle.write(header + "\n")
                else:
                    print(header, file=sys.stderr)

            print(f"Processing: {img_path.name}...", file=sys.stderr)
            text = extract_text(str(img_path), model, processor, max_tokens)
            output_handle.write(text + "\n")

            if len(image_paths) > 1:
                output_handle.write("\n")
    finally:
        if output_file:
            output_handle.close()
            print(f"\nOutput saved to: {output_file}", file=sys.stderr)


def main():
    parser = argparse.ArgumentParser(
        description="Extract text from screenshots using GOT-OCR 2.0",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  python ocr_reader.py screenshot.png
  python ocr_reader.py *.png --output all_text.txt
  python ocr_reader.py ./screenshots/
  python ocr_reader.py terminal.png --max-tokens 8192
        """
    )
    parser.add_argument("images", nargs="+", help="Image files or directories to process")
    parser.add_argument("--output", "-o", help="Save output to file (default: print to stdout)")
    parser.add_argument("--max-tokens", type=int, default=4096,
                        help="Maximum tokens to generate (default: 4096, increase for very long texts)")
    parser.add_argument("--device", choices=["cuda", "cpu"],
                        help="Force device (default: auto-detect)")

    args = parser.parse_args()

    if args.device:
        os.environ["OCR_DEVICE"] = args.device

    process_images(args.images, output_file=args.output, max_tokens=args.max_tokens)


if __name__ == "__main__":
    main()