/
gcp.go
138 lines (125 loc) · 3.24 KB
/
gcp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ocr
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path"
"strings"
"time"
vision "cloud.google.com/go/vision/apiv1"
"google.golang.org/api/option"
pb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)
type GCPClient struct {
CredentialsPath string
}
// Method required by ocr.Client
// Returns GCP document text detection Result
// Reference: https://cloud.google.com/vision/docs/apis
func (c GCPClient) Run(file []byte) (*Result, error) {
const (
service = "GCP"
version = "v1"
keyName = "gcp.json"
)
credentialsFile := path.Join(c.CredentialsPath, keyName)
ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(
ctx,
option.WithCredentialsFile(credentialsFile),
)
if err != nil {
return nil, fmt.Errorf("%s: configuration error: %v", service, err)
}
defer client.Close()
fileReader := bytes.NewReader(file)
image, err := vision.NewImageFromReader(fileReader)
if err != nil {
return nil, fmt.Errorf("%s: failed to read image bytes: %v", service, err)
}
start := time.Now()
annotation, err := client.DetectDocumentText(ctx, image, nil)
milli := int64(time.Since(start) / time.Millisecond)
if err != nil {
return nil, fmt.Errorf("%s: OCR request failed - %v", service, err)
}
// Extract full text
fullText := ""
if annotation != nil {
fullText = annotation.Text
}
date := fmtTime(start.UTC())
encoded, err := json.Marshal(annotation)
return &Result{
Service: service,
Version: version,
FullText: fullText,
Duration: milli,
Date: date,
Raw: encoded,
}, err
}
func polyToBox(poly *pb.BoundingPoly) (string, error) {
if len(poly.Vertices) != 4 {
return "", fmt.Errorf("Found %d != 4 vertices", len(poly.Vertices))
}
minx, miny := poly.Vertices[0].X, poly.Vertices[0].Y
maxx, maxy := minx, miny
for _, v := range poly.Vertices {
if v.X < minx {
minx = v.X
}
if v.Y < miny {
miny = v.Y
}
if v.X > maxx {
maxx = v.X
}
if v.Y > maxy {
maxy = v.Y
}
}
return encodeRawBounds(int(minx), int(miny), int(maxx-minx), int(maxy-miny)), nil
}
func (_ GCPClient) ResultToDetection(result *Result, _, _ int) (*Detection, error) {
var response pb.TextAnnotation
err := json.Unmarshal(result.Raw, &response)
if err != nil {
return nil, err
}
blocks := make([]Block, 0, 3)
for _, p := range response.Pages {
for _, r := range p.Blocks {
lines := make([]Line, 0, len(r.Paragraphs))
for _, l := range r.Paragraphs {
words := make([]Word, 0, len(l.Words))
for _, w := range l.Words {
bounds, err := polyToBox(w.BoundingBox)
if err != nil {
return nil, err
}
symbols := make([]string, len(w.Symbols))
for _, s := range w.Symbols {
symbols = append(symbols, s.Text)
}
word := strings.Join(symbols, "")
words = append(words, Word{bounds, word})
}
bounds, err := polyToBox(l.BoundingBox)
if err != nil {
return nil, err
}
lines = append(lines, Line{bounds, words})
}
bounds, err := polyToBox(r.BoundingBox)
if err != nil {
return nil, err
}
blocks = append(blocks, Block{bounds, lines})
}
}
algoID := sanitizeString(result.Service[:3] + "-" + result.Version)
millis := uint32(result.Duration)
return &Detection{algoID, result.Date, millis, blocks}, nil
}