zhangmeng
2019-12-20 b0b52863a0dd4d9546a744615a1d2c0057e5caee
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
package sdk
 
import (
    "analysis/goconv"
    "analysis/logo"
    "analysis/work"
    "context"
 
    "github.com/gogo/protobuf/proto"
 
    "analysis/gohumantrack"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/gogpu.git"
)
 
type imageWithID struct {
    img *gohumantrack.ImageHumanTracker
    fx  float64
    fy  float64
    id  string
}
 
// HumanTracker track
type HumanTracker struct {
    tracker   *gohumantrack.HumanTracker
    gpu       int
    batchSize int
    flag      int
    list      *LockList
 
    mapCameraImageIndex map[string]int
    wait2RunMoreThan    int
    index               int
    images              []*imageWithID
    msgs                []*work.MsgRS
}
 
// NewHumanTracker new
func NewHumanTracker(gpu, batchSize, flag int) *HumanTracker {
    return &HumanTracker{
        gpu:       gpu,
        batchSize: batchSize,
        flag:      flag,
        list:      NewLockList(6),
 
        wait2RunMoreThan:    0,
        index:               0,
        mapCameraImageIndex: make(map[string]int),
        images:              make([]*imageWithID, batchSize),
        msgs:                make([]*work.MsgRS, batchSize),
    }
}
 
// Free free
func (t *HumanTracker) Free() {
    if t.tracker != nil {
        t.tracker.Free()
    }
}
 
// Init impl
func (t *HumanTracker) Init() bool {
 
    gpu := t.gpu
 
    if gpu == -1 {
        gpu = gogpu.ValidGPU(2048)
    }
    logo.Infof("start gpu %d, batch %d, flag %d\n", gpu, t.batchSize, t.flag)
    t.tracker = gohumantrack.NewHumanTracker(gpu, t.batchSize, t.flag)
    if t.tracker != nil {
        logo.Infoln("Start Success")
        return true
    }
    logo.Infoln("Start Failed")
    return false
}
 
