# How to extract text using LayoutLM2

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-extract-text-using-layoutlm2%2F18816%3E%5C
**Category:** Beginners\
**Created:** [June 7, 2022, 12:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-extract-text-using-layoutlm2%2F18816 "2022-06-07T12:41:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IvoTavares](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@IvoTavares](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FIvoTavares)%5C
**Post date:** [June 7, 2022, 12:41pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fhow-to-extract-text-using-layoutlm2%2F18816%2F1 "2022-06-07T12:41:51Z")

</div>

I’m trying to use LayoutLMv2 to extract information from some invoices pictures.  
So far, and based on what it’s [here](https://huggingface.co/docs/transformers/model_doc/layoutlmv2#transformers.LayoutLMv2ForQuestionAnswering), I’ve run the following:

`from transformers import LayoutLMv2Processor, LayoutLMv2ForQuestionAnswering

from PIL import Image

import torch

processor = LayoutLMv2Processor.from\_pretrained(“microsoft/layoutlmv2-base-uncased”)

model = LayoutLMv2ForQuestionAnswering.from\_pretrained(“microsoft/layoutlmv2-base-uncased”)

image = Image.open(“name\_of\_your\_document - can be a png file, pdf, etc.”).convert(“RGB”)

question = “what’s his name?”

encoding = processor(image, question, return\_tensors=“pt”)

start\_positions = torch.tensor([1])

end\_positions = torch.tensor([3])

outputs = model(\*\*encoding, start\_positions=start\_positions,  
end\_positions=end\_positions)

loss = outputs.loss

start\_scores = outputs.start\_logits

end\_scores = outputs.end\_logits`

However, I’m not exactly sure, how from this we can extract the words, and not just a visual box… For a visual box, there are a few gradio apps [here](https://huggingface.co/microsoft/layoutlmv2-base-uncased) on the spaces links. However, all the examples draw boxes on the picture, and I want to extract the answer as text.
