From 0b932b8af021e1f97a27c85d823f828fbf854a39 Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期四, 06 六月 2024 16:20:13 +0800 Subject: [PATCH] 完善人脸照片图片切图 --- /dev/null | 8 -------- config/config.go | 12 +++++++----- repository/captureRepo.go | 23 +++++++++-------------- config/gat1400.yaml | 8 ++++++++ 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/config/config.go b/config/config.go index f5e1c8b..cebb37e 100644 --- a/config/config.go +++ b/config/config.go @@ -47,14 +47,12 @@ ReportServer string `mapstructure:"report-server"` ReportInterval int `mapstructure:"report-interval"` RetryInterval int `mapstructure:"retry-interval"` - - Enable bool `mapstructure:"enable"` + Enable bool `mapstructure:"enable"` } type image struct { - CutFaceImage bool `mapstructure:"cut-face-image"` - OriginWidth float64 `mapstructure:"origin-width"` - OriginHeight float64 `mapstructure:"origin-height"` + CutFaceImage bool `mapstructure:"cut-face-image"` + Enlarge int `mapstructure:"enlarge"` } // 姊帶璁惧 @@ -128,5 +126,9 @@ ClientConf.Proto = "http" } + if ImageConf.Enlarge == 0 { + ImageConf.Enlarge = 100 + } + logger.SetLogLevel(LogConf.Level) } diff --git a/config/gat1400.yaml b/config/gat1400.yaml index a6ac000..f6d1dc6 100644 --- a/config/gat1400.yaml +++ b/config/gat1400.yaml @@ -9,6 +9,7 @@ password: "Aa123456" realm: "Basic Realm" role: "cascade" + client: enable: true device-id: "12312312315031200003" @@ -21,6 +22,8 @@ channel-number: "12312312315031200003" heartbeat-interval: 30 heartbeat-count: 3 + add-floor-faceId: false + nvcs: model: "" port : "" @@ -35,6 +38,11 @@ sync-server: "http://192.168.20.119:9696/api-a/device/alarm" report-server: "http://192.168.20.119:8007/data/api-v/device/inputData" report-interval: 30 + retry-interval: 10 + +image: + cut-face-image: false + enlarge: 100 # 浠ょ墝妗堕檺娴侀厤缃� rate-limit: diff --git a/repository/captureRepo.go b/repository/captureRepo.go index 80fb1e8..559a6b7 100644 --- a/repository/captureRepo.go +++ b/repository/captureRepo.go @@ -3,12 +3,12 @@ import ( "encoding/base64" "encoding/json" - "gat1400Exchange/pkg" "time" "gat1400Exchange/client" "gat1400Exchange/config" "gat1400Exchange/models" + "gat1400Exchange/pkg" "gat1400Exchange/pkg/logger" "gat1400Exchange/util" "gat1400Exchange/vo" @@ -37,7 +37,6 @@ var deviceId = face.DeviceID var faceId = face.FaceID var bgImageStr string - var bgImageWidth, bgImageHeight int var bgImageBytes, faceImageBytes []byte = nil, nil // 鑾峰彇澶у浘, 鐩墠娴峰悍鐨勫皬鍥惧垎杈ㄧ巼澶綆 @@ -46,8 +45,6 @@ continue } - bgImageWidth = image.Width - bgImageHeight = image.Height if len(image.Data) > 0 { if len(image.Data) > len(bgImageStr) { bgImageStr = image.Data @@ -72,17 +69,15 @@ } // 鍒ゆ柇鍥剧墖绫诲瀷鏄惁涓哄満鏅浘, 鏍规嵁浜鸿劯鍧愭爣鍒囧皬鍥�. - if config.ImageConf.CutFaceImage { - scaleX := float64(bgImageWidth) / config.ImageConf.OriginWidth - scaleY := float64(bgImageHeight) / config.ImageConf.OriginHeight - faceRect := &vo.Rect{ - Left: int(float64(face.LeftTopX) * scaleX), - Top: int(float64(face.LeftTopY) * scaleY), - Right: int(float64(face.RightBtmX) * scaleX), - Bottom: int(float64(face.RightBtmY) * scaleY), + if config.ImageConf.CutFaceImage && face.LeftTopX != 0 { + faceRect := &pkg.Rect{ + Left: face.LeftTopX, + Top: face.LeftTopY, + Right: face.RightBtmX, + Bottom: face.RightBtmY, } - faceImageBytes, err = util.SubCutImg(bgImageBytes, faceRect, 90) + faceImageBytes, err = pkg.SubCutImage(bgImageBytes, faceRect, config.ImageConf.Enlarge) if err != nil { logger.Warn("Cut face image failure, %s", err.Error()) } @@ -118,7 +113,7 @@ c.CacheData(cacheItem, "basic") logger.Warn("The data forwarding failed, adding to local cache.") } else { - logger.Debug("The data forwarding successful. deviceId:%s", deviceId) + logger.Debug("The data forwarding successful. deviceId:%s, picId:", deviceId, face.FaceID) } } } diff --git a/util/imageTool.go b/util/imageTool.go deleted file mode 100644 index 3f4dc4a..0000000 --- a/util/imageTool.go +++ /dev/null @@ -1,177 +0,0 @@ -package util - -import ( - "bytes" - "image" - "time" - - "gat1400Exchange/vo" - - "basic.com/pubsub/protomsg.git" - "basic.com/valib/godraw.git" -) - -// 鎸夊昂瀵稿幓鍒囧浘 -func SubCutImg(srcImage []byte, rect *vo.Rect, enlarge int) ([]byte, error) { - i, err := readFromUploadImg(srcImage) - - bkImg, _ := godraw.ToImage(srcImage, int(i.Width), int(i.Height)) - newRect := EnlargeCut(rect, int(i.Width), int(i.Height), enlarge) - squareRect := LetterBox(newRect, int(i.Width), int(i.Height)) - rectNew := image.Rect(squareRect.Left, squareRect.Top, squareRect.Right, squareRect.Bottom) - subImg := bkImg.(*image.RGBA).SubImage(rectNew) - - bytes, err := godraw.ImageToJpeg(subImg, nil) - if err != nil { - return nil, err - } - - return bytes, nil -} - -// letterbox 鎸夌収瀹借竟淇敼鐩爣妗嗕负姝f柟褰� -func LetterBox(rect *vo.Rect, maxW, maxH int) *vo.Rect { - width := rect.Right - rect.Left - height := rect.Bottom - rect.Top - - // 璁$畻鐩爣妗嗙殑涓績鐐瑰潗鏍� - centerX := (rect.Left + rect.Right) / 2 - centerY := (rect.Top + rect.Bottom) / 2 - - // 濡傛灉瀹藉害澶т簬楂樺害锛屽垯浠ラ珮搴︿负鍩哄噯璋冩暣瀹藉害锛屼娇鍏舵垚涓烘鏂瑰舰 - if width > height { - newWidth := height - newLeft := centerX - newWidth/2 - newRight := centerX + newWidth/2 - - // 璋冩暣鍚庣殑姝f柟褰㈡瓒呭嚭鍥惧儚杈圭晫鏃剁殑澶勭悊 - if newLeft < 0 { - // 濡傛灉宸﹁竟鐣岃秴鍑猴紝鍚戝彸绉诲姩鍙宠竟鐣� - newRight += -newLeft - newLeft = 0 - } - if newRight > maxW { - // 濡傛灉鍙宠竟鐣岃秴鍑猴紝鍚戝乏绉诲姩宸﹁竟鐣� - newLeft -= newRight - maxW - newRight = maxW - } - - return &vo.Rect{ - Left: newLeft, - Top: rect.Top, - Right: newRight, - Bottom: rect.Bottom, - } - } - - // 濡傛灉楂樺害澶т簬瀹藉害锛屽垯浠ュ搴︿负鍩哄噯璋冩暣楂樺害锛屼娇鍏舵垚涓烘鏂瑰舰 - if height > width { - newHeight := width - newTop := centerY - newHeight/2 - newBottom := centerY + newHeight/2 - - // 璋冩暣鍚庣殑姝f柟褰㈡瓒呭嚭鍥惧儚杈圭晫鏃剁殑澶勭悊 - if newTop < 0 { - // 濡傛灉涓婅竟鐣岃秴鍑猴紝鍚戜笅绉诲姩涓嬭竟鐣� - newBottom += -newTop - newTop = 0 - } - if newBottom > maxH { - // 濡傛灉涓嬭竟鐣岃秴鍑猴紝鍚戜笂绉诲姩涓婅竟鐣� - newTop -= newBottom - maxH - newBottom = maxH - } - - return &vo.Rect{ - Left: rect.Left, - Top: newTop, - Right: rect.Right, - Bottom: newBottom, - } - } - - // 濡傛灉瀹藉害鍜岄珮搴︾浉绛夛紝鍒欏凡缁忔槸姝f柟褰紝鐩存帴杩斿洖鍘熷鐭╁舰 - return rect -} - -// 闀垮鍙樹负涓�姣斾竴锛屾瘡杈瑰悇鎵╃櫨鍒嗕箣... -func EnlargeCut(rect *vo.Rect, maxW, maxH int, enlargePer int) *vo.Rect { - var x0New, x1New, y0New, y1New int - - // 鍏堟妸闀垮鍙樹负涓�姣斾竴 - x0 := rect.Left - y0 := rect.Top - x1 := rect.Right - y1 := rect.Bottom - - chaZhi := (y1 - y0) - (x1 - x0) - if chaZhi > 0 { - x0 = x0 - chaZhi/2 - if x0 < 0 { - x0 = 0 - } - x1 = x1 + chaZhi/2 - if x1 > maxW { - x1 = maxW - } - } else { - y0 = y0 + chaZhi/2 - if y0 < 0 { - y0 = 0 - } - y1 = y1 - chaZhi/2 - if y1 > maxH { - y1 = maxH - } - } - - // 鍐嶆妸姣忚竟鍚勬墿澶х櫨鍒嗕箣... - enlarge := float32(enlargePer) / 100 - x0New = int((1+enlarge)*float32(x0) - enlarge*float32(x1)) - if x0New < 0 { - x0New = 0 - } - - x1New = int((1+enlarge)*float32(x1) - enlarge*float32(x0)) - if x1New > maxW { - x1New = maxW - } - - y0New = int((1+enlarge)*float32(y0) - enlarge*float32(y1)) - if y0New < 0 { - y0New = 0 - } - - y1New = int((1+enlarge)*float32(y1) - enlarge*float32(y0)) - if y1New > maxH { - y1New = maxH - } - - return &vo.Rect{ - Left: x0New, - Top: y0New, - Right: y0New, - Bottom: y1New, - } -} - -func readFromUploadImg(imageFile []byte) (*protomsg.Image, error) { - bt := bytes.NewBuffer(imageFile) - img, _, err := image.Decode(bt) - if err != nil { - return nil, err - } - bgr := godraw.Image2BGR(img) - - timeUnix := time.Now().Unix() - formatTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05") - - return &protomsg.Image{ - Width: int32(img.Bounds().Dx()), - Height: int32(img.Bounds().Dy()), - Timestamp: formatTimeStr, - Data: bgr, - Id: time.Now().Unix(), - Cid: "gat1400", - }, nil -} diff --git a/vo/common.go b/vo/common.go deleted file mode 100644 index 87d8cfb..0000000 --- a/vo/common.go +++ /dev/null @@ -1,8 +0,0 @@ -package vo - -type Rect struct { - Left int - Top int - Right int - Bottom int -} -- Gitblit v1.8.0