func (t *HumanTracker) 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 {
        ejectResult(nil, rMsg, out)
        return
    }
 
    imgW, imgH := int(i.Width), int(i.Height)
 
    var images []gohumantrack.ImageHumanTracker
    img := gohumantrack.ImageHumanTracker{
        Data:    i.Data,
        Width:   imgW,
        Height:  imgH,
        Channel: 3,
    }
    images = append(images, img)
    res, err := t.tracker.Process(images)
    if err != nil {
        ejectResult(nil, rMsg, out)
        return
    }
 
    hr := convert2ProtoHumanTrackResult(res[0])
    result := protomsg.HumanTrackResult{Result: hr}
    data, err := proto.Marshal(&result)
    if err != nil {
        logo.Errorln("HUMAN TRACKER MARSHAL PROTO PLATE IDS 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, " Human Track COUNT: ", len(hr))
 
}
 
func (t *HumanTracker) trackBatch(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 {
        ejectResult(nil, rMsg, out)
        return
    }
 
    imgW, imgH := int(i.Width), int(i.Height)
 
    var imgData []byte
    rw, rh := 1280, 720
    fx, fy := 1.0, 1.0
    if imgW != rw || imgH != rh {
        imgData = goconv.ResizeBGR(i.Data, imgW, imgH, rw, rh)
        fx = (float64)(rw) / (float64)(imgW)
        fy = (float64)(rh) / (float64)(imgH)
    }
    img := gohumantrack.ImageHumanTracker{
        Data:    imgData,
        Width:   rw,
        Height:  rh,
        Channel: 3,
    }
 
    // 查找是否有这个摄像机图片的位置,有就替换成最新的,并且保存rMsg
    if i, ok := t.mapCameraImageIndex[rMsg.Msg.Cid]; ok {
        if i < t.batchSize {
            t.images[i] = &imageWithID{&img, fx, fy, rMsg.Msg.Cid}
            t.msgs[i] = &rMsg
        }
    } else {
        // 没有,如果index小于最大的batch,创建一个新的位置
        if t.index < t.batchSize {
            t.images[t.index] = &imageWithID{&img, fx, fy, rMsg.Msg.Cid}
            t.msgs[t.index] = &rMsg
            t.mapCameraImageIndex[rMsg.Msg.Cid] = t.index
        }
        t.index++
    }
    // 等收到次数大于?次后开始运行,因为不一定各自摄像机的图片都会到
    t.wait2RunMoreThan++
    if t.wait2RunMoreThan < t.batchSize+t.batchSize/2 {
        return
    }
 
    for k, v := range t.mapCameraImageIndex {
        logo.Infoln("batch~~~~~~Map index: ", v, " camera: ", k)
    }
 
    var pimg []*gohumantrack.ImageHumanTracker
    for k, v := range t.images[:] {
        if v != nil {
            pimg = append(pimg, v.img)
            logo.Infoln("batch~~~~~~Image index: ", k, " camera: ", v.id, " image address: ", v.img)
        } else {
            pimg = append(pimg, nil)
        }
    }
 
    res, err := t.tracker.ProcessImagePointer(pimg, rw, rh, 3)
    // 从0开始计算收到了多少次图片
    t.wait2RunMoreThan = 0
    // 如果index大于最大batch,说明摄像机可能改变了,清空所有结构重新开始
    if t.index > t.batchSize {
        t.mapCameraImageIndex = make(map[string]int)
    }
 
    if err != nil {
        logo.Infoln("batch~~~~~~Track Image Count: ", t.index, " Failed: ", err)
        // 将images设置为nil,重新开始一个流程
        for i := 0; i < t.batchSize; i++ {
            t.images[i] = nil
            t.msgs[i] = nil
        }
 
        ejectResult(nil, rMsg, out)
        return
    }
 
    for i := 0; i < t.batchSize; i++ {
        if t.images[i] == nil {
            continue
        }
        hr := convert2ProtoHumanTrackResultWithScale(res[i], t.images[i].fx, t.images[i].fy)
        result := protomsg.HumanTrackResult{Result: hr}
        data, err := proto.Marshal(&result)
        if err != nil {
            logo.Errorln("batch~~~~~~HUMAN TRACKER MARSHAL PROTO PLATE IDS ERROR", err)
            data = nil
        }
        msg := *t.msgs[i]
        ejectResult(data, msg, out)
        var id, name string
        if msg.Msg.Tasklab != nil {
            id, name = msg.Msg.Tasklab.Taskid, msg.Msg.Tasklab.Taskname
        }
        logo.Infoln("batch~~~~~~CAMERAID: ", msg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " Human Track COUNT: ", len(hr))
 
    }
    // 将images设置为nil,重新开始一个流程
    for i := 0; i < t.batchSize; i++ {
        t.images[i] = nil
        t.msgs[i] = nil
    }
 
}
 
// Run impl
func (t *HumanTracker) Run(ctx context.Context, in <-chan work.MsgRS, out chan<- work.MsgRS, typ string) {
    // FlowSimple(ctx, in, out, typ, t.list.Push, t.list.Pop, t.track, t.Free)
    FlowSimple(ctx, in, out, typ, t.list.Push, t.list.Pop, t.trackBatch, t.Free)
}
 
// message HumanTrack {
//     Rect rcHuman = 1;
//     float confidence = 2;
//     int32 x = 3;
//     int32 y = 4;
//     int32 id = 5;
//     repeated float feature = 6;
// }
 
// message HumanTrackResult {
//     repeated HumanTrack result = 1;
// }
 
func convert2ProtoHumanTrackResult(obj gohumantrack.FgResult) []*protomsg.HumanTrack {
    res := []*protomsg.HumanTrack{}
    for i := 0; i < int(obj.FgNum); i++ {
        r := obj.Fginfo[i]
        rect := protomsg.Rect{
            Left:   r.Left,
            Right:  r.Right,
            Top:    r.Top,
            Bottom: r.Bottom,
        }
        pr := &protomsg.HumanTrack{
            RcHuman:    &rect,
            Confidence: r.Confidence,
            X:          r.X,
            Y:          r.Y,
            Id:         r.ID,
            Feature:    r.Feature[:],
        }
        res = append(res, pr)
    }
    return res
}
 
func convert2ProtoHumanTrackResultWithScale(obj gohumantrack.FgResult, fx, fy float64) []*protomsg.HumanTrack {
    res := []*protomsg.HumanTrack{}
    for i := 0; i < int(obj.FgNum); i++ {
        r := obj.Fginfo[i]
        rect := protomsg.Rect{
            Left:   (int32)((float64)(r.Left) / fx),
            Right:  (int32)((float64)(r.Right) / fy),
            Top:    (int32)((float64)(r.Top) / fx),
            Bottom: (int32)((float64)(r.Bottom) / fy),
        }
        pr := &protomsg.HumanTrack{
            RcHuman:    &rect,
            Confidence: r.Confidence,
            X:          (int32)((float64)(r.X) / fx),
            Y:          (int32)((float64)(r.Y) / fy),
            Id:         r.ID,
            Feature:    r.Feature[:],
        }
        res = append(res, pr)
    }
    return res
}