panlei
2019-06-22 847983cd82cff288e5b610d2bbcebbdb0398f1d6
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
package main
 
import (
    "fmt"
    "ruleprocess/ruleserver"
    "ruleprocess/insertdata"
 
    "sync"
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/deliver.git"
    "github.com/golang/protobuf/proto"
)
 
func main() {
    wg := sync.WaitGroup{}
    wg.Add(1)
    go ruleserver.TimeTicker()
    nReciever("ipc:///tmp/sdk-2-rules-process.ipc", deliver.PushPull, 1)
    wg.Wait()
}
func nReciever(url string, m deliver.Mode, count int) {
    c := deliver.NewServer(m, url)
    nRecvImpl(c, 1)
}
 
func nRecvImpl(c deliver.Deliver, index int) {
 
    var msg []byte
    var err error
 
    for {
        select {
        // case <-ctx.Done():
        //     return
        default:
            msg, err = c.Recv()
            if err != nil {
                fmt.Println("recv error : ", err)
                continue
            } else {
                arg := ruleserver.ArgsFromSdk{}
                m := paramFormat(msg,&arg)
                fmt.Println("解析出来的数据:",arg)
                ruleserver.MainJudge(&arg)
                // 把arg里的打的标签拿出来给m再封装一层
                resultMag := ruleserver.ResultMsg{SdkMessage: m, RuleResult: arg.RuleResult}
                //fmt.Println("打完标签后的结果:",resultMag)
 
                // 将打完标签的数据插入到ES
                insertdata.InsertToEs(resultMag)
            }
        }
    }
}
 
// 将外部传进来的sdk数据包解成 ArgsFromSdk
func paramFormat(msg []byte,arg *ruleserver.ArgsFromSdk) protomsg.SdkMessage {
    defer func() {
        if err := recover();err != nil{
            fmt.Println("解包过程的错误",err.(string))
        }
 
    }()
    // 反序列化数据得到sdk入参
    m := protomsg.SdkMessage{}
    err := proto.Unmarshal(msg, &m)
    if err != nil {
        panic("解析msg时出现错误")
    }
    arg.CameraId = m.Cid
    arg.TaskId = m.Tasklab.Taskid
    bdata,err := UnCompress(m.Data)
    if err != nil {
        panic("解压缩图片时出现错误")
    }
    i := protomsg.Image{}
    err = proto.Unmarshal(bdata, &i)
    arg.ImageWidth = int(i.Width)
    arg.ImageHeight = int(i.Height)
    // 暂时写死,sdk还没有这俩算法
    arg.KeepRight = false
    arg.IsStatic = false
    fmt.Println("从mongos中拿到的数据包长度为:", len(msg))
    for _, sdkinfo := range m.Tasklab.Sdkinfos { // yolo算法
        if sdkinfo.Sdktype == "Yolo" {
            yoloParam := protomsg.ParamYoloObj{}
            err = proto.Unmarshal(sdkinfo.Sdkdata, &yoloParam)
            if err != nil {
                fmt.Println("解析FACE sdk数据时出现错误", err)
                continue
            }
            for _, info := range yoloParam.Infos {
                photoMap := ruleserver.PhotoMap{Rects:rectFormat(info.RcObj, i.Width, i.Height), Score: float64(info.Prob)}
                arg.Photo = append(arg.Photo, photoMap)
            }
 
        }
        if sdkinfo.Sdktype == "FaceDetect" { // 人脸检测
            faceParam := protomsg.ParamFacePos{}
            err = proto.Unmarshal(sdkinfo.Sdkdata, &faceParam)
            if err != nil {
                fmt.Println("解析YOLO sdk数据时出现错误", err)
                continue
            }
            for _, info := range faceParam.Faces {
                photoMap := ruleserver.PhotoMap{Rects:ruleserver.Rect{}, Score: float64(info.Pos.Quality)}
                arg.Photo = append(arg.Photo, photoMap)
            }
        }
        //if sdkinfo.Sdktype == "FaceExtract" { // 人脸提取
 
        //}
 
    }
    return m
}
 
// 将外部传进来的rect(top,bottom,left,right)转化为自己内部的rect(left top width height)
func rectFormat(rcobj *protomsg.Rect, width int32, height int32) ruleserver.Rect {
    rect := ruleserver.Rect{}
    rect.X = float64(rcobj.Left)
    rect.Y = float64(rcobj.Top)
    rect.Width = float64(width - rcobj.Left - rcobj.Right)
    rect.Height = float64(height - rcobj.Top - rcobj.Bottom)
    return rect
}