Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Danile71 committed Feb 12, 2020
1 parent 3caf75f commit d65b255
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
32 changes: 25 additions & 7 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,38 @@ import (

// Path to directory with models and test images. Here it's assumed it
// points to the <https://github.com/Kagami/go-face-testdata> clone.
const dataDir = "testdata"
const cnn = "testdata/mmod_human_face_detector.dat"
const shape = "testdata/shape_predictor_68_face_landmarks.dat"
const descr = "testdata/dlib_face_recognition_resnet_model_v1.dat"
const gender = "testdata/dnn_gender_classifier_v1.dat"
const age = "testdata/dnn_age_predictor_v1.dat"

// This example shows the basic usage of the package: create an
// recognizer, recognize faces, classify them using few known ones.
func Example_basic() {
// Init the recognizer.
rec, err := face.NewRecognizer(dataDir)
rec, err := face.NewRecognizer()
if err != nil {
log.Fatalf("Can't init face recognizer: %v", err)
}
// Free the resources when you're finished.
defer rec.Close()

rec.SetCNNModel(cnn)
rec.SetDescriptorModel(descr)
rec.SetShapeModel(shape)
rec.SetGenderModel(gender)
rec.SetAgeModel(age)

rec.SetSize(150)
rec.SetPadding(0.25)
rec.SetMinImageSize(100)
rec.SetJittering(0)

// Test image with 10 faces.
testImagePristin := filepath.Join(dataDir, "pristin.jpg")
// Recognize faces on that image.
faces, err := rec.RecognizeFile(testImagePristin)
faces, err := rec.DetectFromFile(testImagePristin)
if err != nil {
log.Fatalf("Can't recognize: %v", err)
}
Expand All @@ -40,6 +55,7 @@ func Example_basic() {
var samples []face.Descriptor
var cats []int32
for i, f := range faces {
rec.Recognize(&f)
samples = append(samples, f.Descriptor)
// Each face is unique on that image so goes to its own category.
cats = append(cats, int32(i))
Expand All @@ -54,14 +70,16 @@ func Example_basic() {

// Now let's try to classify some not yet known image.
testImageNayoung := filepath.Join(dataDir, "nayoung.jpg")
nayoungFace, err := rec.RecognizeSingleFile(testImageNayoung)
nayoungFace, err := rec.DetectFromFile(testImageNayoung)
if err != nil {
log.Fatalf("Can't recognize: %v", err)
}
if nayoungFace == nil {
log.Fatalf("Not a single face on the image")
if len(faces) != 1 {
log.Fatalf("Wrong number of faces")
}
catID := rec.Classify(nayoungFace.Descriptor)
rec.Recognize(&nayoungFace[0])

catID := rec.Classify(nayoungFace[0].Descriptor)
if catID < 0 {
log.Fatalf("Can't classify")
}
Expand Down
4 changes: 2 additions & 2 deletions face.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ func (rec *Recognizer) detectFromFile(type_ int, file string) (faces []Face, err
cType := C.int(type_)
cFile := C.CString(file)
defer C.free(unsafe.Pointer(cFile))
var ptr *C.image_pointer
ret := C.facerec_detect_from_file(rec.ptr, ptr, cFile, cType)
var ptr C.image_pointer
ret := C.facerec_detect_from_file(rec.ptr, (*C.image_pointer)(unsafe.Pointer(&ptr)), cFile, cType)
defer C.free(unsafe.Pointer(ret))

if ret.err_str != nil {
Expand Down

0 comments on commit d65b255

Please sign in to comment.