reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-02-24 d9b98e947f7b5aae886ff02ec27bd92aa34feaaa
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
package main
 
/*
#include <stdlib.h>
#include <string.h>
void* crop_image(void *vsrc, int srcW, int srcH, int x0, int y0, int x1, int y1, int channel, int *length)
{
    if (x0 < 0) x0 = 0;
    if (x0 > srcW) x0 = srcW-1;
    if (x1 < 0) x1 = 0;
    if (x1 > srcW) x1 = srcW-1;
 
    if (y0 < 0) y0 = 0;
    if (y0 > srcH) y0 = srcH-1;
    if (y1 < 0) y1 = 0;
    if (y1 > srcH) y1 = srcH-1;
 
    if (x1 - x0 <= 0 || y1 - y0 <= 0) return NULL;
 
    if (x1-x0 > srcW) x1 = srcW-x0;
    if (y1-y0 > srcH) y1 = srcH-y0;
 
 
    unsigned char *src = (unsigned char*)vsrc;
 
    int destW = x1 - x0 + 1;
    int destH = y1 - y0 + 1;
 
    *length = channel * destW * destH;
    unsigned char * desData = (unsigned char*)malloc(*length);
 
    int i = 0;
    int destIdy = 0;
 
    for (i = y0; i <= y1; i++)
    {
        destIdy = i - y0;
        memcpy(&(desData[destIdy * destW * channel]), &(src[(i * srcW + x0) * channel]),sizeof(char) * channel * destW);
    }
 
    return desData;
}*/
import "C"
import (
    "context"
    "time"
    "unsafe"
 
    "reid/rpc"
 
    "reid/common"
 
    "basic.com/valib/gogpu.git"
 
    "basic.com/pubsub/protomsg.git"
 
    "github.com/gogo/protobuf/proto"
)
 
type reid struct {
    handle   *ReID
    fnLogger func(...interface{})
 
    gpu int
    ipc string
}
 
// Create Reid
func Create(config string, typ, id string, gpu int, shm bool, fn func(...interface{}), reserved map[string]interface{}) interface{} {
 
    cfg, err := common.ReadConfig(config)
    if err != nil {
        fn("Reid SDK Create Error When Read Config: ", err)
        return nil
    }
 
    sModel, sGPU, sIPC :=
        "reid-model",
        "gpu-memory",
        "ipc-url"
 
    params := []string{sModel, sGPU, sIPC}
 
    for _, v := range params {
        if _, ok := cfg.Param[v]; !ok {
            fn("Reid SDK Create Error Because of Param Not Found: ", v)
            return nil
        }
    }
 
    gpuM := common.Atoi(cfg.Param[sGPU])
 
    rGPU := gpu
 
    if rGPU == -1 {
        rGPU = gogpu.ValidGPU(gpuM + 512)
    }
 
    handle := NewSDK(rGPU, cfg.Param[sModel])
    if handle == nil {
        fn("Reid SDK Create Error When New SDK")
        return nil
    }
 
    return &reid{
        handle:   handle,
        fnLogger: fn,
 
        gpu: rGPU,
        ipc: cfg.Param[sIPC],
    }
}
 
// Run run
func Run(ctx context.Context, i interface{}) {
    s := i.(*reid)
 
    const (
        postPull = `_2`
        postPush = `_1`
    )
 
    ipcSnd := s.ipc + postPush
    ipcRcv := s.ipc + postPull
 
    sndURL := common.GetIpcAddress(true, ipcSnd)
    rcvURL := common.GetIpcAddress(true, ipcRcv)
 
    chSnd := make(chan []byte, 3)
    chRcv := make(chan []byte, 3)
 
    recv := rpc.NewReciever(rcvURL, chRcv, true, s.fnLogger)
    go recv.Run(ctx)
 
    chMsg := make(chan protomsg.SdkMessage, 3)
    go common.UnserilizeProto(ctx, chRcv, chMsg, s.fnLogger)
 
    send := rpc.NewSender(sndURL, chSnd, true, s.fnLogger)
    go send.Run(ctx)
 
    for {
        select {
        case <-ctx.Done():
            return
        case msg := <-chMsg:
            if len(msg.Tasklab.Sdkinfos) == 0 || int(msg.Tasklab.Index) >= len(msg.Tasklab.Sdkinfos) {
                s.fnLogger("reid !!!!!! Recv Msg From Humantrack Error")
                continue
            }
            i := common.UnpackImage(msg, "reid", s.fnLogger)
            if i == nil || i.Data == nil || i.Width <= 0 || i.Height <= 0 {
                s.fnLogger("reid !!!!!! Unpack Image From Humantrack Msg Failed")
                continue
            }
 
            sdkInfo := msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)]
            s.fnLogger("reid~~~~~~Recv From Humantrack SDK Result Length: ", len(sdkInfo.Sdkdata))
 
            res := &protomsg.HumanTrackResult{}
            if err := proto.Unmarshal(sdkInfo.Sdkdata, res); err != nil {
                s.fnLogger("reid !!!!!! proto.Unmarshal SDK Result From Humantrack msg Error:", err)
                continue
            }
 
            for _, v := range res.Result {
 
                var clen C.int
                l, t, r, b := C.int(v.RcHuman.Left), C.int(v.RcHuman.Top), C.int(v.RcHuman.Right), C.int(v.RcHuman.Bottom)
                cutImg := C.crop_image(unsafe.Pointer(&i.Data[0]), C.int(i.Width), C.int(i.Height), l, t, r, b, 3, &clen)
                if cutImg != nil {
                    dl := int(clen)
                    data := (*[1 << 26]byte)((*[1 << 26]byte)(cutImg))[:dl:dl]
 
                    w, h := int(r-l+1), int(b-t+1)
                    v.Feature = s.handle.Extract2(unsafe.Pointer(&data[0]), w, h, 3)
                    C.free(cutImg)
                }
 
            }
 
            if len(res.Result) > 0 {
                if out, err := proto.Marshal(res); err == nil {
                    msg.Tasklab.Sdkinfos[int(msg.Tasklab.Index)].Sdkdata = out
                    s.fnLogger("reid~~~~~~Send To Humantrack Result Length:", len(out))
                }
            }
 
            if data, err := proto.Marshal(&msg); err == nil {
                if data == nil {
                    s.fnLogger("reid !!!!!! proto.Marshal Failed To Marshal proto.SdkMessage")
                    continue
                }
                s.fnLogger("reid~~~~~~MSG Send Back To Humantrack Length:", len(data))
 
                chSnd <- data
            } else {
                s.fnLogger("reid !!!!!! proto.Marshal Out Msg Error:", err)
            }
        default:
 
            time.Sleep(10 * time.Millisecond)
 
        }
 
    }
}