-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisionapi.go
311 lines (266 loc) · 9.69 KB
/
visionapi.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package router
import (
"cloud.google.com/go/storage"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/martinomburajr/gcp-vision-api/app"
"github.com/martinomburajr/gcp-vision-api/app/api/gcp_storage"
"github.com/martinomburajr/gcp-vision-api/app/api/vision"
"log"
"net/http"
"strings"
"sync"
)
// OCRHandler handles POST requests that represent a basic request to perform OCR.
// A GCS file path and output path must be provided
func OCRHandler(gCPVisionAPIServer *GCPVisionAPIServer) AppHandler {
return func(w http.ResponseWriter, r *http.Request) *AppError {
outputParam := r.URL.Query().Get("output")
if outputParam != "json" && outputParam != "" && outputParam != "text" {
err := fmt.Errorf("invalid url param")
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("invalid param. output = 'json', 'text | %v", err), err)
}
ctx := context.Background()
var bodyInfo FileInfoJSON
// DECODE HTTP REQUEST BODY
err := json.NewDecoder(r.Body).Decode(&bodyInfo)
if err != nil {
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("error reading request | %v", err), err)
}
// GET SOURCE AND DESTINATION BUCKET NAMES
sourceBucketName, err := GetBucketName(bodyInfo.InputURI)
if err != nil {
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("error parsing bucket name | %v", err), err)
}
outputBucketName, err := GetBucketName(bodyInfo.OutputURI)
if outputBucketName == "" {
outputBucketName = sourceBucketName
}
if err != nil {
log.Printf("annotateFilesOperation bucket not set - defaulting to input bucket name")
}
log.Printf("Using Input Storage Bucket: %s", sourceBucketName)
inputPath, err := ExtractGCSPath(bodyInfo.InputURI)
outputPath := ""
if bodyInfo.OutputPath == "" {
outputPath = "visionapi/output/" + inputPath
} else {
outputPath = bodyInfo.OutputPath + "/" + inputPath
}
outputFullURI := fmt.Sprintf("gs://%s/%s", outputBucketName, outputPath)
sourceFullURI := fmt.Sprintf("%s", bodyInfo.InputURI)
//Performs the document scanning
annotateFilesOperation, err := vision.DetectAsyncDocumentURI(ctx, sourceFullURI, outputFullURI)
if err != nil {
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("error performing document detection | %v", err), err)
}
_, err = annotateFilesOperation.Wait(ctx)
if err != nil {
return AppErrorf(http.StatusInternalServerError, fmt.Sprintf("error waiting on files operation | %v", err),
err)
}
if annotateFilesOperation.Done() {
log.Println("completed annotation of files in VisionAPI")
}
// OUTPUT TO CLIENT
outputMsg := fmt.Sprintf("Successfully wrote to %s", outputFullURI)
w.WriteHeader(http.StatusCreated)
_, err = w.Write([]byte(outputMsg))
if err != nil {
log.Printf("error writing to client %s", err.Error())
}
return nil
}
}
// OCRBucketDirHandler performs the vision API operation on the entire bucket or dir provided
func OCRBucketDirHandler(gCPVisionAPIServer *GCPVisionAPIServer) AppHandler {
return func(w http.ResponseWriter, r *http.Request) *AppError {
outputParam := r.URL.Query().Get("format")
if outputParam != "json" && outputParam != "" && outputParam != "text" {
err := fmt.Errorf("invalid url param")
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("invalid param. output = 'json', 'text | %v", err), err)
}
ctx := context.Background()
var bodyInfo FileInfoJSON
// DECODE HTTP REQUEST BODY
err := json.NewDecoder(r.Body).Decode(&bodyInfo)
if err != nil {
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("error reading request | %v", err), err)
}
// GET SOURCE AND DESTINATION BUCKET NAMES
sourceBucketName, err := GetBucketName(bodyInfo.InputURI)
if err != nil {
return AppErrorf(http.StatusBadRequest, fmt.Sprintf("error parsing bucket name | %v", err), err)
}
outputBucketName, err := GetBucketName(bodyInfo.OutputURI)
if outputBucketName == "" {
outputBucketName = sourceBucketName
}
if err != nil {
log.Printf("annotateFilesOperation bucket not set - defaulting to input bucket name")
}
log.Printf("Using Input Storage Bucket: %s", sourceBucketName)
inputPath, err := ExtractGCSPath(bodyInfo.InputURI)
outputPath := ""
if bodyInfo.OutputPath == "" {
outputPath = "visionapi/output"
} else {
outputPath = bodyInfo.OutputPath
}
sourceBucketPath, err := ExtractGCSPath(bodyInfo.InputURI)
if err != nil {
return AppErrorf(http.StatusInternalServerError, "", err)
}
fileDestinationURI := fmt.Sprintf("gs://%s/%s/%s", outputBucketName, outputPath, inputPath)
sourceFullURI := fmt.Sprintf("%s", bodyInfo.InputURI)
///////////////////////////////////////// READ FILES FROM BUCKET ///////////////////////////////////////////////
// Get Files In Bucket
objectsHandles, err := gcp_storage.GetObjectHandlesFromBucket(ctx, sourceBucketName, sourceBucketPath,
gCPVisionAPIServer.StorageClient)
if err != nil {
return AppErrorf(http.StatusInternalServerError, fmt.Sprintf("error getting files from buckets names | %v",
err), err)
}
var wg sync.WaitGroup
var errChan chan *AppError
errChan = make(chan *AppError)
var fileDoneChan = make(chan string)
var completeChan = make(chan bool)
// Run Vision API on Files
for _, object := range objectsHandles {
wg.Add(1)
go func(object *storage.ObjectAttrs, outputBucketName, outputPath string, wg *sync.WaitGroup) {
defer wg.Done()
fileName := object.Name
fileDestinationURI = fmt.Sprintf("gs://%s/%s/%s", outputBucketName, outputPath, fileName)
fileSourceURI := fmt.Sprintf("%s/%s", sourceFullURI, fileName)
/////////////////////////////////////////// CALL VISION API
annotateFilesOperation, err := vision.DetectAsyncDocumentURI(ctx, fileSourceURI, fileDestinationURI)
if err != nil {
errChan <- AppErrorf(http.StatusBadRequest,
fmt.Sprintf("error performing document detection | %v",
err), err)
}
_, err = annotateFilesOperation.Wait(ctx)
if err != nil {
errChan <- AppErrorf(http.StatusInternalServerError, fmt.Sprintf("error waiting on files operation | %v", err),
err)
}
if annotateFilesOperation.Done() {
msg := fmt.Sprintf("Completed: File %s | Output: %s", fileName, fileDestinationURI)
fileDoneChan <- msg
log.Print("LOG: " + msg)
}
}(object, outputBucketName, outputPath, &wg)
}
wg.Wait()
log.Print(" Waiting! ")
completeChan <- true
sb := strings.Builder{}
//for {
log.Print(" In for! ")
select {
case str := <-fileDoneChan:
sb.WriteString(str + "\n")
log.Print(str)
case err := <-errChan:
errStr := fmt.Sprintf("error: %d | %s | %s", err.Code, err.Message, err.Error.Error())
sb.WriteString(errStr)
case <-completeChan:
log.Print(" COMPLETE INVOKED! ")
w.WriteHeader(http.StatusCreated)
w.Write([]byte(sb.String()))
break
default:
fmt.Print("Waiting on Channels")
}
//}
return nil
}
}
// VisionAPI writes back to cloud storage to the given path
//uri, err := ExtractGCSPath(outputFullURI)
//if err != nil {
// return AppErrorf(http.StatusInternalServerError, fmt.Sprintf("error constructing output path | %v",
// err), err)
//}
//fullPath, err := gcp_storage.GetFileNameFromBucket(ctx, outputPath, uri, gCPVisionAPIServer.StorageClient)
//if err != nil {
// return AppErrorf(http.StatusInternalServerError, fmt.Sprintf("error getting file | %v", err), err)
//}
//response, err := gcp_storage.Download(ctx, outputBucketName, outputPath, gCPVisionAPIServer.StorageClient)
//if err != nil {
// log.Printf("%v", err)
//}
//
//parsedText, err2 := gcp_storage.RetrieveText(response)
//if err2 != nil {
// log.Printf("%v", err)
//}
//data, err3 := json.Marshal(struct {
// Text string `json:"text"`
//}{Text: parsedText})
//if err3 != nil {
// log.Printf("Error marshalling output json | %v", err3)
//}
//Upload(w,r, parsedText)
//newPath := strings.Replace(bucketPath, "json", "txt", -1)
//log.Printf("Creating file /%s/%s\n", outputPath, newPath)
//gcp_storage.UploadTextToGCS(ctx, parsedText, fullPath, outputFullURI, gCPVisionAPIServer.StorageClient)
// FileInfoJSON represents the POST from the client detailing the URI for the client.
// Once processed the items will be sent to the OutputURI. Both must be specified
type FileInfoJSON struct {
// OutputURI represents the GCS URI to drop off the artifacts after processing.
OutputURI string `json:"outputUri"`
// OutputPath represents the internal bucket folder to use
OutputPath string `json:"outputPath"`
// InputURI represents the GCS URI of the data to be used
InputURI string `json:"inputUri"`
}
// GetBucketName tries to obtain the bucket name.
// Bucket names must contain the gs:// prefix to distinguish them as being GCP type buckets.
func GetBucketName(uri string) (string, error) {
if uri == "" {
return "", errors.New("error getting bucket name from outputuri | probably empty")
}
if len(uri) < 7 {
return "", errors.New("error invalid size uri | probably small in size")
}
if !strings.HasPrefix(uri, "gs://") {
return "", errors.New("error invalid uri, must start with gs://")
}
split := strings.Split(uri[5:], "")
ans := ""
for _, v := range split {
if v != "/" {
ans = ans + v
} else {
break
}
}
return strings.Trim(ans, ""), nil
}
// ExtractGCSPath returns the path after the bucket name and gs:// scheme.
func ExtractGCSPath(uri string) (string, error) {
if uri == "" {
return "", fmt.Errorf("uri cannot be empty")
}
if !strings.Contains(uri, app.GCSScheme) {
return "", fmt.Errorf("uri must contain GCS")
}
bucketName, err := GetBucketName(uri)
if err != nil {
return "", err
}
stringWithoutGCSScheme := strings.Replace(uri, app.GCSScheme, "", -1)
ans := strings.Replace(stringWithoutGCSScheme, bucketName, "", -1)
if ans == "" {
return "", nil
}
if ans == "/" {
return "", nil
}
return ans[1:], nil
}