forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor_rpc_worker.go
282 lines (232 loc) · 7.24 KB
/
preprocessor_rpc_worker.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
package ocrworker
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"github.com/couchbaselabs/logg"
"github.com/streadway/amqp"
)
type PreprocessorRpcWorker struct {
rabbitConfig RabbitConfig
conn *amqp.Connection
channel *amqp.Channel
tag string
Done chan error
bindingKey string
preprocessorMap map[string]Preprocessor
}
const preprocessor_tag = "preprocessor" // TODO: should be unique for each worker instance (eg, uuid)
func NewPreprocessorRpcWorker(rc RabbitConfig, preprocessor string) (*PreprocessorRpcWorker, error) {
preprocessorMap := make(map[string]Preprocessor)
preprocessorMap[PREPROCESSOR_STROKE_WIDTH_TRANSFORM] = StrokeWidthTransformer{}
preprocessorMap[PREPROCESSOR_IDENTITY] = IdentityPreprocessor{}
_, ok := preprocessorMap[preprocessor]
if !ok {
return nil, fmt.Errorf("No preprocessor found for: %q", preprocessor)
}
preprocessorRpcWorker := &PreprocessorRpcWorker{
rabbitConfig: rc,
conn: nil,
channel: nil,
tag: preprocessor_tag,
Done: make(chan error),
bindingKey: preprocessor,
preprocessorMap: preprocessorMap,
}
return preprocessorRpcWorker, nil
}
func (w PreprocessorRpcWorker) Run() error {
var err error
logg.LogTo("PREPROCESSOR_WORKER", "Run() called...")
logg.LogTo("PREPROCESSOR_WORKER", "dialing %q", w.rabbitConfig.AmqpURI)
w.conn, err = amqp.Dial(w.rabbitConfig.AmqpURI)
if err != nil {
return err
}
go func() {
fmt.Printf("closing: %s", <-w.conn.NotifyClose(make(chan *amqp.Error)))
}()
logg.LogTo("PREPROCESSOR_WORKER", "got Connection, getting Channel")
w.channel, err = w.conn.Channel()
if err != nil {
return err
}
if err = w.channel.ExchangeDeclare(
w.rabbitConfig.Exchange, // name of the exchange
w.rabbitConfig.ExchangeType, // type
true, // durable
false, // delete when complete
false, // internal
false, // noWait
nil, // arguments
); err != nil {
return err
}
// just call the queue the same name as the binding key, since
// there is no reason to have a different name.
queueName := w.bindingKey
queue, err := w.channel.QueueDeclare(
queueName, // name of the queue
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
if err != nil {
return err
}
if err = w.channel.QueueBind(
queue.Name, // name of the queue
w.bindingKey, // bindingKey
w.rabbitConfig.Exchange, // sourceExchange
false, // noWait
nil, // arguments
); err != nil {
return err
}
logg.LogTo("PREPROCESSOR_WORKER", "Queue bound to Exchange, starting Consume (consumer tag %q, binding key: %v)", preprocessor_tag, w.bindingKey)
deliveries, err := w.channel.Consume(
queue.Name, // name
preprocessor_tag, // consumerTag,
true, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
if err != nil {
return err
}
go w.handle(deliveries, w.Done)
return nil
}
func (w *PreprocessorRpcWorker) Shutdown() error {
// will close() the deliveries channel
if err := w.channel.Cancel(w.tag, true); err != nil {
return fmt.Errorf("Worker cancel failed: %s", err)
}
if err := w.conn.Close(); err != nil {
return fmt.Errorf("AMQP connection close error: %s", err)
}
defer logg.LogTo("PREPROCESSOR_WORKER", "Shutdown OK")
// wait for handle() to exit
return <-w.Done
}
func (w *PreprocessorRpcWorker) handle(deliveries <-chan amqp.Delivery, done chan error) {
for d := range deliveries {
logg.LogTo(
"PREPROCESSOR_WORKER",
"got %d byte delivery: [%v]. Routing key: %s Reply to: %v",
len(d.Body),
d.DeliveryTag,
d.RoutingKey,
d.ReplyTo,
)
err := w.handleDelivery(d)
if err != nil {
msg := "Error handling delivery in preprocessor. Error: %v"
logg.LogError(fmt.Errorf(msg, err))
}
}
logg.LogTo("PREPROCESSOR_WORKER", "handle: deliveries channel closed")
done <- fmt.Errorf("handle: deliveries channel closed")
}
func (w *PreprocessorRpcWorker) preprocessImage(ocrRequest *OcrRequest) error {
descriptor := w.bindingKey // eg, "stroke-width-transform"
preprocessor := w.preprocessorMap[descriptor]
logg.LogTo("PREPROCESSOR_WORKER", "Preproces %v via %v", ocrRequest, descriptor)
err := preprocessor.preprocess(ocrRequest)
if err != nil {
msg := "Error doing %s on: %v. Error: %v"
errMsg := fmt.Sprintf(msg, descriptor, ocrRequest, err)
logg.LogError(fmt.Errorf(errMsg))
return err
}
return nil
}
func (w *PreprocessorRpcWorker) strokeWidthTransform(ocrRequest *OcrRequest) error {
// write bytes to a temp file
tmpFileNameInput, err := createTempFileName()
if err != nil {
return err
}
defer os.Remove(tmpFileNameInput)
tmpFileNameOutput, err := createTempFileName()
if err != nil {
return err
}
defer os.Remove(tmpFileNameOutput)
err = saveBytesToFileName(ocrRequest.ImgBytes, tmpFileNameInput)
if err != nil {
return err
}
// run DecodeText binary on it (if not in path, print warning and do nothing)
darkOnLightSetting := "1" // todo: this should be passed as a param.
out, err := exec.Command(
"DetectText",
tmpFileNameInput,
tmpFileNameOutput,
darkOnLightSetting,
).CombinedOutput()
if err != nil {
logg.LogFatal("Error running command: %s. out: %s", err, out)
}
logg.LogTo("PREPROCESSOR_WORKER", "output: %v", string(out))
// read bytes from output file into ocrRequest.ImgBytes
resultBytes, err := ioutil.ReadFile(tmpFileNameOutput)
if err != nil {
return err
}
ocrRequest.ImgBytes = resultBytes
return nil
}
func (w *PreprocessorRpcWorker) handleDelivery(d amqp.Delivery) error {
ocrRequest := OcrRequest{}
err := json.Unmarshal(d.Body, &ocrRequest)
if err != nil {
msg := "Error unmarshaling json: %v. Error: %v"
errMsg := fmt.Sprintf(msg, string(d.Body), err)
logg.LogError(fmt.Errorf(errMsg))
return err
}
logg.LogTo("PREPROCESSOR_WORKER", "ocrRequest before: %v", ocrRequest)
routingKey := ocrRequest.nextPreprocessor(w.rabbitConfig.RoutingKey)
logg.LogTo("PREPROCESSOR_WORKER", "publishing with routing key %q", routingKey)
logg.LogTo("PREPROCESSOR_WORKER", "ocrRequest after: %v", ocrRequest)
err = w.preprocessImage(&ocrRequest)
if err != nil {
msg := "Error preprocessing image: %v. Error: %v"
errMsg := fmt.Sprintf(msg, ocrRequest, err)
logg.LogError(fmt.Errorf(errMsg))
return err
}
ocrRequestJson, err := json.Marshal(ocrRequest)
if err != nil {
return err
}
logg.LogTo("PREPROCESSOR_WORKER", "sendRpcResponse to: %v", routingKey)
if err := w.channel.Publish(
w.rabbitConfig.Exchange, // publish to an exchange
routingKey, // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "text/plain",
ContentEncoding: "",
Body: []byte(ocrRequestJson),
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
Priority: 0, // 0-9
ReplyTo: d.ReplyTo,
CorrelationId: d.CorrelationId,
// a bunch of application/implementation-specific fields
},
); err != nil {
return err
}
logg.LogTo("PREPROCESSOR_WORKER", "handleDelivery succeeded")
return nil
}