Example #1
0
        /*
        public void DetectInPixels32Async(Action<Face[]> callback, Color32[] data, int width, int height, CGImageOrientation imageOrientation = CGImageOrientation.Default) {
            if (_opQueue == null)
                _opQueue = new NSOperationQueue();

            var cgimage = CGImage.FromPixels32(data, width, height, preprocessImageScale);
            data = null;

            _opQueue.AddOperation(delegate() {
                var ciimage = new CIImage(cgimage);
                cgimage = null;
                Face[] faces = DetectInImage(ciimage, imageOrientation);
                ciimage = null;

                CoreXT.RunOnMainThread(delegate() {
                    callback(faces);
                    faces = null;
                });
            });
        }
        */
        /// <summary>
        /// Detects the in image.
        /// </summary>
        /// <returns>The in image.</returns>
        /// <param name="ciimage">Ciimage.</param>
        /// <param name="imageOrientation">Image orientation.</param>
        public Face[] DetectInImage(CIImage ciimage, CGImageOrientation imageOrientation = CGImageOrientation.Default)
        {
            var rect = ciimage.Extent();
            int imageHeight = (int)rect.height;
            int imageWidth = (int)rect.width;
            _finalScale = projectedScale / preprocessImageScale;

            // options
            _imageOpts[CIDetector.ImageOrientation] = (int)imageOrientation;
            _imageOpts[CIDetector.Smile] = detectSmiles;
            _imageOpts[CIDetector.EyeBlink] = detectBlinks;

            // detect
            var features = _detector.FeaturesInImage(ciimage, _imageOpts);

            // go through features and transform coords
            var faces = new Face[features.Length];
            for (int i=0; i<features.Length; i++) {
                var feature = features[i] as CIFaceFeature;
                var face = new Face();

                face.bounds = _FixRect(feature.bounds, imageHeight, imageWidth, imageOrientation);

                if (feature.hasMouthPosition) {
                    face.hasMouthPosition = true;
                    face.mouthPosition = _FixPoint(feature.mouthPosition, imageHeight, imageWidth, imageOrientation);
                }

                if (feature.hasLeftEyePosition) {
                    face.hasLeftEyePosition = true;
                    face.leftEyePosition = _FixPoint(feature.leftEyePosition, imageHeight, imageWidth, imageOrientation);
                }

                if (feature.hasRightEyePosition) {
                    face.hasRightEyePosition = true;
                    face.rightEyePosition = _FixPoint(feature.rightEyePosition, imageHeight, imageWidth, imageOrientation);
                }

                if (feature.RespondsToSelector("trackingID")) {
                    if (feature.hasTrackingID) {
                        face.hasTrackingID = true;
                        face.trackingID = feature.trackingID;
                    }

                    if (feature.hasTrackingFrameCount) {
                        face.hasTrackingFrameCount = true;
                        face.trackingFrameCount = feature.trackingFrameCount;
                    }
                }

                if (feature.RespondsToSelector("faceAngle")) {
                    if (feature.hasFaceAngle) {
                        face.hasFaceAngle = true;
                        face.faceAngle = feature.faceAngle;
                    }

                    face.hasSmile = feature.hasSmile;
                    face.leftEyeClosed = feature.leftEyeClosed;
                    face.rightEyeClosed = feature.rightEyeClosed;
                }

                faces[i] = face;
            }

            return faces;
        }
Example #2
0
        /// <summary>
        /// Detects the in image.
        /// </summary>
        /// <returns>The in image.</returns>
        /// <param name="ciimage">Ciimage.</param>
        /// <param name="imageOrientation">Image orientation.</param>
        public Face[] DetectInImage(CIImage ciimage, CGImageOrientation imageOrientation = CGImageOrientation.Default)
        {
            var rect = ciimage.Extent();
            int imageHeight = (int)(rect.height * PreprocessImageScale);
            int imageWidth = (int)(rect.width * PreprocessImageScale);
            _finalScale = ProjectedScale / PreprocessImageScale;

            // create CIImage from bitmapdata
            //			var ciimage:CIImage = CIImage.fromBitmapData(image, preprocessImageScale); //TODO
            //			var ciimage = new CIImage(CGImage.FromTexture2D(image));

            // orientation settings
            _imageOpts[CIDetector.ImageOrientation] = (int)imageOrientation;

            // detect
            var features = _detector.FeaturesInImage(ciimage, _imageOpts);

            // go through features and transform coords
            var faces = new Face[features.Length];
            for (int i=0; i<features.Length; i++) {
                var feature = features[i] as CIFaceFeature;
                var face = new Face();

                face.bounds = _FixRect(feature.bounds, imageHeight, imageWidth, imageOrientation);

                if (feature.hasMouthPosition) {
                    face.hasMouthPosition = true;
                    face.mouthPosition = _FixPoint(feature.mouthPosition, imageHeight, imageWidth, imageOrientation);
                }

                if (feature.hasLeftEyePosition) {
                    face.hasLeftEyePosition = true;
                    face.leftEyePosition = _FixPoint(feature.leftEyePosition, imageHeight, imageWidth, imageOrientation);
                }

                if (feature.hasRightEyePosition) {
                    face.hasRightEyePosition = true;
                    face.rightEyePosition = _FixPoint(feature.rightEyePosition, imageHeight, imageWidth, imageOrientation);
                }

                if (feature.RespondsToSelector("trackingID")) {
                    if (feature.hasTrackingID) {
                        face.hasTrackingID = true;
                        face.trackingID = feature.trackingID;
                    }

                    if (feature.hasTrackingFrameCount) {
                        face.hasTrackingFrameCount = true;
                        face.trackingFrameCount = feature.trackingFrameCount;
                    }
                }

                faces[i] = face;
            }

            return faces;
        }