Xamarin.Forms(Android)人脸检测器

乌夫·齐默曼(Ufuk Zimmerman)

我想在xamarin.forms中进行人脸检测,

我找到了必要的代码,但没有找到xamarin.forms。

这些代码仅适用于xamarin.android。

我想在xamarin.forms中设置这些代码。

如何使用依赖服务?

掌握这些信息的人可以帮助我吗?

我的密码

public class MainActivity : Activity
{
    //Intent code for camera activity
    private static int TAKE_PICTURE_CODE = 100;

    //Max Faces to detect in a picture
    private static int MAX_FACES = 5;

    //Bitmap of a picture taken
    private Bitmap cameraBitmap = null;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        Button take_picture = FindViewById<Button> (Resource.Id.take_picture);

        take_picture.Click  += take_picture_Clicked;

    }

    void take_picture_Clicked (object sender, EventArgs args )
    {
        //call OpenCamera() Event
        openCamera();
    }



    //OnActivityResult
    protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult (requestCode, resultCode, data);
        //check if method return takepicturecode
        if(TAKE_PICTURE_CODE == requestCode){
            //Call a method to process image data
            processCameraImage(data);
        }
    }
    /// Open an activity for take a picture
    private void openCamera()
    {
        Intent intent = new Intent (Android.Provider.MediaStore.ActionImageCapture);
        StartActivityForResult (intent, TAKE_PICTURE_CODE);
    }

    /// <summary>
    /// Process picture taken an change UI to detect face
    /// </summary>
    /// <param name="intent">Intent.</param>
    private void processCameraImage(Intent intent)
    {
        //Change layout to main Layout
        SetContentView(Resource.Layout.detectlayout);

        Button detect_face = FindViewById<Button> (Resource.Id.detect_face);

        detect_face.Click += detect_face_Clicked;

        ImageView image_view = FindViewById<ImageView> (Resource.Id.image_view);

        //Set image 
        image_view.SetImageBitmap(cameraBitmap);
    }

    void detect_face_Clicked (object sender, EventArgs args )
    {
        //Detect Face
        detectFaces();
    }

检测图片上的脸并在每个脸上绘制一个正方形

private void detectFaces(){  
//first check if picture has been taken  
if(null != cameraBitmap){  
    //get width of a picture  
    int width = cameraBitmap.Width;  
    //get height of a picture  
    int height = cameraBitmap.Height;  
    //Initialize a facedetector with the picture dimensions and the max number of faces to check  
    FaceDetector detector = new FaceDetector(width, height, MainActivity.MAX_FACES);  
    //Create an array of faces with the number of max faces to check  
    FaceDetector.Face[] faces = new FaceDetector.Face[MainActivity.MAX_FACES];  

    //create a main bitmap  
    Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);  
    //create a dither paint  
    Paint ditherPaint = new Paint();  
    //create a draw paint  
    Paint drawPaint = new Paint();  

    //set true dither to dither paint variable  
    ditherPaint.Dither = true;  
    //set color red for the square  
    drawPaint.Color = Color.Green;  
    //set stroke to style  
    drawPaint.SetStyle(Paint.Style.Stroke);  
    //set stroke width  
    drawPaint.StrokeWidth = 2;  

    //Create a canvas  
    Canvas canvas = new Canvas();  
    //set bitmap to canvas  
    canvas.SetBitmap(bitmap565);  
    //draw bitmap to canvas  
    canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);  

    //get a number of faces detected  
    int facesFound = detector.FindFaces(bitmap565, faces);  
    //mid face point  
    PointF midPoint = new PointF();  
    //eye distance variable  
    float eyeDistance = 0.0f;  
    //confidence variable  
    float confidence = 0.0f;  
    //print numbre of faces found  
    System.Console.WriteLine ("Number of faces found: " + facesFound);  

    //check if found at least one face  
    if(facesFound > 0)  
    {  
        //for each face draw a red squeare  
        for(int index=0; index<facesFound; ++index){  
            // get midpoint of a face  
            faces[index].GetMidPoint(midPoint);  
            //get eye distance  
            eyeDistance = faces[index].EyesDistance();  
            //get confidence  
            confidence = faces [index].Confidence ();  
            //print all parameters  
            System.Console.WriteLine ("Confidence: " + confidence +   
                ", Eye distance: " + eyeDistance +   
                ", Mid Point: (" + midPoint.X + ", " + midPoint.Y + ")");  
            //draw a square in the picture  
            canvas.DrawRect((int)midPoint.X - eyeDistance ,   
                (int)midPoint.Y- eyeDistance ,   
                (int)midPoint.X + eyeDistance,   
                (int)midPoint.Y + eyeDistance, drawPaint);  
        }  
    }  

    //get imageview from layout  
    ImageView imageView = (ImageView)FindViewById(Resource.Id.image_view);  
    //set image with the red squares to imageview  
    imageView.SetImageBitmap(bitmap565);  
}  

}

