zhangmeng
2019-12-19 88e51b749b1306dc9573603af116984d7249ced9
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
package sdk
 
import (
    "analysis/logo"
    "analysis/work"
    "context"
 
    "github.com/gogo/protobuf/proto"
 
    "analysis/gohumantrack"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/gogpu.git"
)
 
// HumanTracker track
type HumanTracker struct {
    tracker   *gohumantrack.HumanTracker
    gpu       int
    batchSize int
    flag      int
    list      *LockList
 
    mapCameraImageIndex map[string]int
    recvImageCount      int
    index               int
    images              [batchSize]*gohumantrack.ImageHumanTracker
    msgs                [batchSize]*work.MsgRS
}
 
// NewHumanTracker new
func NewHumanTracker(gpu, batchSize, flag int) *HumanTracker {
    return &HumanTracker{
        gpu:       gpu,
        batchSize: batchSize,
        flag:      flag,
        list:      NewLockList(6),
 
        recvImageCount: 0,
        index:          0,
    }
}
 
// Free free
func (t *HumanTracker) Free() {
    if t.tracker != nil {
        t.tracker.Free()
    }
}
 
// Init impl
func (t *HumanTracker) Init() bool {
    if t.batchSize != 1 {
        logo.Errorln("ONLY SUPPORT BATCH SIZE = 1")
        return false
    }
    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)
    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[0]))
 
}
 
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)
 
    img := gohumantrack.ImageHumanTracker{
        Data:    i.Data,
        Width:   imgW,
        Height:  imgH,
        Channel: 3,
    }
 
    // mapCameraImageIndex map[string]int
    // images              []gohumantrack.ImageHumanTracker
 
    t.recvImageCount++
    if t.mapCameraImageIndex == nil {
        t.mapCameraImageIndex = make(map[string]int)
        for i := 0; i < t.batchSize; i++ {
            t.images[i] = nil
        }
        for i := 0; i < t.batchSize; i++ {
            t.msgs[i] = nil
        }
    }
    if i, ok := t.mapCameraImageIndex[rMsg.Msg.Cid]; ok {
        if i < batchSize {
            t.images[i] = &img
            t.msgs[i] = &rMsg
        }
    } else {
        if t.index < batchSize {
            t.images[t.index] = &img
            t.msgs[t.index] = &rMsg
            t.mapCameraImageIndex[rMsg.Msg.Cid] = t.index
        }
        t.index++
    }
    if t.recvImageCount < t.batchSize+t.batchSize/2 {
        return
    }
 
    res, err := t.tracker.ProcessImagePointer(t.images[:])
 
    if err != nil {
        t.mapCameraImageIndex = nil
        ejectResult(nil, rMsg, out)
        return
    }
 
    for i := 0; i < t.batchSize; i++ {
        if t.images[i] == nil {
            continue
        }
        hr := convert2ProtoHumanTrackResult(res[i])
        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
        }
        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("CAMERAID: ", msg.Msg.Cid, " TASKID: ", id, " TASKNAME: ", name, " Human Track COUNT: ", len(hr[0]))
 
    }
 
}
 
// 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(v.FgNum); i++ {
        r := v.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
}