-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (53 loc) · 1.53 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
package main
import (
"fmt"
"image"
"image/color"
"io/ioutil"
"github.com/openalpr/openalpr/src/bindings/go/openalpr"
"gocv.io/x/gocv"
)
func main() {
alpr := openalpr.NewAlpr("eu", "/etc/openalpr/openalpr.conf", "/var/lib/openalpr/runtime_data")
defer alpr.Unload()
if !alpr.IsLoaded() {
fmt.Println("OpenAlpr failed to load!")
return
}
alpr.SetTopN(20)
fileWithLicensePlate := "./examples/car6.png"
imageBytes, err := ioutil.ReadFile(fileWithLicensePlate)
if err != nil {
fmt.Println(err)
}
resultFromBlob, err := alpr.RecognizeByBlob(imageBytes)
fmt.Printf("%+v\n", resultFromBlob)
// Plates
plates := resultFromBlob.Plates
// Get first plate, we only assume 1 plate.
plate := plates[0]
// Get plate number
platePoints := plate.PlatePoints
// Read image to Mat
mat := gocv.IMRead(fileWithLicensePlate, gocv.IMReadColor)
// Convert plate coordinates to rectangle
if len(platePoints) != 4 {
fmt.Println("Plate points are not 4")
return
}
x := platePoints[0].X
y := platePoints[0].Y
w := platePoints[2].X - platePoints[0].X
h := platePoints[2].Y - platePoints[0].Y
// Rectangle
rect := image.Rect(x, y, x+w, y+h)
// Draw rectangle
gocv.Rectangle(&mat, rect, color.RGBA{0, 255, 0, 0}, 2)
// Get best match plate number
plateNumber := plate.TopNPlates[0].Characters
fmt.Println("License Plate: " + plateNumber)
// Draw text
gocv.PutText(&mat, plateNumber, image.Pt(x, y-20), gocv.FontHersheyPlain, 2, color.RGBA{0, 255, 0, 0}, 2)
// Save image to tmp folder
gocv.IMWrite("/tmp/car6_out.png", mat)
}