-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcascade_classifier.go
238 lines (214 loc) · 5.41 KB
/
cascade_classifier.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
package opencv
import (
"fmt"
"gopkg.in/sensorbee/opencv.v0/bridge"
"gopkg.in/sensorbee/sensorbee.v0/core"
"gopkg.in/sensorbee/sensorbee.v0/data"
)
var (
configFilePath = data.MustCompilePath("file")
xPath = data.MustCompilePath("x")
yPath = data.MustCompilePath("y")
)
// NewCascadeClassifier returns cascadeClassifier state.
//
// file: cascade configuration file path for detection.
// e.g. "haarcascade_frontalface_default.xml".
func NewCascadeClassifier(ctx *core.Context, params data.Map) (core.SharedState,
error) {
var filePath string
if fp, err := params.Get(configFilePath); err != nil {
return nil, err
} else if filePath, err = data.AsString(fp); err != nil {
return nil, err
}
cc := bridge.NewCascadeClassifier()
if !cc.Load(filePath) {
return nil, fmt.Errorf("cannot load the file '%v'", filePath)
}
return &cascadeClassifier{
classifier: cc,
}, nil
}
type cascadeClassifier struct {
classifier bridge.CascadeClassifier
}
func (c *cascadeClassifier) Terminate(ctx *core.Context) error {
c.classifier.Delete()
return nil
}
func lookupCascadeClassifier(ctx *core.Context, name string) (*cascadeClassifier,
error) {
st, err := ctx.SharedStates.Get(name)
if err != nil {
return nil, err
}
if s, ok := st.(*cascadeClassifier); ok {
return s, nil
}
return nil, fmt.Errorf("state '%v' cannot be converted to cascade_classifier.state",
name)
}
// DetectMultiScale classifies and detect image.
//
// classifierName: cascadeClassifier state name.
//
// img: target image as RawData map structure.
func DetectMultiScale(ctx *core.Context, classifierName string, img data.Map) (
data.Array, error) {
raw, err := ConvertMapToRawData(img)
if err != nil {
return nil, err
}
mat, err := raw.ToMatVec3b()
if err != nil {
return nil, err
}
defer mat.Delete()
classifier, err := lookupCascadeClassifier(ctx, classifierName)
if err != nil {
return nil, err
}
rects := classifier.classifier.DetectMultiScale(mat)
ret := make(data.Array, len(rects))
for i, r := range rects {
rect := data.Map{
"x": data.Int(r.X),
"y": data.Int(r.Y),
"width": data.Int(r.Width),
"height": data.Int(r.Height),
}
ret[i] = rect
}
return ret, nil
}
// DrawRectsToImage draws rectangle information on target image. The image is
// required to structured as RawData.
func DrawRectsToImage(img data.Map, rects data.Array) (data.Map, error) {
if len(rects) == 0 {
return img, nil
}
raw, err := ConvertMapToRawData(img)
if err != nil {
return nil, err
}
// copy image binary
temp := make([]byte, len(raw.Data))
copy(temp, raw.Data)
raw.Data = temp
mat, err := raw.ToMatVec3b()
if err != nil {
return nil, err
}
defer mat.Delete()
brRects, err := convertToBridgeRects(rects)
if err != nil {
return nil, err
}
bridge.DrawRectsToImage(mat, brRects)
retRaw := ToRawData(mat)
return retRaw.ConvertToDataMap(), nil
}
func convertToBridgeRects(rects data.Array) ([]bridge.Rect, error) {
brRects := make([]bridge.Rect, len(rects))
for i, r := range rects {
rmap, err := data.AsMap(r)
if err != nil {
return nil, err
}
var x int64
if xv, err := rmap.Get(xPath); err != nil {
return nil, err
} else if x, err = data.ToInt(xv); err != nil {
return nil, err
}
var y int64
if yv, err := rmap.Get(yPath); err != nil {
return nil, err
} else if y, err = data.ToInt(yv); err != nil {
return nil, err
}
var width int64
if wv, err := rmap.Get(widthPath); err != nil {
return nil, err
} else if width, err = data.ToInt(wv); err != nil {
return nil, err
}
var height int64
if hv, err := rmap.Get(heightPath); err != nil {
return nil, err
} else if height, err = data.ToInt(hv); err != nil {
return nil, err
}
rect := bridge.Rect{
X: int(x),
Y: int(y),
Width: int(width),
Height: int(height),
}
brRects[i] = rect
}
return brRects, nil
}
// NewSharedImage returns shared image file to reduce I/O cost.
func NewSharedImage(ctx *core.Context, params data.Map) (core.SharedState, error) {
var filePath string
if fp, err := params.Get(configFilePath); err != nil {
return nil, err
} else if filePath, err = data.AsString(fp); err != nil {
return nil, err
}
mat := bridge.LoadAlphaImage(filePath)
return &sharedImage{
img: mat,
}, nil
}
type sharedImage struct {
img bridge.MatVec4b
}
func (s *sharedImage) Terminate(ctx *core.Context) error {
s.img.Delete()
return nil
}
func lookupSharedImage(ctx *core.Context, name string) (*sharedImage, error) {
st, err := ctx.SharedStates.Get(name)
if err != nil {
return nil, err
}
if s, ok := st.(*sharedImage); ok {
return s, nil
}
return nil, fmt.Errorf("state '%v' cannot be converted to shared_image.state",
name)
}
// MountAlphaImage draw target image on back image.
func MountAlphaImage(ctx *core.Context, imgName string, back data.Map,
rects data.Array) (data.Map, error) {
if len(rects) == 0 {
return back, nil
}
img, err := lookupSharedImage(ctx, imgName)
if err != nil {
return nil, err
}
raw, err := ConvertMapToRawData(back)
if err != nil {
return nil, err
}
// copy image binary
temp := make([]byte, len(raw.Data))
copy(temp, raw.Data)
raw.Data = temp
mat, err := raw.ToMatVec3b()
if err != nil {
return nil, err
}
defer mat.Delete()
brRects, err := convertToBridgeRects(rects)
if err != nil {
return nil, err
}
bridge.MountAlphaImage(img.img, mat, brRects)
retRaw := ToRawData(mat)
return retRaw.ConvertToDataMap(), nil
}