commit 8957d38f1664299a75c3b2d2b6fc29c691a9de25 Author: Daniel Date: Wed Aug 26 08:37:16 2020 +0300 init diff --git a/main.go b/main.go new file mode 100644 index 0000000..a11d042 --- /dev/null +++ b/main.go @@ -0,0 +1,139 @@ +package main + +import ( + "fmt" + "image" + "time" + + "github.com/Danile71/go-face" + "gocv.io/x/gocv" +) + +const ( + netModel = "models/face-detection-retail-0005.bin" + netProto = "models/face-detection-retail-0005.xml" + cnnModel = "models/mmod_human_face_detector.dat" + shapeModel = "models/shape_predictor_68_face_landmarks.dat" + descrModel = "models/dlib_face_recognition_resnet_model_v1.dat" + ageModel = "models/dnn_age_predictor_v1.dat" + genderModel = "models/dnn_gender_classifier_v1.dat" +) + +//openvino detecting +//dlib recognize + +func main() { + var ( + pt = image.Pt(300, 300) + scalar = gocv.NewScalar(0, 0, 0, 0) + faceNet = gocv.ReadNet(netModel, netProto) + ) + + faceNet.SetPreferableBackend(gocv.NetBackendDefault) + faceNet.SetPreferableTarget(gocv.NetTargetCPU) + defer faceNet.Close() + + rec, err := face.NewRecognizer() + if err != nil { + fmt.Println(err) + return + } + defer rec.Close() + + err = rec.SetShapeModel(shapeModel) + if err != nil { + fmt.Println(err) + return + } + + err = rec.SetDescriptorModel(descrModel) + if err != nil { + fmt.Println(err) + return + } + + err = rec.SetAgeModel(ageModel) + if err != nil { + fmt.Println(err) + return + } + + err = rec.SetGenderModel(genderModel) + if err != nil { + fmt.Println(err) + return + } + + err = rec.SetCNNModel(cnnModel) + if err != nil { + fmt.Println(err) + return + } + start := time.Now() + defer func() { + fmt.Println("elapsed", time.Since(start)) + }() + img := gocv.IMRead("test.jpg", gocv.IMReadUnchanged) + defer img.Close() + + width := img.Cols() + height := img.Rows() + + blob := gocv.BlobFromImage(img, 1, pt, scalar, false, false) + defer blob.Close() + + faceNet.SetInput(blob, "") + + outputFace := faceNet.Forward("") + defer outputFace.Close() + + for i := 0; i < outputFace.Total(); i += 7 { + confidence := outputFace.GetFloatAt(0, i+2) + + if confidence > 0.5 { + + left := int(outputFace.GetFloatAt(0, i+3) * float32(width)) + top := int(outputFace.GetFloatAt(0, i+4) * float32(height)) + right := int(outputFace.GetFloatAt(0, i+5) * float32(width)) + bottom := int(outputFace.GetFloatAt(0, i+6) * float32(height)) + + //bugfix + if left < 0 { + left = 0 + } + if top < 0 { + top = 0 + } + if right > img.Cols() { + right = img.Cols() + } + if bottom > img.Rows() { + bottom = img.Rows() + } + + r := image.Rect(left, top, right, bottom) + submat := img.Region(r) + + submatgray := gocv.NewMat() + gocv.CvtColor(submat, &submatgray, gocv.ColorBGRToGray) + + faces, err := rec.DetectFromMatCNN(submatgray) + submatgray.Close() + + if err != nil { + fmt.Println(err) + continue + } + + for _, f := range faces { + rec.Recognize(&f) + rec.GetAge(&f) + rec.GetGender(&f) + fmt.Println("detected:", f.Rectangle, " age:", f.Age, " gender:", f.Gender) + f.Close() + } + submat.Close() + } + + } +} diff --git a/models/dlib_face_recognition_resnet_model_v1.dat b/models/dlib_face_recognition_resnet_model_v1.dat new file mode 100644 index 0000000..ddb5158 Binary files /dev/null and b/models/dlib_face_recognition_resnet_model_v1.dat differ diff --git a/models/dnn_age_predictor_v1.dat b/models/dnn_age_predictor_v1.dat new file mode 100644 index 0000000..78251a0 Binary files /dev/null and b/models/dnn_age_predictor_v1.dat differ diff --git a/models/dnn_gender_classifier_v1.dat b/models/dnn_gender_classifier_v1.dat new file mode 100644 index 0000000..5f6ff08 Binary files /dev/null and b/models/dnn_gender_classifier_v1.dat differ diff --git a/models/face-detection-retail-0005.bin b/models/face-detection-retail-0005.bin new file mode 100644 index 0000000..3719f76 Binary files /dev/null and b/models/face-detection-retail-0005.bin differ diff --git a/models/face-detection-retail-0005.xml b/models/face-detection-retail-0005.xml new file mode 100644 index 0000000..879ae0c --- /dev/null +++ b/models/face-detection-retail-0005.xml @@ -0,0 +1,2661 @@ + + + + + + + 1 + 3 + 300 + 300 + + + + + + + + 1 + 3 + 300 + 300 + + + + + 1 + 3 + 300 + 300 + + + + + + + 1 + 3 + 300 + 300 + + + + + 1 + 3 + 300 + 300 + + + + + + + + + + + + 1 + 3 + 300 + 300 + + + + + 1 + 24 + 150 + 150 + + + + + + + + + + + 1 + 24 + 150 + 150 + + + + + 1 + 24 + 150 + 150 + + + + + + + + 1 + 24 + 150 + 150 + + + + + 1 + 24 + 150 + 150 + + + + + + + + + + + 1 + 24 + 150 + 150 + + + + + 1 + 24 + 150 + 150 + + + + + + + + 1 + 24 + 150 + 150 + + + + + 1 + 12 + 150 + 150 + + + + + + + + + + + + 1 + 12 + 150 + 150 + + + + + 1 + 72 + 150 + 150 + + + + + + + + + + + 1 + 72 + 150 + 150 + + + + + 1 + 72 + 150 + 150 + + + + + + + + 1 + 72 + 150 + 150 + + + + + 1 + 72 + 75 + 75 + + + + + + + + + + + 1 + 72 + 75 + 75 + + + + + 1 + 72 + 75 + 75 + + + + + + + + 1 + 72 + 75 + 75 + + + + + 1 + 18 + 75 + 75 + + + + + + + + + + + + 1 + 18 + 75 + 75 + + + + + 1 + 108 + 75 + 75 + + + + + + + + + + + 1 + 108 + 75 + 75 + + + + + 1 + 108 + 75 + 75 + + + + + + + + 1 + 108 + 75 + 75 + + + + + 1 + 108 + 75 + 75 + + + + + + + + + + + 1 + 108 + 75 + 75 + + + + + 1 + 108 + 75 + 75 + + + + + + + + 1 + 108 + 75 + 75 + + + + + 1 + 18 + 75 + 75 + + + + + + + + + + + + 1 + 18 + 75 + 75 + + + 1 + 18 + 75 + 75 + + + + + 1 + 18 + 75 + 75 + + + + + + + + 1 + 18 + 75 + 75 + + + + + 1 + 108 + 75 + 75 + + + + + + + + + + + 1 + 108 + 75 + 75 + + + + + 1 + 108 + 75 + 75 + + + + + + + + 1 + 108 + 75 + 75 + + + + + 1 + 108 + 38 + 38 + + + + + + + + + + + 1 + 108 + 38 + 38 + + + + + 1 + 108 + 38 + 38 + + + + + + + + 1 + 108 + 38 + 38 + + + + + 1 + 24 + 38 + 38 + + + + + + + + + + + + 1 + 24 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 24 + 38 + 38 + + + + + + + + + + + + 1 + 24 + 38 + 38 + + + 1 + 24 + 38 + 38 + + + + + 1 + 24 + 38 + 38 + + + + + + + + 1 + 24 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 24 + 38 + 38 + + + + + + + + + + + + 1 + 24 + 38 + 38 + + + 1 + 24 + 38 + 38 + + + + + 1 + 24 + 38 + 38 + + + + + + + + 1 + 24 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 38 + 38 + + + + + + + + 1 + 144 + 38 + 38 + + + + + 1 + 144 + 19 + 19 + + + + + + + + + + + 1 + 144 + 19 + 19 + + + + + 1 + 144 + 19 + 19 + + + + + + + + 1 + 144 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + + + + + 1 + 48 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + + + + + 1 + 48 + 19 + 19 + + + 1 + 48 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + 1 + 48 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + + + + + 1 + 48 + 19 + 19 + + + 1 + 48 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + 1 + 48 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + + + + + 1 + 48 + 19 + 19 + + + 1 + 48 + 19 + 19 + + + + + 1 + 48 + 19 + 19 + + + + + + + + 1 + 48 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 288 + 19 + 19 + + + + + + + + 1 + 288 + 19 + 19 + + + + + 1 + 72 + 19 + 19 + + + + + + + + + + + + 1 + 72 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 72 + 19 + 19 + + + + + + + + + + + + 1 + 72 + 19 + 19 + + + 1 + 72 + 19 + 19 + + + + + 1 + 72 + 19 + 19 + + + + + + + + 1 + 72 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 72 + 19 + 19 + + + + + + + + + + + + 1 + 72 + 19 + 19 + + + 1 + 72 + 19 + 19 + + + + + 1 + 72 + 19 + 19 + + + + + + + + 1 + 72 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 432 + 19 + 19 + + + + + + + + 1 + 432 + 19 + 19 + + + + + 1 + 120 + 19 + 19 + + + + + + + + + + + + 1 + 120 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 120 + 19 + 19 + + + + + + + + + + + + 1 + 120 + 19 + 19 + + + 1 + 120 + 19 + 19 + + + + + 1 + 120 + 19 + 19 + + + + + + + + 1 + 120 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 720 + 19 + 19 + + + + + + + + 1 + 720 + 19 + 19 + + + + + 1 + 120 + 19 + 19 + + + + + + + + + + + + 1 + 120 + 19 + 19 + + + 1 + 120 + 19 + 19 + + + + + 1 + 120 + 19 + 19 + + + + + + + + 1 + 120 + 19 + 19 + + + + + 1 + 480 + 19 + 19 + + + + + + + + + + + 1 + 480 + 19 + 19 + + + + + 1 + 480 + 19 + 19 + + + + + + + + 1 + 480 + 19 + 19 + + + + + 1 + 480 + 19 + 19 + + + + + + + + + + + 1 + 480 + 19 + 19 + + + + + 1 + 480 + 19 + 19 + + + + + + + + 1 + 480 + 19 + 19 + + + + + 1 + 360 + 19 + 19 + + + + + + + + + + + + 1 + 360 + 19 + 19 + + + + + 1 + 360 + 19 + 19 + + + + + + + + + + + 1 + 360 + 19 + 19 + + + + + 1 + 360 + 19 + 19 + + + + + + + + 1 + 360 + 19 + 19 + + + + + 1 + 36 + 19 + 19 + + + + + + + + + + + + 1 + 36 + 19 + 19 + + + + + 1 + 19 + 19 + 36 + + + + + + + 2 + + + + + + + + + + 1 + 19 + 19 + 36 + + + 2 + + + + + 1 + 12996 + + + + + + + + 1 + 12996 + + + + + 1 + 12996 + + + + + + + + 1 + 360 + 19 + 19 + + + + + 1 + 360 + 19 + 19 + + + + + + + + + + + 1 + 360 + 19 + 19 + + + + + 1 + 360 + 19 + 19 + + + + + + + + 1 + 360 + 19 + 19 + + + + + 1 + 18 + 19 + 19 + + + + + + + + + + + + 1 + 18 + 19 + 19 + + + + + 1 + 19 + 19 + 18 + + + + + + + 2 + + + + + + + + + + 1 + 19 + 19 + 18 + + + 2 + + + + + 1 + 6498 + + + + + + + + 1 + 6498 + + + + + 1 + 6498 + + + + + + + 3 + + + + + + + + + + 1 + 6498 + + + 3 + + + + + 1 + 3249 + 2 + + + + + + + + 1 + 3249 + 2 + + + + + 1 + 3249 + 2 + + + + + + + 2 + + + + + + + + + + 1 + 3249 + 2 + + + 2 + + + + + 1 + 6498 + + + + + + + + 1 + 360 + 19 + 19 + + + 1 + 3 + 300 + 300 + + + + + 1 + 2 + 12996 + + + + + + + + 1 + 2 + 12996 + + + + + 1 + 2 + 12996 + + + + + + + + 1 + 12996 + + + 1 + 6498 + + + 1 + 2 + 12996 + + + + + 1 + 1 + 200 + 7 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/models/mmod_human_face_detector.dat b/models/mmod_human_face_detector.dat new file mode 100644 index 0000000..f112a0a Binary files /dev/null and b/models/mmod_human_face_detector.dat differ diff --git a/models/shape_predictor_5_face_landmarks.dat b/models/shape_predictor_5_face_landmarks.dat new file mode 100644 index 0000000..67878ed Binary files /dev/null and b/models/shape_predictor_5_face_landmarks.dat differ diff --git a/models/shape_predictor_68_face_landmarks.dat b/models/shape_predictor_68_face_landmarks.dat new file mode 100644 index 0000000..e0ec20d Binary files /dev/null and b/models/shape_predictor_68_face_landmarks.dat differ diff --git a/test.jpg b/test.jpg new file mode 100644 index 0000000..a90b268 Binary files /dev/null and b/test.jpg differ