zhangmeng
2019-12-11 2f5f0c75f3257b4ea9c37df6d02e5598b975740f
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
package sdk
 
import (
    "analysis/logo"
    "analysis/work"
    "context"
    "plugin"
    "sync"
 
    "basic.com/libgowrapper/sdkstruct.git"
 
    "basic.com/valib/gogpu.git"
)
 
const MaxFaceDetectThreadNum = 32
 
// EFDetect detect face
type EFDetect struct {
    threads int
    iGPU    int
 
    // track
    faceAngle  int
    faceNum    int
    interval   int
    sampleSize int
 
    ftrackChans     map[string]chan work.MsgRS
    ftrackChannels  map[string]int
    ftrackChanStats []bool
    chnLock         sync.Mutex
 
    handle          interface{}
    fnInit          func() interface{}
    fnFree          func(interface{})
    fnRun           func(interface{}, []byte, int, int, int, int) []sdkstruct.CFaceResult
    fnTrackerResize func(interface{}, int, int, int) bool
    fnExtractor     func(interface{}, int, int) bool
    fnPropertizer   func(interface{}, int) bool
    fnTracker       func(interface{}, int, int, int, int, int, int, int) bool
}
 
// NewEFDetectWithTrack with track
func NewEFDetectWithTrack(gi, thrds, faceNum, faceAngle, interval, samp int) *EFDetect {
    soFile := "libface.so"
 
    plug, err := plugin.Open(soFile)
    if err != nil {
        logo.Errorln("Open: ", soFile, " error: ", err)
        return nil
    }
 
    fnInit, _ := LoadFunc(plug, soFile, "NewSDK")
    fnFree, _ := LoadFunc(plug, soFile, "Free")
    fnRun, _ := LoadFunc(plug, soFile, "Run")
    fnTrackerResize, _ := LoadFunc(plug, soFile, "TrackerResize")
    fnExtractor, _ := LoadFunc(plug, soFile, "Extractor")
    fnPropertizer, _ := LoadFunc(plug, soFile, "Propertizer")
    fnTracker, _ := LoadFunc(plug, soFile, "Tracker")
 
    return &EFDetect{
        threads:         thrds,
        faceAngle:       faceAngle,
        ftrackChans:     make(map[string]chan work.MsgRS, MaxFaceDetectThreadNum),
        ftrackChannels:  make(map[string]int, MaxFaceDetectThreadNum),
        ftrackChanStats: make([]bool, MaxFaceDetectThreadNum, MaxFaceDetectThreadNum),
        faceNum:         faceNum,
        interval:        interval,
        iGPU:            gi,
        sampleSize:      samp,
 
        handle:          nil,
        fnInit:          fnInit.(func() interface{}),
        fnFree:          fnFree.(func(interface{})),
        fnRun:           fnRun.(func(interface{}, []byte, int, int, int, int) []sdkstruct.CFaceResult),
        fnTrackerResize: fnTrackerResize.(func(interface{}, int, int, int) bool),
        fnExtractor:     fnExtractor.(func(interface{}, int, int) bool),
        fnPropertizer:   fnPropertizer.(func(interface{}, int) bool),
        fnTracker:       fnTracker.(func(interface{}, int, int, int, int, int, int, int) bool),
    }
}
 
// Init impl interface
func (e *EFDetect) Init() bool {
 
    e.fakeInit()
 
    e.cleanChnStat()
 
    return true
}
 
func (e *EFDetect) fakeInit() {
    gpu := e.iGPU
 
    if gpu == -1 {
        gpu = gogpu.ValidGPU(2048)
    }
 
    e.handle = e.fnInit()
 
    if !e.fnTracker(e.handle, 1280, 720, e.faceNum, e.interval, e.sampleSize, e.threads, gpu) {
        logo.Errorln("FACE TRACKER CREATE ERROR")
    }
    logo.Infoln("Face Tracker Use GPU: ", gpu)
 
    if !e.fnPropertizer(e.handle, e.threads) {
        logo.Errorln("FACE PROPERTIZER CREATE ERROR")
    }
 
    if !e.fnExtractor(e.handle, e.threads, gpu) {
        logo.Errorln("FACE EXTRACTOR CREATE ERROR")
    }
 
    logo.Infoln("Face Extractor Use GPU: ", gpu)
}
 
// Run impl interface
func (e *EFDetect) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
    e.detectTrack(ctx, in, out, typ)
}