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
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
package sdk
 
import (
    "analysis/logo"
    "analysis/work"
    "container/list"
    "context"
    "fmt"
    "plugin"
    "sync"
    "time"
 
    "basic.com/libgowrapper/sdkstruct.git"
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/gogpu.git"
    "github.com/gogo/protobuf/proto"
)
 
// YoloDetect yolo detect
type YoloDetect struct {
    iGPU    int
    cfg     string
    weights string
    name    string
 
    cache *list.List
    cv    *sync.Cond
    cond  bool
 
    handle interface{}
    fnInit func(string, string, string, int) interface{}
    fnFree func(interface{})
    fnRun  func(interface{}, string, []byte, int, int, int, float32, int) ([]sdkstruct.CObjTrackInfo, []sdkstruct.CObjTrackInfo)
}
 
// NewYDetectWithTrack with track
func NewYDetectWithTrack(gi int, cfg, weights, name string) *YoloDetect {
    soFile := "libyolo.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")
 
    return &YoloDetect{
        iGPU:    gi,
        cfg:     cfg,
        weights: weights,
        name:    name,
        cache:   list.New(),
        cv:      sync.NewCond(&sync.Mutex{}),
        cond:    false,
 
        handle: nil,
        fnInit: fnInit.(func(string, string, string, int) interface{}),
        fnFree: fnFree.(func(interface{})),
        fnRun:  fnRun.(func(interface{}, string, []byte, int, int, int, float32, int) ([]sdkstruct.CObjTrackInfo, []sdkstruct.CObjTrackInfo)),
    }
}
 
// Init impl interface
func (y *YoloDetect) Init() bool {
 
    gpu := y.iGPU
    if gpu == -1 {
        gpu = gogpu.ValidGPU(2048)
    }
    h := y.fnInit(y.cfg, y.weights, y.name, gpu)
    logo.Infoln("YOLO USE GPU: ", gpu)
 
    if h == nil {
        logo.Errorln("CREATE YOLO DETECTOR ERROR")
        return false
    }
 
    y.handle = h
    return true
}
 
// Run impl interface
func (y *YoloDetect) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
    go y.work(ctx, out, typ)
 
    for {
        select {
        case <-ctx.Done():
            y.fnFree(y.handle)
            return
        default:
            rMsg := <-in
            if !validRemoteMessage(rMsg, typ) {
                logo.Errorln("yolo validremotemessage invalid")
                ejectResult(nil, rMsg, out)
                continue
            }
            y.push(rMsg)
        }
    }
 
}
 
func (y *YoloDetect) track(rMsg work.MsgRS, out chan<- work.MsgRS, typ string) {
 
    /////////////////////////////////////////////
    i := unpackImage(rMsg, typ)
    if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
        logo.Errorln("yolo image error: ", i)
        ejectResult(nil, rMsg, out)
 
        return
    }
 
    imgW, imgH := int(i.Width), int(i.Height)
 
    whole, recent := y.fnRun(y.handle, rMsg.Msg.Cid, i.Data, imgW, imgH, 3, 0.4, 0)
    if len(recent) > 0 {
    }
 
    infos := convert2ProtoYoloTrack(whole, 1.0, 1.0)
    p := protomsg.ParamYoloObj{Infos: infos}
 
    data, err := proto.Marshal(&p)
    if err != nil {
        fmt.Println("ydetect track marshal proto yolo obj error", err)
        data = nil
    }
 
    ejectResult(data, rMsg, out)
 
    var id, name string
    if rMsg.Msg.Tasklab != nil {
        id, name = rMsg.Msg.Tasklab.Taskid, rMsg.Msg.Tasklab.Taskname
    }
 
    logo.Infoln("CAMERAID: ", rMsg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " DETECT YOLO COUNT: ", len(whole))
}
 
func convert2ProtoYoloTrack(obj []sdkstruct.CObjTrackInfo, fx, fy float64) []*protomsg.ObjInfo {
    ret := []*protomsg.ObjInfo{}
 
    for _, v := range obj {
        if fx < 1.0 || fy < 1.0 {
            v.ObjInfo.RcObj.Left = (int32)((float64)(v.ObjInfo.RcObj.Left) / fx)
            v.ObjInfo.RcObj.Right = (int32)((float64)(v.ObjInfo.RcObj.Right) / fx)
            v.ObjInfo.RcObj.Top = (int32)((float64)(v.ObjInfo.RcObj.Top) / fy)
            v.ObjInfo.RcObj.Bottom = (int32)((float64)(v.ObjInfo.RcObj.Bottom) / fy)
        }
 
        rect := protomsg.Rect{
            Left:   v.ObjInfo.RcObj.Left,
            Right:  v.ObjInfo.RcObj.Right,
            Top:    v.ObjInfo.RcObj.Top,
            Bottom: v.ObjInfo.RcObj.Bottom,
        }
        obj := protomsg.ObjInfo{
            RcObj: &rect,
            Typ:   v.ObjInfo.Typ,
            Prob:  v.ObjInfo.Prob,
            ObjID: v.ID,
        }
 
        ret = append(ret, &obj)
    }
    return ret
}
 
func (y *YoloDetect) work(ctx context.Context, out chan<- work.MsgRS, typ string) {
    tm := time.Now()
    sc := 0
 
    for {
        select {
        case <-ctx.Done():
            return
        default:
 
            y.cv.L.Lock()
 
            for !y.cond {
                y.cv.Wait()
            }
 
            rMsg := y.cache.Front().Value.(work.MsgRS)
 
            y.track(rMsg, out, typ)
            y.cache.Remove(y.cache.Front())
            y.cond = false
            y.cv.L.Unlock()
 
            sc++
            if sc == 25 {
                logo.Infoln("YOLO RUN 25 FRAME USE TIME: ", time.Since(tm))
                sc = 0
                tm = time.Now()
            }
            if time.Since(tm) > time.Second {
                logo.Infof("YOLO RUN %d FRAME USE TIME: %v", sc, time.Since(tm))
                sc = 0
                tm = time.Now()
            }
        }
    }
 
}
 
func (y *YoloDetect) push(data work.MsgRS) {
    y.cv.L.Lock()
    y.cache.PushBack(data)
    if y.cache.Len() > 12 {
        for i := 0; i < y.cache.Len(); {
            y.cache.Remove(y.cache.Front())
            i = i + 2
        }
    }
    // logo.Infof("push to cache count : %d\n", t.cache.Len())
    y.cond = true
    y.cv.Signal()
    y.cv.L.Unlock()
}