googleapis / googleapis/google-cloud-go
documentai: symbols do not contain "words" field when using pkg but does with REST
- Dominant language
- Go
- Stars
- 4.5k
- Forks
- 1.6k
- Avg merge
- 1d 13h
- Merged PRs (30d)
- 109
Description
## Environment
go version go1.22.2 linux/amd64
## Code and Dependencies
```go
func docAIUsingSDK() {
projectID := flag.String("project_id", "123", "Cloud Project ID")
location := flag.String("location", "us", "The Processor location")
// Create a Processor before running sample
processorID := flag.String("processor_id", "123", "The Processor ID")
filePath := flag.String("file_path", "./templates/TenantInfo.pdf", "The path to the file to parse")
mimeType := flag.String("mime_type", "application/pdf", "The mimeType of the file")
flag.Parse()
ctx := context.Background()
endpoint := fmt.Sprintf("%s-documentai.googleapis.com:443", *location)
client, err := documentai.NewDocumentProcessorClient(ctx, option.WithEndpoint(endpoint))
if err != nil {
fmt.Println(fmt.Errorf("error creating Document AI client: %w", err))
}
defer client.Close()
// Open local file.
data, err := os.ReadFile(*filePath)
if err != nil {
fmt.Println(fmt.Errorf("os.ReadFile: %w", err))
}
req := &documentaipb.ProcessRequest{
SkipHumanReview: true,
Name: fmt.Sprintf("projects/%s/locations/%s/processors/%s", *projectID, *location, *processorID),
Source: &documentaipb.ProcessRequest_RawDocument{
RawDocument: &documentaipb.RawDocument{
Content: data,
MimeType: *mimeType,
},
},
ProcessOptions: &documentaipb.ProcessOptions{
OcrConfig: &documentaipb.OcrConfig{
EnableNativePdfParsing: true,
EnableImageQualityScores: false,
EnableSymbol: true,
DisableCharacterBoxesDetection: false,
},
PageRange: &documentaipb.ProcessOptions_IndividualPageSelector_{
IndividualPageSelector: &documentaipb.ProcessOptions_IndividualPageSelector{
Pages: []int32{1},
},
},
},
}
resp, err := client.ProcessDocument(ctx, req)
if err != nil {
fmt.Println(fmt.Errorf("processDocument: %w", err))
}
document := resp.Document
// fmt.Printf("Document Text: %s", document.GetText())
w, err := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
log.Fatalf("failed to open or create file: %v", err)
}
defer w.Close()
pages := document.GetPages()
for _, page := range pages {
symbols := page.GetSymbols()
for _, symbol := range symbols{
//want to read the text of the symbol but it doesn't exist
}
}
}
```
## Expected behavior
I'd expect symbols to have the text like they do when calling the processor with just REST http calls
Below is part of the response with REST, you can see in paragraphs>words>symbols there is text showing the actual letters.
{
"boundingBox": {
"normalizedVertices": [
{
"x": 0.4019608,
"y": 0.05429293
},
{
"x": 0.60294116,
"y": 0.05429293
},
{
"x": 0.60294116,
"y": 0.06439394
},
{
"x": 0.4019608,
"y": 0.06439394
}
]
},
"paragraphs": [
{
"boundingBox": {
"normalizedVertices": [
{
"x": 0.4019608,
"y": 0.05429293
},
{
"x": 0.60294116,
"y": 0.05429293
},
{
"x": 0.60294116,
"y": 0.06439394
},
{
"x": 0.4019608,
"y": 0.06439394
}
]
},
"words": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 1
}
]
},
"boundingBox": {
"normalizedVertices": [
{
"x": 0.4019608,
"y": 0.05429293
},
{
"x": 0.4738562,
"y": 0.05429293
},
{
"x": 0.4738562,
"y": 0.06439394
},
{
"x": 0.4019608,
"y": 0.06439394
}
]
},
"symbols": [
{
"text": "T",
"confidence": 0.9863702
},
{
"text": "E",
"confidence": 0.9875592
},
{
"text": "N",
"confidence": 0.99181056
},
{
"text": "A",
"confidence": 0.99042034
},
{
"text": "N",
"confidence": 0.99400955
},
{
"property": {
"detectedBreak": {
"type": "SPACE"
}
},
"text": "T",
"confidence": 0.99327475
}
],
"confidence": 0.9905741
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 1
}
]
},
"boundingBox": {
"normalizedVertices": [
{
"x": 0.48039216,
"y": 0.05429293
},
{
"x": 0.60294116,
"y": 0.05429293
},
{
"x": 0.60294116,
"y": 0.06439394
},
{
"x": 0.48039216,
"y": 0.06439394
}
]
},
"symbols": [
{
"text": "I",
"confidence": 0.9922731
},
{
"text": "N",
"confidence": 0.99282557
},
{
"text": "F",
"confidence": 0.9891642
},
{
"text": "O",
"confidence": 0.98348856
},
{
"text": "R",
"confidence": 0.99201816
},
{
"text": "M",
"confidence": 0.9929482
},
{
"text": "A",
"confidence": 0.9926885
},
{
"text": "T",
"confidence": 0.9933449
},
{
"text": "I",
"confidence": 0.9935098
},
{
"text": "O",
"confidence": 0.9947548
},
{
"property": {
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"text": "N",
"confidence": 0.9881829
}
],
"confidence": 0.9913817
}
],
"confidence": 0.9910967
}
],
"blockType": "TEXT",
"confidence": 0.9910967
},
## Actual behavior
Heres what I get from using the documentai pkg to process the pdf.
symbols:{layout:{text_anchor:{text_segments:{start_index:1 end_index:2}} confidence:1 bounding_poly:{vertices:{x:730 y:124} vertices:{x:746 y:124} vertices:{x:746 y:148} vertices:{x:730 y:148} normalized_vertices:{x:0.4152446 y:0.054505493} normalized_vertices:{x:0.42434585 y:0.054505493} normalized_vertices:{x:0.42434585 y:0.065054946} normalized_vertices:{x:0.4152446 y:0.065054946}} orientation:PAGE_UP}}
Contributor guide
Assessment
This issue has not been assessed yet.