溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

添加面部跟蹤和實時識別到您的Android應用程序

發布時間:2020-07-06 07:16:19 來源:網絡 閱讀:1223 作者:wangccsy 欄目:移動開發

今天的相機應用可以做的不僅僅是拍完美的照片。無論是添加過濾器到您的圖像或讓您調整焦點和手動曝光,應用程序可以從根本上把你變成一個專業攝影師。雖然應用商店中的眾多應用程序讓你用相機做很多事情,還有其他人可以實時地對你的圖像進行巧妙的操作,例如在臉上添加自定義掩碼,操縱你的臉看起來更老或更年輕,和更多的。今天的社交媒體應用程序有這樣的智能,所以你不必打開另一個應用程序讓你的自拍看起來平凡的在你與朋友和家人分享。

無論是構建一個提供有趣過濾器的用戶應用程序,還是一個識別收入的企業應用程序,添加像這樣的智能輪胎讓你比其他人更有優勢。通過像微軟認知服務API這樣的服務,您只需添加幾行代碼即可快速分析和處理照片。

在這篇文章中,您將學習如何在Android應用程序中顯示一個攝像頭,通過使用Google的移動視覺的APIs添加面部跟蹤并使用微軟認知服務的API通過前置相機來識別人臉。

添加面部跟蹤和實時識別到您的Android應用程序

在應用程序中添加面部跟蹤

Google的移動視覺提供了一組用于“檢測面部”和“掃描條碼”的API。對于這個示例,我將使用面部API從應用程序中的實時攝像機流中檢測人臉。

開始, 添加 Xamarin.GooglePlayServices.Vision的 NuGet包。

添加面部跟蹤和實時識別到您的Android應用程序

為了在Android上訪問相機,你需要在你的Android的mainifest中添加你的用戶權限請求和相機功能:

點擊(此處)折疊或打開

  1.     

  2. if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.Camera) == Permission.Granted)

  3. {

  4.     CreateCameraSource();

  5.     //...

  6. }

  7. else

  8. {

  9. Log.Warn(TAG, "Camera permission is not granted. Requesting permission");

  10.  

  11. var permissions = new string[] { Manifest.Permission.Camera };

  12.  

  13. if (!ActivityCompat.ShouldShowRequestPermissionRationale(this,

  14.         Manifest.Permission.Camera))

  15. {

  16.     ActivityCompat.RequestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM);

  17.     return;

  18. }

  19.  

  20. Snackbar.Make(mGraphicOverlay, Resource.String.permission_camera_rationale,

  21.         Snackbar.LengthIndefinite)

  22.         .SetAction(Resource.String.ok, (o) => { ActivityCompat.RequestPermissions(this, permissions, RC_HANDLE_CAMERA_PERM); })

  23.         .Show();

  24. }


創建相機源

通過指定的屬性使用FaceDetector.Builder創建FaceDetector的一個實例。在本例中,我們使用默認的面部檢測器設置,并將處理器關聯到它。

點擊(此處)折疊或打開

  1. private void CreateCameraSource()

  2. {

  3.  

  4.     var context = Application.Context;

  5.     FaceDetector detector = new FaceDetector.Builder(context)

  6.             .SetClassificationType(ClassificationType.All)

  7.             .Build();

  8.  

  9.     detector.SetProcessor(

  10.             new MultiProcessor.Builder(this)

  11.                     .Build());

  12.  

  13.     if (!detector.IsOperational)

  14.     {

  15.         // Note: The first time that an app using face API is installed on a device, GMS will

  16.         // download a native library to the device in order to do detection.  Usually this

  17.         // completes before the app is run for the first time.  But if that download has not yet

  18.         // completed, then the above call will not detect any faces.

  19.         //

  20.         // isOperational() can be used to check if the required native library is currently

  21.         // available.  The detector will automatically become operational once the library

  22.         // download completes on the device.

  23.         Log.Warn(TAG, "Face detector dependencies are not yet available.");

  24.     }

  25.  

  26.     mCameraSource = new CameraSource.Builder(context, detector)

  27.             .SetRequestedPreviewSize(640, 480)

  28.             .SetFacing(CameraFacing.Front)

  29.             .SetRequestedFps(30.0f)

  30.             .Build();

  31.  

  32.     

  33. }


在上面的代碼中, MultiProcessor被用來接收偵測結果,CameraSource.Builder用來創建一個指定預覽尺寸,相機面對面和所需的FPS的相機源。


在相機顯示一個實時圖像

現在我們已經進入相機并且建立CameraSource,我們也可以通過自定義一個SurfaceView開始預覽;本例中CameraSourcePreview.GraphicOverlay實例也被傳遞,以便在檢測時繪制面部邊界。

點擊(此處)折疊或打開

  1. private void StartCameraSource()

  2. {

  3.  

  4.     // check that the device has play services available.

  5.     int code = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(

  6.             this.ApplicationContext);

  7.     if (code != ConnectionResult.Success)

  8.     {

  9.         var dlg =

  10.                 GoogleApiAvailability.Instance.GetErrorDialog(this, code, RC_HANDLE_GMS);

  11.         dlg.Show();

  12.     }

  13.  

  14.     if (mCameraSource != null)

  15.     {

  16.         try

  17.         {

  18.             mPreview.Start(mCameraSource, mGraphicOverlay);

  19.         }

  20.         catch (System.Exception e)

  21.         {

  22.             Log.Error(TAG, "Unable to start camera source.", e);

  23.             mCameraSource.Release();

  24.             mCameraSource = null;

  25.         }

  26.     }

  27. }


