Update code example

This commit is contained in:
Minghao Li 2022-01-11 06:55:24 +00:00 committed by huggingface-web
parent cbb9551979
commit bb14f6d89e
1 changed files with 4 additions and 9 deletions

View File

@ -23,7 +23,7 @@ You can use the raw model for optical character recognition (OCR) on single text
Here is how to use this model in PyTorch:
```python
from transformers import TrOCRProcessor, VisionEncoderDecoderModel, AutoFeatureExtractor, XLMRobertaTokenizer
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
from PIL import Image
import requests
@ -31,17 +31,12 @@ import requests
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
# For the time being, TrOCRProcessor does not support the small models, so the following temporary solution can be adopted
# processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-handwritten')
feature_extractor = AutoFeatureExtractor.from_pretrained('microsoft/trocr-small-handwritten')
tokenizer = XLMRobertaTokenizer.from_pretrained('microsoft/trocr-small-handwritten')
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-handwritten')
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-handwritten')
# pixel_values = processor(images=image, return_tensors="pt").pixel_values
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = model.generate(pixel_values)
# generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
```
### BibTeX entry and citation info