reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-11 bdf3ad71583fb4ef100d3819ecdae8fd9f70083e
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
package main
 
import (
    "context"
    "unsafe"
 
    "basic.com/libgowrapper/sdkhelper.git"
    "basic.com/valib/gogpu.git"
 
    "basic.com/pubsub/protomsg.git"
 
    "basic.com/valib/deliver.git"
    "github.com/gogo/protobuf/proto"
)
 
type reid struct {
    handle   *ReID
    fnLogger func(...interface{})
 
    ipc string
}
 
// Create Reid
func Create(config string, typ, id string, gpu int, shm bool, ipc2Rule string, ruleMaxSize int, fn func(...interface{}), reserved map[string]interface{}) interface{} {
 
    cfg, err := sdkhelper.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 := sdkhelper.Atoi(cfg.Param[sGPU])
 
    rGPU := gpu
 
    if rGPU == -1 {
        rGPU = gogpu.ValidGPU(gpuM + 512)
    }
    if rGPU == -1 {
        fn("Reid SDK Create Error When Find GPU")
        return nil
    }
 
    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,
 
        ipc: cfg.Param[sIPC],
    }
}
 
// Run run
func Run(ctx context.Context, i interface{}) {
    s := i.(*reid)
 
    c := deliver.NewClient(deliver.ReqRep, s.ipc)
 
    var msg []byte
    var err error
 
    for {
        select {
        case <-ctx.Done():
            return
        default:
 
            msg, err = c.Recv()
            if err != nil {
                continue
            }
 
            i := &protomsg.Image{}
            err := proto.Unmarshal(msg, i)
            if err != nil {
                s.fnLogger("REID~~~~~~protobuf decode CameraImage error: ", err)
                continue
            }
            if i.Data == nil {
                s.fnLogger("REID~~~~~~protomsg.Image data null")
                continue
            }
            s.fnLogger("REID~~~~~~Recv Image:", len(i.Data))
 
            feat := s.handle.Extract2(unsafe.Pointer(&i.Data[0]), int(i.Width), int(i.Height), 3)
            if feat == nil {
                feat = make([]float32, 1)
            } else {
                for k := 0; k < 3; k++ {
                    s.fnLogger("REID~~~~~~extractor---human_feats------%f", feat[k+2000])
                }
            }
            buf := float32SliceAsByteSlice(feat)
            c.Send(buf)
        }
 
    }
}
 
func float32SliceAsByteSlice(src []float32) []byte {
    if len(src) == 0 {
        return nil
    }
 
    l := len(src) * 4
    ptr := unsafe.Pointer(&src[0])
    // It is important to keep in mind that the Go garbage collector
    // will not interact with this data, and that if src if freed,
    // the behavior of any Go code using the slice is nondeterministic.
    // Reference: https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
    return (*[1 << 26]byte)((*[1 << 26]byte)(ptr))[:l:l]
}