我想在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);
}
}
答案 0 :(得分:1)
您只需要找到如何在Xamarin.Forms中拍照,然后使用FaceDetector
来处理Android或Ios返回的图片。
Here很简单,使用DependencyService
在Android和Ios中拍照,由@ Vaka.GopiNadhReddy提供,并感谢@Vaka。
下载它,然后在detectFaces
方法中更改一些内容,并将其添加到MainActivity
。
我已经完成了它,你可以参考下面的代码:
private Bitmap detectFaces(Bitmap cameraBitmap)
{
//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 = Android.Graphics.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);
}
}
return bitmap565;
}
return null;
}
在OnActivityResult
中,您需要添加
AppClass.bitmap = detectFaces(AppClass.bitmap);
在这一行之下:
AppClass.bitmap = AppClass._file.Path.LoadAndResizeBitmap(width, width);
最后,不要忘记添加:
public static int MAX_FACES = 5;
<{1>}中的。
答案 1 :(得分:0)
我做到了
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);