static void WriteTrainDataAndTargets(byte[] facePix, Rect rect, FaceDisp.FaceData faceData) { Point leftEye = FaceFeatureToScaledPoint(faceData.TrueLeftEye, rect); Point rightEye = FaceFeatureToScaledPoint(faceData.TrueRightEye, rect); Point nose = FaceFeatureToScaledPoint(faceData.Nose, rect); Point leftMouth = FaceFeatureToScaledPoint(faceData.LeftMouth, rect); Point rightMouth = FaceFeatureToScaledPoint(faceData.RightMouth, rect); double[] eyes = { leftEye.X, leftEye.Y, rightEye.X, rightEye.Y, nose.X, nose.Y, leftMouth.X, leftMouth.Y, rightMouth.X, rightMouth.Y }; _outWriter.WriteSample(facePix, eyes); }
/// <summary> /// Write single patch as an instance /// </summary> /// <param name="writer">Where to write</param> /// <param name="center">Patch centre</param> /// <param name="rect">Patch size</param> /// <param name="target">Target value</param> private void WritePatch(TrainDataFileWriter writer, Point center, Rect rect, double [] target) { rect.X = center.X * _eyeDetectFaceRect.Width - rect.Width / 2.0; rect.Y = center.Y * _eyeDetectFaceRect.Height - rect.Height / 2.0; Rect faceRect = new Rect(0, 0, _faceDisplayWidth, _faceDisplayHeight); byte[] pixs = ExtractImagePatch(_normFacePixs, faceRect, rect); if (null == pixs) { string extractString = string.Format("({0:F3}, {1:F3}, {2:F3}, {3:F3})", rect.Left, rect.Top, rect.Right, rect.Bottom); string srcRectString = string.Format("({0:F3}, {1:F3}, {2:F3}, {3:F3})", faceRect.Left, faceRect.Top, faceRect.Right, faceRect.Bottom); throw new Exception("WritePatch: cannot extract patch " + extractString + " from source rect " + srcRectString); } writer.WriteSample(pixs, target); }
private int ReportFileUsingFeats(string imageFileName, List <FeaturePts> featureList) { DetectionResult detectionResult = _detector.DetectObject(imageFileName); List <ScoredRect> scoredResultList = detectionResult.GetMergedRectList(0.0F); int imageCount = 0; if (null != featureList && scoredResultList.Count > 0) { if (true != ReadPhoto(imageFileName)) { return(0); } foreach (FeaturePts features in featureList) { Point leftEye = FeaturePtToPoint((System.Drawing.PointF)features.ptLeftEye); Point rightEye = FeaturePtToPoint((System.Drawing.PointF)features.ptRightEye); Point nose = FeaturePtToPoint((System.Drawing.PointF)features.ptNose); Point leftMouth = FeaturePtToPoint((System.Drawing.PointF)features.ptLeftMouth); Point rightMouth = FeaturePtToPoint((System.Drawing.PointF)features.ptRightMouth); foreach (ScoredRect scoredRect in scoredResultList) { Rect rect = new Rect(); rect.X = scoredRect.X; rect.Y = scoredRect.Y; rect.Width = scoredRect.Width; rect.Height = scoredRect.Height; if (rect.Contains(leftEye) && rect.Contains(rightEye)) { _outStream.WriteLine("{0}", imageFileName); _outStream.Write("{0} {1} {2} {3} ", (int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height); _outStream.Write("{0:F3} {1:F3} {2:F3} {3:F3} ", leftEye.X, leftEye.Y, rightEye.X, rightEye.Y); if (true == _doPatchGeneration) { if (false == NormalizeFaceWithEyePos(rect, leftEye, rightEye, null)) { continue; } byte[] target = new byte[1]; target[0] = 0; _normFaceWriter.WriteSample(_normFacePixs, target); Rect largeRect = new Rect(0, 0, 10.0, 10.0); //Rect largeRect = new Rect(0, 0, 40.0, 40.0); //WritePatch(_leftEyeWriter, _normLeftEye, largeRect, 0); //WritePatch(_rightEyeWriter, _normRightEye, largeRect, 0); //WritePatch(_leftMouthWriter, _normLeftMouth, largeRect, 0); //WritePatch(_rightMouthWriter, _normRightMouth, largeRect, 0); //WritePatch(_leftNoseWriter, _leftNormNose, largeRect, 0); //WritePatch(_rightNoseWriter, _rightNormNose, largeRect, 0); } leftEye.X -= rect.X; leftEye.Y -= rect.Y; rightEye.X -= rect.X; rightEye.Y -= rect.Y; double[,] affine = GetFaceAffine(rect, leftEye, rightEye, _targetRect, _normLeftEye, _normRightEye); ReportNormPoint(affine, (System.Drawing.PointF)features.ptLeftEye, rect, _targetRect); ReportNormPoint(affine, (System.Drawing.PointF)features.ptRightEye, rect, _targetRect); ReportNormPoint(affine, (System.Drawing.PointF)features.ptNose, rect, _targetRect); ReportNormPoint(affine, (System.Drawing.PointF)features.ptLeftMouth, rect, _targetRect); ReportNormPoint(affine, (System.Drawing.PointF)features.ptRightMouth, rect, _targetRect); //ReportNormPoint(affine, _normLeftEye, rect, _targetRect); //ReportNormPoint(affine, _normRightEye, rect, _targetRect); //ReportNormPoint(affine, _normNose, rect, _targetRect); //ReportNormPoint(affine, _normLeftMouth, rect, _targetRect); //ReportNormPoint(affine, _normRightMouth, rect, _targetRect); _outStream.WriteLine(); _outStream.Flush(); ++imageCount; break; } } } } return(imageCount); }