zhangzengfei
2024-10-17 b4a86051e9bf4888fd5d01c12232a26f3874e03b
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package controller
 
import (
    "encoding/base64"
    "fmt"
    "math/rand"
    "net/http"
    "path"
    "strconv"
    "time"
 
    "gat1400Exchange/config"
    "gat1400Exchange/pkg"
    "gat1400Exchange/pkg/logger"
    "gat1400Exchange/repository"
    "gat1400Exchange/service"
    "gat1400Exchange/util"
    "gat1400Exchange/vo"
 
    "github.com/gin-gonic/gin"
)
 
type CaptureController struct {
    Repository repository.CaptureRepository
}
 
// 构造函数
func NewCaptureController() CaptureController {
    svr := repository.NewCaptureRepository()
    controller := CaptureController{Repository: svr}
 
    return controller
}
 
// 批量提交人脸
func (a CaptureController) Faces(c *gin.Context) {
    var req vo.RequestFaceList
    if err := c.BindJSON(&req); err != nil {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    if len(req.FaceListObject.FaceObject) == 0 {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    face := req.FaceListObject.FaceObject[0]
    logger.Debug("Receive new face message, ip:%s, device id:%s faceId:%s, LeftTopX:%d, appearTime:%s", c.RemoteIP(), face.DeviceID, face.FaceID, face.LeftTopX, face.FaceAppearTime)
 
    if config.ClientConf.UploadType == "url" {
        for idx, img := range face.SubImageList.SubImageInfoObject {
            if img.Type != "14" {
                continue
            }
 
            imageBytes, err := base64.StdEncoding.DecodeString(img.Data)
            if err != nil {
                logger.Warn("Decode Small Image Base64 String failure, %s", err.Error())
                c.AbortWithStatus(http.StatusBadRequest)
                return
            }
 
            today := time.Now().Format("2006-01-02")
            imagePath := path.Join(config.LogConf.Path, "gat1400_face_images", today, img.ImageID+".jpeg")
            err = util.WriteToFile(imagePath, imageBytes)
            if err == nil {
                face.SubImageList.SubImageInfoObject[idx].Data = ""
                face.SubImageList.SubImageInfoObject[idx].StoragePath = config.ImageConf.ImageUriPrefix + "/" + today + "/" + img.ImageID + ".jpeg"
            }
        }
    }
 
    // 如果开启了下级, 身份应该是消息代理, 不再转发到服务器
    if config.ClientConf.Enable && config.ServeConf.Role == "agent" {
        go a.Repository.VIIDFaceMsgForward(&req)
    } else if config.ServeConf.Role == "cascade" {
        go service.AddFaceNotification(&face)
    }
 
    if config.ForwardConf.SyncServer != "" {
        go a.Repository.FaceForward(req.FaceListObject.FaceObject)
    }
 
    if config.ForwardConf.RecordServer != "" {
        go a.Repository.PubRecordMessage(face.DeviceID, face.FaceID)
    }
 
    rspMsg := vo.ResponseStatus{
        RequestURL:   c.FullPath(),
        StatusCode:   vo.StatusSuccess,
        StatusString: vo.StatusString[vo.StatusSuccess],
        Id:           face.FaceID,
        LocalTime:    time.Now().Format("20060102150405"),
    }
 
    c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg})
}
 
