I am trying to implement face detection for authentication in my xamarin application. i want to capture image automatically without user interaction, i am able to capture image using capture button but my requirement is image capture should be automatically please help me to achieve this.
Asked
Active
Viewed 1,924 times
1
-
The [Vision Framework](https://learn.microsoft.com/en-us/xamarin/ios/platform/introduction-to-ios11/vision) is new to iOS 11, if you just need this for newer phones, you can use it. For Android, I Recommend the [Google Vision Api](https://www.nuget.org/packages/Xamarin.GooglePlayServices.Vision/). You could use Services like the one from Microsoft too. – JanMer Aug 14 '18 at 07:03
1 Answers
3
There's 2 parts to doing it. You will need to open a Camera activity:
There are multiple tutorials about this :
Android
App.Instance.ShouldTakePicture += () => {
var intent = new Intent(MediaStore.ActionImageCapture);
intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(file));
StartActivityForResult(intent, 0);
};
IOS
imagePicker.FinishedPickingMedia += (sender, e) => {
var filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tmp.png");
var image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
InvokeOnMainThread(() => {
image.AsPNG().Save(filepath, false);
App.Instance.ShowImage(filepath);
});
DismissViewController(true, null);
};
Next step is to use facial recognition , once you have the users face you can store the image.
using (var stream = photo.GetStream())
{
var faceServiceClient = new FaceServiceClient("{FACE_API_SUBSCRIPTION_KEY}");
// Step 4a - Detect the faces in this photo.
var faces = await faceServiceClient.DetectAsync(stream);
var faceIds = faces.Select(face => face.FaceId).ToArray();
// Step 4b - Identify the person in the photo, based on the face.
var results = await faceServiceClient.IdentifyAsync(personGroupId, faceIds);
var result = results[0].Candidates[0].PersonId;
// Step 4c - Fetch the person from the PersonId and display their name.
var person = await faceServiceClient.GetPersonAsync(personGroupId, result);
UserDialogs.Instance.ShowSuccess($"Person identified is {person.Name}.");
}

LeRoy
- 4,189
- 2
- 35
- 46