panlei
2019-07-02 54f7bb20860ec5a98964290c721113d71ea6e408
上传人脸
3个文件已修改
56 ■■■■ 已修改文件
insertdata/insertDataToEs.go 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
util/image.go 43 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
util/upload.go 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
insertdata/insertDataToEs.go
@@ -126,9 +126,8 @@
                }
                i := protomsg.Image{}
                err = proto.Unmarshal(bdata, &i)
                i1 := protomsg.Image{}
                i1 = util.Subimg(i.Data, int(face.Location.X), int(face.Location.Y), int(face.Location.X+face.Location.Width), int(face.Location.Y+face.Location.Height))
                resp, err := util.PostFormBufferData(weedfsUrl, i1, uuid.NewV4().String())
                bytes := util.Subimg(i.Data, int(face.Location.X), int(face.Location.Y), int(face.Location.X+face.Location.Width), int(face.Location.Y+face.Location.Height))
                resp, err := util.PostFormBufferData1(weedfsUrl, bytes, uuid.NewV4().String())
                if err != nil {
                    log.Println("上传小图出错")
                }
util/image.go
@@ -1,33 +1,32 @@
package util
import (
    "basic.com/pubsub/protomsg.git"
    "bytes"
    "fmt"
    "github.com/gogo/protobuf/proto"
    "gocv.io/x/gocv"
    "image"
    "image/jpeg"
    "log"
    "reflect"
)
// 按尺寸去切图
func Subimg(dbyte []byte,x0,y0,x1,y1 int,) protomsg.Image{
func Subimg(dbyte []byte,x0,y0,x1,y1 int,) []byte{
    bbb := bytes.NewBuffer(dbyte)                           // 必须加一个buffer 不然没有read方法就会报错
    log.Println("==================看看这个buffer",bbb)
    m, _, _ := image.Decode(bbb)                          // 图片文件解码
    rgbImg := m.(*image.YCbCr)
    subImg := rgbImg.SubImage(image.Rect(x0, y0, x1, y1)) //图片裁剪x0 y0 x1 y1
    fmt.Println(reflect.TypeOf(subImg))
    //f, _ := os.Create("./test.jpg")                   //创建文件
    //defer f.Close()                                         //关闭文件
    emptyBuff := bytes.NewBuffer(nil)                 //开辟一个新的空buff
    jpeg.Encode(emptyBuff, subImg, nil)                //img写入到buff
    bytes := emptyBuff.Bytes()
    i := protomsg.Image{}
    proto.Unmarshal(bytes,&i)
    return i
    //bbb := bytes.NewBuffer(dbyte)                           // 必须加一个buffer 不然没有read方法就会报错
    ////log.Println("==================看看这个buffer",bbb)
    //m, _, _ := image.Decode(bbb)                          // 图片文件解码
    //rgbImg := m.(*image.YCbCr)
    //subImg := rgbImg.SubImage(image.Rect(x0, y0, x1, y1)) //图片裁剪x0 y0 x1 y1
    //fmt.Println(reflect.TypeOf(subImg))
    ////f, _ := os.Create("./test.jpg")                   //创建文件
    ////defer f.Close()                                         //关闭文件
    //emptyBuff := bytes.NewBuffer(nil)                 //开辟一个新的空buff
    //jpeg.Encode(emptyBuff, subImg, nil)                //img写入到buff
    //bytes := emptyBuff.Bytes()
    //i := protomsg.Image{}
    //proto.Unmarshal(bytes,&i)
    //return i
    //f, _ := os.Create("./test.jpg")                   //创建文件
    //defer f.Close()                                         //关闭文件
    //jpeg.Encode(f, subImg, nil)                         //写入文件
    img,_ := gocv.NewMatFromBytes(int(y1-y0),int(x1-x0),gocv.MatTypeCV8UC3,dbyte)
    region := img.Region(image.Rectangle{image.Point{x0,y0},image.Point{x1,y1}})
    bytes, _ := gocv.IMEncode(".jpg", region)
    return bytes
}
util/upload.go
@@ -76,11 +76,11 @@
}
// 上传图片(二进制流)
func PostFormBufferData1(uri string, fbada []byte, fileName string,) (maps map[string]interface{}, err0 error) {
func PostFormBufferData1(uri string, fdata []byte, fileName string,) (maps map[string]interface{}, err0 error) {
    // 要指定转byte的格式
    imgs := gocv.NewMat()
    imgs, _ = gocv.NewMatFromBytes(1000, 1000, gocv.MatTypeCV8UC3, fbada)
    fdata,_ := gocv.IMEncode(".jpg",imgs)
    //imgs := gocv.NewMat()
    //imgs, _ = gocv.NewMatFromBytes(1000, 1000, gocv.MatTypeCV8UC3, fbada)
    //fdata,_ := gocv.IMEncode(".jpg",imgs)
    body := &bytes.Buffer{}
    writer := multipart.NewWriter(body)
    _, err := writer.CreateFormFile("file", fileName)