Commit
·
34306f7
1
Parent(s):
25fe3e3
Update code example
Browse files
README.md
CHANGED
|
@@ -23,7 +23,7 @@ You can use the raw model for optical character recognition (OCR) on single text
|
|
| 23 |
Here is how to use this model in PyTorch:
|
| 24 |
|
| 25 |
```python
|
| 26 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 27 |
from PIL import Image
|
| 28 |
import requests
|
| 29 |
|
|
@@ -31,17 +31,12 @@ import requests
|
|
| 31 |
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
|
| 32 |
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
| 33 |
|
| 34 |
-
|
| 35 |
-
# processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
|
| 36 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained('microsoft/trocr-small-printed')
|
| 37 |
-
tokenizer = XLMRobertaTokenizer.from_pretrained('microsoft/trocr-small-printed')
|
| 38 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
|
| 39 |
-
|
| 40 |
-
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
| 41 |
|
| 42 |
generated_ids = model.generate(pixel_values)
|
| 43 |
-
|
| 44 |
-
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 45 |
```
|
| 46 |
|
| 47 |
### BibTeX entry and citation info
|
|
|
|
| 23 |
Here is how to use this model in PyTorch:
|
| 24 |
|
| 25 |
```python
|
| 26 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 27 |
from PIL import Image
|
| 28 |
import requests
|
| 29 |
|
|
|
|
| 31 |
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
|
| 32 |
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
| 33 |
|
| 34 |
+
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-printed')
|
|
|
|
|
|
|
|
|
|
| 35 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-printed')
|
| 36 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
|
|
|
| 37 |
|
| 38 |
generated_ids = model.generate(pixel_values)
|
| 39 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
| 40 |
```
|
| 41 |
|
| 42 |
### BibTeX entry and citation info
|