liuxiaolong
2019-06-27 31a9d64a0cc99011263c4516dfaac5c9879a0c45
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
package service
 
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/deliver.git"
    "encoding/base64"
    "fmt"
    "github.com/gogo/protobuf/proto"
    "github.com/pierrec/lz4"
    "github.com/satori/go.uuid"
    "gocv.io/x/gocv"
    "time"
)
 
const (
    Ipc_Push_Ext = "_2.ipc"
    Ipc_Pull_Ext = "_1.ipc"
    Ipc_Url_Pre = "ipc:///tmp///"
    Virtual_FaceTaskId = "92496BDF-2BFA-98F2-62E8-96DD9866ABD2"
    Virtual_FaceSdkId = "virtual-faceextract-sdk-pull"
    Url_Service_PUSH = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Push_Ext
    Url_Service_PULL = Ipc_Url_Pre + Virtual_FaceSdkId + Ipc_Pull_Ext
)
var imgPushChan chan protomsg.Recvmsg
var client_push deliver.Deliver
var client_pull deliver.Deliver
 
func TestPushImgMsg() {
    InitService()
 
    i := readImgFile()
    if b, err := proto.Marshal(&i);err !=nil{
        fmt.Println("protoImage marshal err")
        return
    } else {
        bc := make([]byte,len(b))
        ht := make([]int, 64<<10)
        n,err := lz4.CompressBlock(b,bc,ht)
        if err !=nil {
            fmt.Println(err)
        }
        if n >= len(b){
            fmt.Println("image is not compressible")
        }
        bc = bc[:n]
        for {
            PushImgMsg(protomsg.Recvmsg{
                Id:uuid.NewV4().String(),
                Addr:"",
                Picdata:bc,
            })
            fmt.Println("pushed img")
            time.Sleep(5*time.Second)
        }
 
    }
}
 
func readImgFile() protomsg.Image{
    var i protomsg.Image
    timeUnix := time.Now().Unix()
    formatTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05")
    filePath := "/home/user/workspace/timg.jpg"
 
    picMat := gocv.IMRead(filePath, gocv.IMReadColor)
 
    defer picMat.Close()
 
    if picMat.Empty() {
        fmt.Println("file not exist")
        return i
    }
    width := int32(picMat.Rows())
    height := int32(picMat.Cols())
    data := picMat.DataPtrUint8()
    fmt.Printf("width:%d,height:%d,data.length:%d,timestamp:%s",width,height,len(data),formatTimeStr)
    i = protomsg.Image{
        Width: width,
        Height: height,
        Timestamp: formatTimeStr,
        Data: data,
        Id: timeUnix,
    }
    fmt.Println("gocv read img completed")
    return i
}
 
func PushImgMsg(is protomsg.Recvmsg){
    imgPushChan <- is
}
 
var resultMap map[string]protomsg.SdkMessage
 
 
func InitService(){
    fmt.Println("service init!")
    imgPushChan = make(chan protomsg.Recvmsg)
    resultMap = make(map[string]protomsg.SdkMessage,0)
    client_push = deliver.NewClient(deliver.PushPull, Url_Service_PUSH)
    client_pull = deliver.NewClient(deliver.PushPull, Url_Service_PULL)
    defer func() {
        client_push.Close()
        client_pull.Close()
    }()
    go thSend()
 
    go thRecv()
}
 
func thSend(){
    for {
        select {
        case is := <- imgPushChan:
            fmt.Println("imgPushChan in")
            b, _ := proto.Marshal(&is)
            err := client_push.Send(b)
            if err !=nil {
                fmt.Println("img Send err:",err)
            }
        default:
            //fmt.Println("no img in")
        }
    }
}
 
func thRecv(){
    for {
        resultBytes, err := client_pull.Recv()
        if err !=nil{
            fmt.Println("pull err:",err)
            continue
        }
        rMsg := protomsg.SdkMessage{}
        if err := proto.Unmarshal(resultBytes, &rMsg);err ==nil{
            fmt.Println("received MSG:",rMsg.Cid)
            perId := rMsg.Cid //数据id
            if rMsg.Tasklab !=nil && rMsg.Tasklab.Taskid == Virtual_FaceTaskId {
                sdkInfos := rMsg.Tasklab.Sdkinfos
                fmt.Println("Len(sdkInfos)=",len(sdkInfos))
                    for _,swt :=range sdkInfos{
                        if swt.Sdktype == "FaceExtract"{
                            fmt.Println("sdkName:",swt.SdkName)
                            fmt.Println("sdkData.len:",len(swt.Sdkdata))
                            var pff protomsg.ParamFaceFeature
                            if err := proto.Unmarshal(swt.Sdkdata, &pff);err !=nil{
                                fmt.Println("ParamFaceFeature unmarshal err:",err)
                            } else {
                                fmt.Println("目标数:",len(pff.ExtComp))
                                for _,fea :=range pff.ExtComp{
                                    base64Fea := base64.StdEncoding.EncodeToString(fea.Feats)
                                    fmt.Println("perId:",perId)
                                    fmt.Println("faceFeature:",base64Fea)
                                }
                            }
                            break
                        }
                    }
 
            }
            //resultMap[rMsg.Cid] = rMsg
        } else {
            fmt.Println("recv msg Err:",err)
        }
 
    }
}