-
Notifications
You must be signed in to change notification settings - Fork 2
/
find_symbols.go
49 lines (42 loc) · 1.41 KB
/
find_symbols.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
package license_plate_recognition
import (
"image"
"sort"
"github.com/LdDl/go-darknet"
)
// Detections slice of image.Rectangle (for sorting)
type Detections []*darknet.Detection
func (r Detections) Len() int { return len(r) }
func (r Detections) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r Detections) Less(i, j int) bool {
return r[i].BoundingBox.StartPoint.X < r[j].BoundingBox.StartPoint.X
}
func (net *YOLONetwork) detectSymbols(imgSrc image.Image) ([]image.Rectangle, []int, string, float32, error) {
img, err := darknet.Image2Float32(imgSrc)
if err != nil {
return nil, []int{}, "", 0.0, err
}
dr, err := net.OCR.Detect(img)
if err != nil {
return nil, []int{}, "nil", 0.0, err
}
img.Close()
var recognizedText string
var classIDs []int
var probabilities []float32
var rects []image.Rectangle
sort.Sort(Detections(dr.Detections))
for _, d := range dr.Detections {
for i := range d.ClassIDs {
probabilities = append(probabilities, d.Probabilities[i])
recognizedText += d.ClassNames[i]
classIDs = append(classIDs, d.ClassIDs[i])
bBox := d.BoundingBox
minX, minY := float64(bBox.StartPoint.X), float64(bBox.StartPoint.Y)
maxX, maxY := float64(bBox.EndPoint.X), float64(bBox.EndPoint.Y)
rect := image.Rect(round(minX), round(minY), round(maxX), round(maxY))
rects = append(rects, rect)
}
}
return rects, classIDs, recognizedText, sqrStdDeviation(probabilities), nil
}