乌夫·齐默曼(Ufuk Zimmerman)

我做到了

IDetectFace界面

 public  interface IDetectFace
    {
        bool FaceDetect(string filename);
    }

detectFaces.cs

    [assembly: Xamarin.Forms.Dependency(typeof(detectFaces))]
namespace Ceptetamir.Mobil.Droid.DependencieServices
{
    class detectFaces : IDetectFace
    {
        static int TAKE_PICTURE_CODE = 100;
        static int MAX_FACES = 5;
        //static string filename = "/storage/sdcard0/Android/data/com.Ceptetamir.Mobil/files/Pictures/Test/";
        //static File file = getLatestFilefromDir(filename);
        //static Bitmap cameraBitmap = BitmapFactory.DecodeFile(file.AbsolutePath);
        public bool FaceDetect(string stream)
        {
            Bitmap cameraBitmap =  BitmapFactory.DecodeFile(stream);
            bool durum = false;
            if (null != cameraBitmap)
            {
                //get width of a picture
                int width = cameraBitmap.Width;
                //get height of a picture
                int height = cameraBitmap.Height;
                //Initialize a facedetector with the picture dimensions and the max number of faces to check
                FaceDetector detector = new FaceDetector(width, height, MAX_FACES);
                //Create an array of faces with the number of max faces to check
                Android.Media.FaceDetector.Face[] faces = new Android.Media.FaceDetector.Face[MAX_FACES];
                //create a main bitmap
                Bitmap bitmap565 = Bitmap.CreateBitmap(width, height, Bitmap.Config.Rgb565);
                //create a dither paint
                Paint ditherPaint = new Paint();
                //create a draw paint
                Paint drawPaint = new Paint();
                //set true dither to dither paint variable
                ditherPaint.Dither = true;
                //set color red for the square
                drawPaint.Color = Color.Green;
                //set stroke to style
                drawPaint.SetStyle(Paint.Style.Stroke);
                //set stroke width
                drawPaint.StrokeWidth = 2;
                //Create a canvas
                Canvas canvas = new Canvas();
                //set bitmap to canvas
                canvas.SetBitmap(bitmap565);
                //draw bitmap to canvas
                canvas.DrawBitmap(cameraBitmap, 0, 0, ditherPaint);
                //get a number of faces detected
                int facesFound = detector.FindFaces(bitmap565, faces);
                //mid face point
                PointF midPoint = new PointF();
                //eye distance variable
                float eyeDistance = 0.0f;
                //confidence variable
                float confidence = 0.0f;
                //print numbre of faces found
                System.Console.WriteLine("Number of faces found: " + facesFound);
                //check if found at least one face
                if (facesFound > 0)
                {
                    //for each face draw a red squeare
                    durum = true;
                }
            }
            return durum;
        }

和使用

DependencyService.Get<IDetectFace>().FaceDetect(file.Path);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章