調用StartCameraSource()最好的地方是在Activity的OnResume()中,在OnPause()中停止預覽。它將確保只有在用戶使用應用程序時才使用相機資源。

人臉檢測

從相機源接收的每個圖像幀可以包含多個面,并且每個面對應由由多處理器創建的跟蹤器表示的明顯的面部標識。

實現iFactory捕捉每個面

MultiProcessor要求實現IFactory,在相機檢測到一個人臉時以便回調。在本例中,在MainActivity中實現了IFactory,包含了Create():

點擊(此處)折疊或打開

  1. public Tracker Create(Java.Lang.Object item)

  2. {

  3.     return new GraphicFaceTracker(mGraphicOverlay, mCameraSource);

  4. }


在上面的代碼中,在每檢測到一張人臉時都會創建GraphicFaceTracker的一個實例,每個構建人臉框圖形對象在使用構造函數通過GraphicOverlay查看視頻流。

下面是GraphicFaceTracker的一個實現:

點擊(此處)折疊或打開

  1. class GraphicFaceTracker : Tracker

  2. {

  3.     private GraphicOverlay mOverlay;

  4.     private FaceGraphic mFaceGraphic;

  5.  

  6.     public GraphicFaceTracker(GraphicOverlay overlay)

  7.     {

  8.         mOverlay = overlay;

  9.         mFaceGraphic = new FaceGraphic(overlay);

  10.     }

  11.  

  12.     public override void OnNewItem(int id, Java.Lang.Object item)

  13.     {

  14.         mFaceGraphic.SetId(id);

  15.     }

  16.  

  17.     public override void OnUpdate(Detector.Detections detections, Java.Lang.Object item)

  18.     {

  19.         var face = item as Face;

  20.         mOverlay.Add(mFaceGraphic);

  21.         mFaceGraphic.UpdateFace(face);

  22.     }

  23.  

  24.     public override void OnMissing(Detector.Detections detections)

  25.     {

  26.         mOverlay.Remove(mFaceGraphic);

  27.     }

  28.  

  29.     public override void OnDone()

  30.     {

  31.         mOverlay.Remove(mFaceGraphic);

  32.     }

  33. }



在第一次檢測到一張臉時會創建FaceGraphic的一個實例,在臉發生變化時會更新,當臉離開時會隱藏。

正是這樣,我們成功地在應用程序中自定義的相機表面上創建了人臉跟蹤!接下來,我們將識別幀內的人。

從實時流中識別臉

從實時視頻幀中識別臉,每當檢測到新的人臉并將其發送給微軟認知服務API來識別此人時,我們就捕捉圖像。人臉識別需要人工智能和高效的機器學習算法,提供給您的服務和免費開始。如果你是認知服務新手,我強烈推薦閱讀博客文章,增加對移動應用程序的面部識別。

捕獲檢測到的人臉

捕捉新發現的人臉,首先改變GraphicFaceTracker實現CameraSource.IPictureCallback。

點擊(此處)折疊或打開

  1. class GraphicFaceTracker : Tracker, CameraSource.IPictureCallback

  2. {

  3.     //...

  4. }

修改OnNewItem()捕捉面部圖像幀

點擊(此處)折疊或打開

  1. public override void OnNewItem(int id, Java.Lang.Object item)

  2. {

  3.     mFaceGraphic.SetId(id);

  4.     if (mCameraSource != null && !isProcessing)

  5.         mCameraSource.TakePicture(null, this);

  6. }



識別捕獲幀中的人

最后,使用助手類ImageAnalyzer  和iveCamHelper (這些抽象了COGs API調用),我們從視頻流中識別一個人。

點擊(此處)折疊或打開

  1. public void OnPictureTaken(byte[] data)

  2. {

  3.     Task.Run(async () =>

  4.     {

  5.         try

  6.         {

  7.             isProcessing = true;

  8.  

  9.             Console.WriteLine("face detected: ");

  10.  

  11.             var p_w_picpathAnalyzer = new ImageAnalyzer(data);

  12.             await LiveCamHelper.ProcessCameraCapture(p_w_picpathAnalyzer);

  13.         }

  14.  

  15.         finally

  16.         {

  17.             isProcessing = false;

  18.         }

  19.     });

  20. }

https://s3.amazonaws.com/blog.xamarin.com/wp-content/uploads/2017/06/12050912/En-Face.mp4

結束語

人臉識別在許多場景中被廣泛使用,包括安全性,自然用戶界面、機器人技術等等。通過使用這些服務和平臺API的組合,你可以建立無與倫比的移動應用程序,智能和五星級用戶提供經驗。在這個博客文章,我們使用谷歌移動視覺API來檢測視頻流和微軟認知服務中的人臉以識別幀內的人。在iOS上構建相同的示例,請閱讀如何在iOS應用程序中顯示相機 和 添加面部識別到您的移動應用程序。

在這篇文章中構建并解釋的示例是由谷歌開發人員關于面部跟蹤的文檔啟發的。

對于iOS和Android從我的GitHub的回購下載樣本。 注意:對于這個演示,我編寫了代碼來識別框架中的一個單獨的面。不過,請隨意修改并使之變得更好。


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女