-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.go
377 lines (334 loc) · 8.94 KB
/
models.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package sf6go
// #cgo CXXFLAGS: -std=c++11 -Wall -O3 -DNDEBUG -march=native
// #cgo LDFLAGS: -ltennis -ltennis_haswell -ltennis_sandy_bridge -ltennis_pentium -lSeetaAuthorize
// #include <stdlib.h>
// #include "CStruct.h"
// #include "CFaceInfo.h"
// #include "CTrackingFaceInfo.h"
import "C"
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"reflect"
"unsafe"
)
// _model_base_path 模型基础路径
var _model_base_path string
// 初始化模型目录
func InitModelPath(path string) error {
fi, err := os.Stat(path)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("%v不是目录", path)
}
_model_base_path = path
return nil
}
// ModelType 模型类型
type ModelType uint8
const (
ModelType_default ModelType = iota // 默认模型,68特征点
ModelType_light // 轻量级模型,5特征点
ModelType_mask // 口罩模型,5特征点
)
// SeetaImageData 图像数据结构
type SeetaImageData struct {
_ptr C.struct_SeetaImageData
cdata []C.uchar //此数据最终将指针交给c处理,此数据时为了方式数据逃逸,方便go释放内存
}
func (s *SeetaImageData) GetWidth() int {
return int(s._ptr.width)
}
func (s *SeetaImageData) GetHeight() int {
return int(s._ptr.height)
}
func (s *SeetaImageData) GetChannels() int {
return int(s._ptr.channels)
}
func (s *SeetaImageData) GetData() []uint8 {
size := len(s.cdata)
data := make([]uint8, size)
for i := 0; i < size; i++ {
data[i] = uint8(s.cdata[i])
}
return data
}
func (s *SeetaImageData) GetImage() image.Image {
width := s.GetWidth()
height := s.GetHeight()
channel := s.GetChannels()
img := image.NewRGBA(image.Rect(0, 0, width, height))
index := 0
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.SetRGBA(x, y, color.RGBA{
B: uint8(s.cdata[index]),
G: uint8(s.cdata[index+1]),
R: uint8(s.cdata[index+2]),
})
index += channel
}
}
return img
}
func (s *SeetaImageData) CutFace(rect *SeetaRect) image.Image {
rx := rect.GetX()
ry := rect.GetY()
width := rect.GetWidth()
height := rect.GetHeight()
channel := s.GetChannels()
if rx-(height-width)/2 >= 0 && (rx+width)+(height-width)/2 <= s.GetWidth() {
rx = rx - (height-width)/2
width = height
}
// else if rx > 0 {
// width += rx * 2
// rx = 0
// }
img := image.NewRGBA(image.Rect(0, 0, width, height))
originalWidth := s.GetWidth()
dataLen := len(s.cdata)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
index := (ry+y)*originalWidth*channel + (rx+x)*channel
if index >= dataLen {
continue
}
img.SetRGBA(x, y, color.RGBA{
B: uint8(s.cdata[index]),
G: uint8(s.cdata[index+1]),
R: uint8(s.cdata[index+2]),
})
}
}
return img
}
func (s *SeetaImageData) getCStruct() C.struct_SeetaImageData {
s.Reset()
return s._ptr
}
func (s *SeetaImageData) SetUint8(data []uint8) error {
if len(s.cdata) != len(data) {
return fmt.Errorf("设置的数据与初始化的大小不符")
}
for i, v := range data {
s.cdata[i] = C.uchar(v)
}
return nil
}
func (s *SeetaImageData) Reset() {
s._ptr.data = &s.cdata[0]
}
func (s *SeetaImageData) Close() {
// C.free(unsafe.Pointer(&s.cdata))
// C.free(unsafe.Pointer(&s.ptr))
// C.free(unsafe.Pointer(&s.ptr.width))
// C.free(unsafe.Pointer(&s.ptr.height))
// C.free(unsafe.Pointer(&s.ptr.channels))
}
// NewSeetaImageData 创建一个sf6图片(不包含数据)
// 创建后需要通过SetUint8设置数据,一般用于opencv的mat转换
func NewSeetaImageData(width, height, channels int) *SeetaImageData {
imageData := &SeetaImageData{
cdata: make([]C.uchar, width*height*channels),
_ptr: C.struct_SeetaImageData{
width: C.int(width),
height: C.int(height),
channels: C.int(channels),
},
}
imageData._ptr.data = &imageData.cdata[0]
return imageData
}
func NewSeetaImageDataFromCStruct(cstruct C.struct_SeetaImageData) *SeetaImageData {
imageData := &SeetaImageData{
_ptr: cstruct,
}
var clist []C.uchar
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&clist))
arrayLen := int(imageData.GetWidth() * imageData.GetHeight() * imageData.GetChannels())
sliceHeader.Cap = arrayLen
sliceHeader.Len = arrayLen
sliceHeader.Data = uintptr(unsafe.Pointer(cstruct.data))
var cdata []C.uchar = make([]C.uchar, arrayLen)
for i := 0; i < arrayLen; i++ {
cdata[i] = clist[i]
}
imageData.cdata = cdata
imageData._ptr.data = &imageData.cdata[0]
return imageData
}
// NewSeetaImageDataFromBase64 通过base64图片数据创建sf6图片数据
func NewSeetaImageDataFromBase64(data string) (*SeetaImageData, error) {
buffer, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
reader := bytes.NewReader(buffer)
img, foramt, err := image.Decode(reader)
if err != nil {
log.Println(foramt)
return nil, err
}
return NewSeetaImageDataFromImage(img), nil
}
// NewSeetaImageDataFromFile 根据文件路径创建sf6图片数据
func NewSeetaImageDataFromFile(filePath string) (*SeetaImageData, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
img, foramt, err := image.Decode(file)
if err != nil {
log.Println(foramt)
return nil, err
}
return NewSeetaImageDataFromImage(img), nil
}
// NewSeetaImageDataFromImage 根据go 内置image数据创建sf6图片数据
func NewSeetaImageDataFromImage(img image.Image) *SeetaImageData {
rect := img.Bounds()
width := rect.Dx()
height := rect.Dy()
channels := 3
imageData := &SeetaImageData{
cdata: make([]C.uchar, width*height*channels),
_ptr: C.struct_SeetaImageData{
width: C.int(width),
height: C.int(height),
channels: C.int(channels),
},
}
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
c := img.At(rect.Min.X+x, rect.Min.Y+y)
r, g, b, _ := c.RGBA()
offset := y*width*channels + x*channels
imageData.cdata[offset] = C.uchar(b >> 8)
imageData.cdata[offset+1] = C.uchar(g >> 8)
imageData.cdata[offset+2] = C.uchar(r >> 8)
}
}
imageData._ptr.data = &imageData.cdata[0]
return imageData
}
// SeetaRect 人脸位置信息
type SeetaRect struct {
_ptr C.struct_SeetaRect
}
func newSeetaRect(seetaRect C.struct_SeetaRect) *SeetaRect {
return &SeetaRect{
_ptr: seetaRect,
}
}
func (s *SeetaRect) getCStruct() C.struct_SeetaRect {
return s._ptr
}
func (s *SeetaRect) GetX() int {
return int(s._ptr.x)
}
func (s *SeetaRect) GetY() int {
return int(s._ptr.y)
}
func (s *SeetaRect) GetWidth() int {
return int(s._ptr.width)
}
func (s *SeetaRect) GetHeight() int {
return int(s._ptr.height)
}
type SeetaFaceInfo struct {
Postion *SeetaRect
Score float32
}
func NewSeetaFaceInfo(seetaFaceInfo C.struct_SeetaFaceInfo) *SeetaFaceInfo {
return &SeetaFaceInfo{
Postion: newSeetaRect(seetaFaceInfo.pos),
Score: float32(seetaFaceInfo.score),
}
}
// SeetaTrackingFaceInfo 人脸追踪结果信息
type SeetaTrackingFaceInfo struct {
Postion *SeetaRect
Score float32
Frame_NO int
PID int
Step int
}
func NewSeetaTrackingFaceInfo(seetaTrackingFaceInfo C.struct_SeetaTrackingFaceInfo) *SeetaTrackingFaceInfo {
return &SeetaTrackingFaceInfo{
Postion: newSeetaRect(seetaTrackingFaceInfo.pos),
Score: float32(seetaTrackingFaceInfo.score),
Frame_NO: int(seetaTrackingFaceInfo.frame_no),
PID: int(seetaTrackingFaceInfo.PID),
Step: int(seetaTrackingFaceInfo.step),
}
}
// SeetaPointInfo 人脸特征点信息
type SeetaPointInfo struct {
PointCount int // 特征点数
Points []C.struct_SeetaPointF
Masks []int
}
func NewSeetaPointInfo(pointCount int) *SeetaPointInfo {
return &SeetaPointInfo{
PointCount: pointCount,
Points: make([]C.struct_SeetaPointF, pointCount),
Masks: make([]int, pointCount),
}
}
func (s *SeetaPointInfo) getCSeetaPointFArray() *C.struct_SeetaPointF {
return &s.Points[0]
}
// Mask 是否佩戴口罩
func (s *SeetaPointInfo) Mask() bool {
maskCount := 0
for i := 2; i < len(s.Masks); i++ {
maskCount += s.Masks[i]
}
return maskCount >= 2
}
// SeetaModelSetting 模型配置数据结构
// type SeetaModelSetting struct {
// ptr *C.struct_SeetaModelSetting
// }
// func NewSeetaModelSetting(model string) *SeetaModelSetting {
// var setting C.struct_SeetaModelSetting
// setting.device = C.SEETA_DEVICE_AUTO
// setting.id = 0
// css := make([]*C.char, len(models))
// for i, v := range models {
// cs := C.CString(v)
// defer C.free(unsafe.Pointer(cs))
// css[i] = cs
// }
// setting.model = &css[0]
// return &SeetaModelSetting{
// ptr: &setting,
// }
// }
func TestCStruct() {
a := NewSeetaImageData(320, 160, 3)
defer a.Close()
log.Println(a.getCStruct())
}
// GoStrings 将字符串数组转换成go的字符串数组
// func GoStrings(argc C.int, argv **C.char) []string {
// length := int(argc)
// tmpslice := (*[1 << 30]*C.char)(unsafe.Pointer(argv))[:length:length]
// gostrings := make([]string, length)
// for i, s := range tmpslice {
// gostrings[i] = C.GoString(s)
// }
// return gostrings
// }