func (a CaptureController) VideoLabels(c *gin.Context) {
    var req vo.RequestVideoLabelList
    if err := c.BindJSON(&req); err != nil {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    if len(req.VideoLabelListObject.VideoLabelObject) == 0 {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    videoLabel := req.VideoLabelListObject.VideoLabelObject[0]
    logger.Debug("Receive new videoLabel message, Id:%s Ip:%s ", videoLabel.VideoLabelID, c.RemoteIP())
 
    // 转人员消息
    var person vo.PersonObject
    person.PersonID = videoLabel.VideoLabelID[0:41] + "01" + videoLabel.VideoLabelID[43:48]
    person.InfoKind = "1"
    person.DeviceID = videoLabel.IVADeviceID
    person.SourceID = videoLabel.VideoImageID
    person.LocationMarkTime = videoLabel.BehaviorAnalysisObject.BehaviorBeginTime
    person.PersonAppearTime = videoLabel.BehaviorAnalysisObject.BehaviorBeginTime
    person.PersonDisAppearTime = videoLabel.BehaviorAnalysisObject.BehaviorEndTime
    person.IsDriver = 2
    person.IsCriminalInvolved = 2
 
    var hasTargetImage bool
    var bgImageWith, bgImageHeight int
    var bgImage *vo.SubImageInfoObject
    for idx, img := range videoLabel.SubImageList.SubImageInfoObject {
        videoLabel.SubImageList.SubImageInfoObject[idx].EventSort = 10
 
        // 判断是否有小图, 如果没有切一张
        if img.Type == "14" {
            bgImageWith, bgImageHeight = img.Width, img.Height
        } else {
            hasTargetImage = true
            bgImage = &videoLabel.SubImageList.SubImageInfoObject[idx]
        }
 
        person.SubImageList.SubImageInfoObject = append(
            person.SubImageList.SubImageInfoObject,
            videoLabel.SubImageList.SubImageInfoObject[idx],
        )
    }
 
    if !hasTargetImage && bgImage != nil {
        imgData, err := base64.StdEncoding.DecodeString(bgImage.Data)
        if err != nil {
            c.AbortWithStatus(http.StatusBadRequest)
            return
        }
 
        var subRect pkg.Rect
        subRect.Left = bgImageWith / 3
        subRect.Top = bgImageHeight / 3
        subRect.Right = subRect.Left * 2
        subRect.Bottom = subRect.Top * 2
 
        subImage, err := pkg.SubCutImage(imgData, &subRect, 0)
        if err != nil {
            c.AbortWithStatus(http.StatusBadRequest)
            return
        }
 
        imageId := bgImage.ImageID[37:41]
        imageNumber, err := strconv.Atoi(imageId)
        if err != nil {
            imageNumber = rand.Intn(90000) + 10000
        } else {
            imageNumber++
        }
        var subImageInfo = vo.SubImageInfoObject{
            ImageID:     fmt.Sprintf("%s%d", bgImage.ImageID[0:37], imageNumber),
            EventSort:   10,
            DeviceID:    bgImage.DeviceID,
            StoragePath: "",
            Type:        "11",
            FileFormat:  "Jpeg",
            ShotTime:    bgImage.ShotTime,
            Width:       subRect.Right - subRect.Left,
            Height:      subRect.Top - subRect.Bottom,
            Data:        base64.StdEncoding.EncodeToString(subImage),
        }
        person.SubImageList.SubImageInfoObject = append(
            person.SubImageList.SubImageInfoObject,
            subImageInfo,
        )
    }
 
    // 如果开启了下级, 身份应该是消息代理, 不再转发到服务器
    if config.ClientConf.Enable && config.ServeConf.Role == "agent" {
        var personList vo.RequestPersonList
        personList.PersonListObject.PersonObject = append(personList.PersonListObject.PersonObject, person)
        go a.Repository.VIIDPersonMsgForward(&personList)
    } else if config.ServeConf.Role == "cascade" {
        go service.AddPersonNotification(&person)
    }
 
    if config.ForwardConf.SyncServer != "" {
        go a.Repository.PersonForward([]vo.PersonObject{person})
    }
 
    rspMsg := vo.ResponseStatus{
        RequestURL:   c.FullPath(),
        StatusCode:   vo.StatusSuccess,
        StatusString: vo.StatusString[vo.StatusSuccess],
        Id:           videoLabel.VideoLabelID,
        LocalTime:    time.Now().Format("20060102150405"),
    }
 
    c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg})
}
 
func (a CaptureController) Persons(c *gin.Context) {
    var req vo.RequestPersonList
    if err := c.BindJSON(&req); err != nil {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    if len(req.PersonListObject.PersonObject) == 0 {
        c.AbortWithStatus(http.StatusBadRequest)
        return
    }
 
    person := req.PersonListObject.PersonObject[0]
    logger.Debug("Receive new person message, ip:%s, device Id:%s personId:%s, appearTime:%s", c.RemoteIP(), person.DeviceID, person.PersonID, person.PersonAppearTime)
 
    // 如果开启了下级, 身份应该是消息代理, 不再转发到服务器
    if config.ClientConf.Enable && config.ServeConf.Role == "agent" {
        go a.Repository.VIIDPersonMsgForward(&req)
    } else if config.ServeConf.Role == "cascade" {
        go service.AddPersonNotification(&person)
    }
 
    if config.ForwardConf.SyncServer != "" {
        go a.Repository.PersonForward(req.PersonListObject.PersonObject)
    }
 
    rspMsg := vo.ResponseStatus{
        RequestURL:   c.FullPath(),
        StatusCode:   vo.StatusSuccess,
        StatusString: vo.StatusString[vo.StatusSuccess],
        Id:           person.PersonID,
        LocalTime:    time.Now().Format("20060102150405"),
    }
 
    c.JSON(http.StatusOK, gin.H{"ResponseStatusObject": rspMsg})
}