mirror of
https://github.com/Kagami/go-face.git
synced 2025-09-27 03:55:51 +08:00
Put shapes to go struct
This commit is contained in:
15
face.go
15
face.go
@@ -16,9 +16,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
shapeLen = 2
|
|
||||||
rectLen = 4
|
rectLen = 4
|
||||||
descrLen = 128
|
descrLen = 128
|
||||||
|
shapeLen = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Recognizer creates face descriptors for provided images and
|
// A Recognizer creates face descriptors for provided images and
|
||||||
@@ -90,7 +90,6 @@ func (rec *Recognizer) recognize(type_ int, imgData []byte, maxFaces int) (faces
|
|||||||
cImgData := (*C.uint8_t)(&imgData[0])
|
cImgData := (*C.uint8_t)(&imgData[0])
|
||||||
cLen := C.int(len(imgData))
|
cLen := C.int(len(imgData))
|
||||||
cMaxFaces := C.int(maxFaces)
|
cMaxFaces := C.int(maxFaces)
|
||||||
|
|
||||||
cType := C.int(type_)
|
cType := C.int(type_)
|
||||||
|
|
||||||
ret := C.facerec_recognize(rec.ptr, cImgData, cLen, cMaxFaces, cType)
|
ret := C.facerec_recognize(rec.ptr, cImgData, cLen, cMaxFaces, cType)
|
||||||
@@ -102,13 +101,14 @@ func (rec *Recognizer) recognize(type_ int, imgData []byte, maxFaces int) (faces
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// No faces.
|
|
||||||
numFaces := int(ret.num_faces)
|
numFaces := int(ret.num_faces)
|
||||||
if numFaces == 0 {
|
if numFaces == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
numShapes := int(ret.num_shapes)
|
||||||
|
|
||||||
// Copy faces data to Go structure.
|
// Copy faces data to Go structure.
|
||||||
|
defer C.free(unsafe.Pointer(ret.shapes))
|
||||||
defer C.free(unsafe.Pointer(ret.rectangles))
|
defer C.free(unsafe.Pointer(ret.rectangles))
|
||||||
defer C.free(unsafe.Pointer(ret.descriptors))
|
defer C.free(unsafe.Pointer(ret.descriptors))
|
||||||
|
|
||||||
@@ -120,6 +120,10 @@ func (rec *Recognizer) recognize(type_ int, imgData []byte, maxFaces int) (faces
|
|||||||
dDataPtr := unsafe.Pointer(ret.descriptors)
|
dDataPtr := unsafe.Pointer(ret.descriptors)
|
||||||
dData := (*[1 << 30]float32)(dDataPtr)[:dDataLen:dDataLen]
|
dData := (*[1 << 30]float32)(dDataPtr)[:dDataLen:dDataLen]
|
||||||
|
|
||||||
|
sDataLen := numFaces * numShapes * shapeLen
|
||||||
|
sDataPtr := unsafe.Pointer(ret.shapes)
|
||||||
|
sData := (*[1 << 30]C.long)(sDataPtr)[:sDataLen:sDataLen]
|
||||||
|
|
||||||
for i := 0; i < numFaces; i++ {
|
for i := 0; i < numFaces; i++ {
|
||||||
face := Face{}
|
face := Face{}
|
||||||
x0 := int(rData[i*rectLen])
|
x0 := int(rData[i*rectLen])
|
||||||
@@ -128,6 +132,11 @@ func (rec *Recognizer) recognize(type_ int, imgData []byte, maxFaces int) (faces
|
|||||||
y1 := int(rData[i*rectLen+3])
|
y1 := int(rData[i*rectLen+3])
|
||||||
face.Rectangle = image.Rect(x0, y0, x1, y1)
|
face.Rectangle = image.Rect(x0, y0, x1, y1)
|
||||||
copy(face.Descriptor[:], dData[i*descrLen:(i+1)*descrLen])
|
copy(face.Descriptor[:], dData[i*descrLen:(i+1)*descrLen])
|
||||||
|
for j := 0; j < numShapes; j++ {
|
||||||
|
shapeX := int(sData[(i*numShapes+j)*shapeLen])
|
||||||
|
shapeY := int(sData[(i*numShapes+j)*shapeLen+1])
|
||||||
|
face.Shapes = append(face.Shapes, image.Point{shapeX, shapeY})
|
||||||
|
}
|
||||||
faces = append(faces, face)
|
faces = append(faces, face)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
14
facerec.cc
14
facerec.cc
@@ -45,9 +45,9 @@ using anet_type = loss_metric<fc_no_bias<128,avg_pool_everything<
|
|||||||
input_rgb_image_sized<150>
|
input_rgb_image_sized<150>
|
||||||
>>>>>>>>>>>>;
|
>>>>>>>>>>>>;
|
||||||
|
|
||||||
static const size_t SHAPE_LEN = 2;
|
|
||||||
static const size_t RECT_LEN = 4;
|
static const size_t RECT_LEN = 4;
|
||||||
static const size_t DESCR_LEN = 128;
|
static const size_t DESCR_LEN = 128;
|
||||||
|
static const size_t SHAPE_LEN = 2;
|
||||||
static const size_t RECT_SIZE = RECT_LEN * sizeof(long);
|
static const size_t RECT_SIZE = RECT_LEN * sizeof(long);
|
||||||
static const size_t DESCR_SIZE = DESCR_LEN * sizeof(float);
|
static const size_t DESCR_SIZE = DESCR_LEN * sizeof(float);
|
||||||
static const size_t SHAPE_SIZE = SHAPE_LEN * sizeof(long);
|
static const size_t SHAPE_SIZE = SHAPE_LEN * sizeof(long);
|
||||||
@@ -209,13 +209,13 @@ faceret* facerec_recognize(facerec* rec, const uint8_t* img_data, int len, int m
|
|||||||
}
|
}
|
||||||
ret->num_shapes = shapes[0].num_parts();
|
ret->num_shapes = shapes[0].num_parts();
|
||||||
ret->shapes = (long*)malloc(ret->num_faces * ret->num_shapes * SHAPE_SIZE);
|
ret->shapes = (long*)malloc(ret->num_faces * ret->num_shapes * SHAPE_SIZE);
|
||||||
for (int i = 0; i < ret->num_faces; i++) {
|
for (int i = 0; i < ret->num_faces; i++) {
|
||||||
long* dst = ret->shapes + i * ret->num_shapes * SHAPE_LEN;
|
long* dst = ret->shapes + i * ret->num_shapes * SHAPE_LEN;
|
||||||
auto shape = shapes[i];
|
const auto& shape = shapes[i];
|
||||||
for (long unsigned int j = 0;j < shape.num_parts(); j++) {
|
for (int j = 0; j < ret->num_shapes; j++) {
|
||||||
dst[j * SHAPE_LEN] = shape.part(j).x();
|
dst[j*SHAPE_LEN] = shape.part(j).x();
|
||||||
dst[(j * SHAPE_LEN) + 1] = shape.part(j).y();
|
dst[j*SHAPE_LEN+1] = shape.part(j).y();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user