Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// Write all patches to a single (combined file)
        /// </summary>
        /// <param name="writer">Where to write</param>
        /// <param name="center">Patch centre</param>
        /// <param name="rect">Patch size</param>
        /// <param name="target">Unused</param>
        private void WritePatchCombined(TrainDataFileWriter writer, Point center, Rect rect, double[] target)
        {
            rect.X = center.X * _faceDisplayWidth - rect.Width / 2.0;
            rect.Y = center.Y * _faceDisplayHeight - rect.Height / 2.0;
            Rect targetRect = new Rect(0, 0, _faceDisplayWidth, _faceDisplayHeight);

            byte[] pixs = ExtractImagePatch(_normFacePixs, targetRect, rect);

            //writer.WriteSample(pixs, target);
            if (writer.PartialCount > 1)
            {
                writer.WriteDataContinue(pixs);
            }
            else
            {
                writer.WriteNewInput(pixs);
            }
        }
Пример #3
0
        public FaceRecoGen(string[] args, int iArg)
        {
            float  detectionThresh = 0.0F;
            string classifierPath  = @"D:\ll\private\research\private\CollaborativeLibs_01\LibFaceDetect\FaceDetect\Classifier\classifier.txt";
            string outFile         = "out.txt";

            _faceDetectPixCount = (int)(_eyeDetectFaceRect.Width * _eyeDetectFaceRect.Height);

            if (args.Length < 1)
            {
                return;
            }
            bool doExtra = false;


            ProgActions action         = ProgActions.None;
            string      dataFilePrefix = "";

            while (iArg < args.Length - 1)
            {
                switch (args[iArg++].ToLower())
                {
                case "-detectpath":
                    classifierPath = args[iArg++];
                    break;

                case "-eyedetectpath":
                    _eyeDetectPath = args[iArg++];
                    break;

                case "-detectphoto":
                    action = ProgActions.DetectPhotos;
                    break;

                case "-predetectphoto":
                    action = ProgActions.PreDetectPhotos;
                    break;

                case "-generatepatch":
                    _doPatchGeneration = true;
                    break;

                case "-generatejpg":
                    _dumpJpgCount = 0;
                    break;

                case "-reportusingfeats":
                    action = ProgActions.ReportPositions;
                    break;

                case "-writebinary":
                    _dataFileMode = TrainDataFileWriter.ModeEnum.Binary;
                    break;

                case "-writetext":
                    _dataFileMode = TrainDataFileWriter.ModeEnum.Text;
                    break;

                case "-datafileprefix":
                    dataFilePrefix = args[iArg++];
                    break;

                case "-skipfacedetect":
                    _skipFaceDetect = true;
                    break;

                case "-addextra":
                    doExtra = true;
                    break;

                case "-out":
                    outFile = args[iArg++];
                    break;

                default:
                    Console.WriteLine("Unrecognized option {0}", args[iArg - 1]);
                    return;
                }
            }

            string filename = args[iArg];

            _detector  = new FaceDetector(classifierPath, true, detectionThresh);
            _outStream = new StreamWriter(outFile);

            if (true == _doPatchGeneration)
            {
                _normFaceWriter = new TrainDataFileWriter(dataFilePrefix + "normFace.dat", _dataFileMode);

                if (doExtra == true)
                {
                    _featureWriter = new TrainDataFileWriter(dataFilePrefix + "featuresFace.dat", _dataFileMode);
                }

                //_leftEyeWriter = new TrainDataFileWriter(dataFilePrefix + "leftEye.dat", _dataFileMode);
                //_rightEyeWriter = new TrainDataFileWriter(dataFilePrefix + "rightEye.dat", _dataFileMode);
                //_leftMouthWriter = new TrainDataFileWriter(dataFilePrefix + "leftMouth.dat", _dataFileMode);
                //_rightMouthWriter = new TrainDataFileWriter(dataFilePrefix + "rightMouth.dat", _dataFileMode);
                //_leftNoseWriter = new TrainDataFileWriter(dataFilePrefix + "leftNose.dat", _dataFileMode);
                //_rightNoseWriter = new TrainDataFileWriter(dataFilePrefix + "rightNose.dat", _dataFileMode);
            }

            try
            {
                if (action == ProgActions.ReportPositions)
                {
                    ReportUsingFeatures(filename);
                }
                else if (action == ProgActions.DetectPhotos)
                {
                    ReportPhotos(filename);
                }
                else if (action == ProgActions.PreDetectPhotos)
                {
                    ReportPreDetectPhotos(filename);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("{0}", e.Message);
            }

            if (true == _doPatchGeneration)
            {
                if (null != _normFaceWriter)
                {
                    _normFaceWriter.Dispose();
                }

                if (null != _featureWriter)
                {
                    _featureWriter.Dispose();
                }

                if (null != _leftEyeWriter)
                {
                    _leftEyeWriter.Dispose();
                    _rightEyeWriter.Dispose();
                    _leftMouthWriter.Dispose();
                    _rightMouthWriter.Dispose();
                    _leftNoseWriter.Dispose();
                    _rightNoseWriter.Dispose();
                }
            }

            _outStream.Close();
        }
Пример #4
0
        static private int _numPatchFeatures          = 4; // Eye, Nose, LeftMouth, RightMouth

        static void Main(string[] args)
        {
            float  detectionThresh = 0.0F;
            string classifierPath  = "classifier.txt";
            int    iArg            = 0;
            string outFile         = "out.Txt";
            bool   preDetect       = false;

            _normalizeAction = NormalizeActionsEnum.None;

            _action               = ProgActions.GenerateEyeFaceData;
            _normalizeSum         = 211676;
            _defaultNormalizedLen = 41 * 41;
            _maxTransformCount    = 0;

            _algo     = EyeDetect.AlgorithmEnum.MSRA;
            _algoData = "";

            if (args.Length < 1)
            {
                Usage();
                return;
            }

            while (iArg < args.Length - 1)
            {
                switch (args[iArg++].ToLower())
                {
                case "-width":
                    _faceDisplayWidth = Convert.ToDouble(args[iArg++]);
                    break;

                case "-height":
                    _faceDisplayHeight = Convert.ToDouble(args[iArg++]);
                    break;

                case "-eyewidth":
                    _eyePatchWidth = Convert.ToDouble(args[iArg++]);
                    break;

                case "-eyeheight":
                    _eyePatchHeight = Convert.ToDouble(args[iArg++]);
                    break;

                case "-noneyepatchcount":
                    _nonEyePatchCount = Convert.ToInt32(args[iArg++]);
                    break;

                case "-detectpath":
                    classifierPath = args[iArg++];
                    break;

                case "-detect":
                    _action = ProgActions.FaceDetect;
                    break;

                case "-generateeyeface":
                    _action = ProgActions.GenerateEyeFaceData;
                    break;


                case "-generateeyepatch":
                    _action = ProgActions.GenerateEyePatchData;
                    break;

                case "-generatereco":
                    FaceRecoGen gen = new FaceRecoGen(args, iArg);
                    return;

                case "-normsum":
                    _normalizeAction = NormalizeActionsEnum.ConstantSum;
                    break;

                case "-normblur":
                    _normalizeAction = NormalizeActionsEnum.BlurSubtract;
                    break;

                case "-normblur3":
                    _normalizeAction = NormalizeActionsEnum.BlurSubtract3;
                    break;

                case "-blurvar":
                    _blurVar = Convert.ToSingle(args[iArg++]);
                    break;

                case "-blurkernel":
                    _blurKernelSize = Convert.ToInt32(args[iArg++]);
                    break;

                case "-normsift":
                    _normalizeAction = NormalizeActionsEnum.SIFT;
                    break;

                case "-siftpartnum":
                    _partitionNumber = Convert.ToInt32(args[iArg++]);
                    break;

                case "-msra":
                    _algo = EyeDetect.AlgorithmEnum.MSRA;
                    break;

                case "-nn":
                    _algo     = EyeDetect.AlgorithmEnum.NN;
                    _algoData = args[iArg++];
                    break;

                case "-predetect":
                    preDetect = true;
                    break;

                case "-maxtransformcount":
                    _maxTransformCount = Convert.ToInt32(args[iArg++]);;
                    break;

                case "-maxtheta":
                    _transform.MaxTheta = Convert.ToDouble(args[iArg++]);
                    break;

                case "-maxx":
                    _transform.MaxX = Convert.ToDouble(args[iArg++]);
                    break;

                case "-maxy":
                    _transform.MaxY = Convert.ToDouble(args[iArg++]);
                    break;

                case "-writebinary":
                    _dataFileMode = TrainDataFileWriter.ModeEnum.Binary;
                    break;

                case "-writetext":
                    _dataFileMode = TrainDataFileWriter.ModeEnum.Text;
                    break;

                case "-out":
                    outFile = args[iArg++];
                    break;

                case "-h":
                    Usage();
                    return;

                default:
                    Console.WriteLine("Unrecognized option {0}", args[iArg - 1]);
                    break;
                }
            }

            if (_action == ProgActions.GenerateEyeFaceData ||
                _action == ProgActions.GenerateEyePatchData)
            {
                _outWriter = new TrainDataFileWriter(outFile, _dataFileMode);
            }
            else
            {
                _outStream = new StreamWriter(outFile);
            }

            string suiteFile = args[iArg];

            try
            {
                if (System.IO.File.Exists(suiteFile) == false)
                {
                    Console.WriteLine("Cannot open suiteFile {0}\n", suiteFile);
                    Usage();
                    return;
                }
                if (true == preDetect)
                {
                    DoPreDetectSuiteFile(suiteFile);
                }
                else
                {
                    _detector = new FaceDetector(classifierPath, true, detectionThresh);
                    DoSuiteFile(suiteFile);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error {0}", e.Message);
                if (null != e.InnerException)
                {
                    Console.WriteLine("{0}", e.InnerException.Message);
                }
            }

            if (null != _outWriter)
            {
                _outWriter.Dispose();
            }
            if (null != _outStream)
            {
                _outStream.Close();
            }
        }