TibetanCodexAITeam commited on
Commit
2a989bd
·
verified ·
1 Parent(s): b47f5d9

Update model card (README.md) via PechaBridge

Browse files
Files changed (1) hide show
  1. README.md +20 -6
README.md CHANGED
@@ -44,21 +44,35 @@ Each image produces a `.txt` transcript and an `*_overlay.jpg` with detected lin
44
  If you need to call the model directly from Python, you must apply the BDRC-style preprocessing manually before passing pixel values to the model. See `pechabridge/ocr/preprocess_bdrc.py` in the PechaBridge repository for the full preprocessing pipeline (`gray` pipeline was used for this checkpoint).
45
 
46
  ```python
47
- # Minimal example — apply BDRC gray preprocessing first
48
  import torch
49
  from transformers import VisionEncoderDecoderModel, AutoTokenizer
50
- from pechabridge.ocr.preprocess_bdrc import BDRCPreprocessConfig, preprocess_line_image_bdrc
 
 
 
 
51
  from PIL import Image
52
  import numpy as np
53
 
54
  model = VisionEncoderDecoderModel.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
55
  tokenizer = AutoTokenizer.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
56
 
57
- # Load and preprocess a line crop with the BDRC pipeline
58
  image = Image.open('line_crop.png').convert('RGB')
59
- cfg = BDRCPreprocessConfig() # uses training defaults
60
- preprocessed = preprocess_line_image_bdrc(np.array(image), cfg) # returns float32 CHW in [-1, 1]
61
- pixel_values = torch.tensor(preprocessed).unsqueeze(0) # add batch dim
 
 
 
 
 
 
 
 
 
 
62
 
63
  generated_ids = model.generate(pixel_values)
64
  text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
44
  If you need to call the model directly from Python, you must apply the BDRC-style preprocessing manually before passing pixel values to the model. See `pechabridge/ocr/preprocess_bdrc.py` in the PechaBridge repository for the full preprocessing pipeline (`gray` pipeline was used for this checkpoint).
45
 
46
  ```python
47
+ # Minimal example — BDRC gray preprocessing applied explicitly
48
  import torch
49
  from transformers import VisionEncoderDecoderModel, AutoTokenizer
50
+ from pechabridge.ocr.preprocess_bdrc import (
51
+ BDRCPreprocessConfig,
52
+ preprocess_image_bdrc, # returns grayscale PIL Image (mode 'L')
53
+ bdrc_image_to_normalized_tensor, # grayscale PIL → float32 HW in [-1, 1]
54
+ )
55
  from PIL import Image
56
  import numpy as np
57
 
58
  model = VisionEncoderDecoderModel.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
59
  tokenizer = AutoTokenizer.from_pretrained("TibetanCodexAITeam/PechaBridgeOCR")
60
 
61
+ # 1. Load the line crop as RGB
62
  image = Image.open('line_crop.png').convert('RGB')
63
+
64
+ # 2. Apply BDRC preprocessing:
65
+ # - converts to grayscale (luma by default)
66
+ # - optionally normalises background, binarises, pads/resizes
67
+ # Returns a grayscale PIL Image (mode 'L')
68
+ cfg = BDRCPreprocessConfig.ocr_line_defaults() # defaults used during training
69
+ gray_pil = preprocess_image_bdrc(image, cfg)
70
+
71
+ # 3. Normalise to float32 in [-1, 1] and build a 3-channel tensor
72
+ # (model expects C=3; replicate the single gray channel)
73
+ gray_hw = bdrc_image_to_normalized_tensor(image, cfg) # shape: (H, W), float32
74
+ pixel_values = torch.tensor(gray_hw).unsqueeze(0).unsqueeze(0) # (1, 1, H, W)
75
+ pixel_values = pixel_values.expand(-1, 3, -1, -1) # (1, 3, H, W)
76
 
77
  generated_ids = model.generate(pixel_values)
78
  text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]