-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
300 lines (248 loc) · 8.79 KB
/
main.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
/*
Text detection model: https://github.com/argman/EAST
Download link: https://www.dropbox.com/s/r2ingd0l3zt8hxs/frozen_east_text_detection.tar.gz?dl=1
Text recognition models can be downloaded directly here:
Download link: https://drive.google.com/drive/folders/1cTbQ3nuZG-EKWak6emD_s8_hHXWz7lAr?usp=sharing
and https://github.com/opencv/opencv/blob/master/doc/tutorials/dnn/dnn_text_spotting/dnn_text_spotting.markdown
How to convert from pb to onnx:
Using classes from here: https://github.com/meijieru/crnn.pytorch/blob/master/models/crnn.py
import torch
from models.crnn import CRNN
model = CRNN(32, 1, 37, 256)
model.load_state_dict(torch.load('crnn.pth'))
dummy_input = torch.randn(1, 1, 32, 100)
torch.onnx.export(model, dummy_input, "crnn.onnx", verbose=True)
For more information, please refer to https://github.com/opencv/opencv/blob/master/doc/tutorials/dnn/dnn_text_spotting/dnn_text_spotting.markdown
and https://github.com/opencv/opencv/blob/master/doc/tutorials/dnn/dnn_OCR/dnn_OCR.markdown
*/
package main
import (
"flag"
"fmt"
"image"
"image/color"
"math"
"strings"
"time"
"gocv.io/x/gocv"
)
func main() {
input := flag.String("input", "0", "Path to input image or video file. Skip this argument to capture frames from a camera.")
model := flag.String("model", "", "Path to a binary .pb file contains trained detector network.")
ocr := flag.String("ocr", "crnn.onnx", "Path to a binary .pb or .onnx file contains trained recognition network")
width := flag.Int("width", 320, "Preprocess input image by resizing to a specific width. It should be multiple by 32.")
height := flag.Int("height", 320, "Preprocess input image by resizing to a specific height. It should be multiple by 32.")
threshold := flag.Float64("thr", 0.5, "Confidence Threshold")
nms := flag.Float64("nms", 0.4, "Non-maximum suppression threshold.")
flag.Parse()
// Load Network
detector := gocv.ReadNet(*model, "")
recognizer := gocv.ReadNet(*ocr, "")
// Create a new named window
window := gocv.NewWindow("EAST: An Efficient and Accurate Scene Text Detector")
defer window.Close()
outNames := []string{
"feature_fusion/Conv_7/Sigmoid",
"feature_fusion/concat_3",
}
img := gocv.NewMat()
defer img.Close()
// Open a video file or an image file or a camera stream
cap, err := gocv.OpenVideoCapture(*input)
if err != nil {
fmt.Printf("Error opening video capture device: %v\n", *input)
return
}
for {
// Read frame
cap.Read(&img)
if img.Empty() {
continue
}
// Get frame height and width
imgHeight := img.Size()[0]
imgWidth := img.Size()[1]
rW := float64(imgWidth) / float64(*width)
rH := float64(imgHeight) / float64(*height)
// Create a 4D blob from frame.
blob := gocv.BlobFromImage(img, 1.0, image.Pt(int(*width), int(*height)), gocv.NewScalar(123.68, 116.78, 103.94, 0), true, false)
defer blob.Close()
var inferenceTime time.Duration
// Run the detection model
detector.SetInput(blob, "")
startTime := time.Now()
outs := detector.ForwardLayers(outNames)
inferenceTime += time.Since(startTime)
// Get scores and geometry
scores := outs[0]
geometry := outs[1]
rotatedBoxes, confidences := decodeBoundingBoxes(scores, geometry, float32(*threshold))
boxes := []image.Rectangle{}
for _, rotatedBox := range rotatedBoxes {
boxes = append(boxes, rotatedBox.BoundingRect)
}
// Only Apply NMS when there are at least one box
indices := make([]int, len(boxes))
if len(boxes) > 0 {
gocv.NMSBoxes(boxes, confidences, float32(*threshold), float32(*nms), indices)
}
// Resize indices to only include those that have values other than zero
var numIndices int = 0
for _, value := range indices {
if value != 0 {
numIndices++
}
}
indices = indices[0:numIndices]
for i := 0; i < len(indices); i++ {
// get 4 corners of the rotated rect
verticesMat := gocv.NewMat()
gocv.BoxPoints(rotatedBoxes[indices[i]], &verticesMat)
// scale the bounding box coordinates based on the respective ratios
vertices := []image.Point{}
for j := 0; j < 4; j++ {
p1 := image.Pt(
int(verticesMat.GetFloatAt(j, 0)*float32(rW)),
int(verticesMat.GetFloatAt(j, 1)*float32(rH)),
)
p2 := image.Pt(
int(verticesMat.GetFloatAt((j+1)%4, 0)*float32(rW)),
int(verticesMat.GetFloatAt((j+1)%4, 1)*float32(rH)),
)
vertices = append(vertices, p1)
gocv.Line(&img, p1, p2, color.RGBA{0, 255, 0, 0}, 1)
}
// get cropped image using perspective transform
if *ocr != "" {
cropped := fourPointsTransform(img, gocv.NewPointVectorFromPoints(vertices))
gocv.CvtColor(cropped, &cropped, gocv.ColorBGRToGray)
// Create a 4D blob from cropped image
blob := gocv.BlobFromImage(cropped, 1/127.5, image.Pt(100, 32), gocv.NewScalar(127.5, 0, 0, 0), false, false)
recognizer.SetInput(blob, "")
// Run the recognition model
startTime = time.Now()
result := recognizer.Forward("")
inferenceTime += time.Since(startTime)
// decode the result into text
wordRecognized := decodeText(result)
gocv.PutText(&img, wordRecognized, vertices[1], gocv.FontHersheySimplex, 0.5, color.RGBA{0, 0, 255, 0}, 1)
}
}
// Put efficiency information
label := fmt.Sprintf("Inference time: %v ms", inferenceTime.Milliseconds())
gocv.PutText(&img, label, image.Pt(0, 15), gocv.FontHersheySimplex, 0.5, color.RGBA{0, 255, 0, 0}, 1)
window.IMShow(img)
if window.WaitKey(1) >= 0 {
break
}
}
}
func decodeText(scores gocv.Mat) string {
text := ""
alphabet := "0123456789abcdefghijklmnopqrstuvwxyz"
for i := 0; i < scores.Size()[0]; i++ {
scoresChannel := gocv.GetBlobChannel(scores, 0, i)
var c int = 0
var cScore float32 = 0
for j := 0; j < scores.Size()[2]; j++ {
score := scoresChannel.GetFloatAt(0, j)
if cScore < score {
c = j
cScore = score
}
}
if c != 0 {
text += string(alphabet[c-1])
} else {
text += "-"
}
}
// adjacent same letters as well as background text must be removed to get the final output
var charList strings.Builder
for i := 0; i < len(text); i++ {
if string(text[i]) != "-" && !(i > 0 && text[i] == text[i-1]) {
charList.WriteByte(text[i])
}
}
return charList.String()
}
func fourPointsTransform(frame gocv.Mat, vertices gocv.PointVector) gocv.Mat {
outputSize := image.Pt(100, 32)
targetVertices := gocv.NewPointVectorFromPoints([]image.Point{
image.Pt(0, outputSize.Y-1),
image.Pt(0, 0),
image.Pt(outputSize.X-1, 0),
image.Pt(outputSize.X-1, outputSize.Y-1),
})
result := gocv.NewMat()
rotationMatrix := gocv.GetPerspectiveTransform(vertices, targetVertices)
gocv.WarpPerspective(frame, &result, rotationMatrix, outputSize)
return result
}
func decodeBoundingBoxes(scores gocv.Mat, geometry gocv.Mat, threshold float32) (detections []gocv.RotatedRect, confidences []float32) {
scoresChannel := gocv.GetBlobChannel(scores, 0, 0)
x0DataChannel := gocv.GetBlobChannel(geometry, 0, 0)
x1DataChannel := gocv.GetBlobChannel(geometry, 0, 1)
x2DataChannel := gocv.GetBlobChannel(geometry, 0, 2)
x3DataChannel := gocv.GetBlobChannel(geometry, 0, 3)
angleChannel := gocv.GetBlobChannel(geometry, 0, 4)
for y := 0; y < scoresChannel.Rows(); y++ {
for x := 0; x < scoresChannel.Cols(); x++ {
// Extract data from scores
score := scoresChannel.GetFloatAt(y, x)
// If score is lower than threshold score, move to next x
if score < threshold {
continue
}
x0Data := x0DataChannel.GetFloatAt(y, x)
x1Data := x1DataChannel.GetFloatAt(y, x)
x2Data := x2DataChannel.GetFloatAt(y, x)
x3Data := x3DataChannel.GetFloatAt(y, x)
angle := angleChannel.GetFloatAt(y, x)
// Calculate offset
// Multiple by 4 because feature maps are 4 time less than input image.
offsetX := x * 4.0
offsetY := y * 4.0
// Calculate cos and sin of angle
cosA := math.Cos(float64(angle))
sinA := math.Sin(float64(angle))
h := x0Data + x2Data
w := x1Data + x3Data
// Calculate offset
offset := []float64{
(float64(offsetX) + cosA*float64(x1Data) + sinA*float64(x2Data)),
(float64(offsetY) - sinA*float64(x1Data) + cosA*float64(x2Data)),
}
// Find points for rectangle
p1 := []float64{
(-sinA*float64(h) + offset[0]),
(-cosA*float64(h) + offset[1]),
}
p3 := []float64{
(-cosA*float64(w) + offset[0]),
(sinA*float64(w) + offset[1]),
}
center := image.Pt(
int(0.5*(p1[0]+p3[0])),
int(0.5*(p1[1]+p3[1])),
)
detections = append(detections, gocv.RotatedRect{
Points: []image.Point{
{int(p1[0]), int(p1[1])},
{int(p3[0]), int(p3[1])},
},
BoundingRect: image.Rect(
int(p1[0]), int(p1[1]),
int(p3[0]), int(p3[1]),
),
Center: center,
Width: int(w),
Height: int(h),
Angle: float64(-1 * angle * 180 / math.Pi),
})
confidences = append(confidences, score)
}
}
// Return detections and confidences
return
}