An Example of Face Detection with Vision in Flutter.
Using MethodChannel to communicate between Flutter and Native code, we can use the Vision API to detect faces in images.
Check my Medium article for more details: Face Detection with Vision in Flutter
import UIKit
import Flutter
import Vision
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let faceDetectionChannel = FlutterMethodChannel(name: "com.example.faceDetectionIos/faceDetectionIos", binaryMessenger: controller.binaryMessenger)
faceDetectionChannel.setMethodCallHandler({(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "getFaceCountFromImage" {
if let imagePath = call.arguments as? String {
self.detectFacesFromImage(imagePath: imagePath, result: result)
}
} else {
result(FlutterMethodNotImplemented)
}
})
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
private func detectFacesFromImage(imagePath: String, result: @escaping FlutterResult) {
let imageURL = URL(fileURLWithPath: imagePath)
guard let image = CIImage(contentsOf: imageURL) else {
result(FlutterError(code: "UNAVAILABLE", message: "Cannot load image", details: nil))
return
}
let faceDetectionRequest = VNDetectFaceRectanglesRequest { (request, error) in
guard error == nil else {
result(FlutterError(code: "ERROR", message: error?.localizedDescription, details: nil))
return
}
let faceCount = request.results?.count ?? 0
result(faceCount)
}
#if targetEnvironment(simulator)
faceDetectionRequest.usesCPUOnly = true
#endif
let handler = VNImageRequestHandler(ciImage: image, options: [:])
do {
try handler.perform([faceDetectionRequest])
} catch {
result(FlutterError(code: "ERROR", message: "Face detection failed", details: error.localizedDescription))
}
}
}
import 'dart:io';
import 'package:flutter/services.dart';
abstract class FaceDetectionManager {
static const _channel = MethodChannel('com.example.faceDetectionIos/faceDetectionIos');
static Future<int> detectFaceFromImage(String imagePath) async {
try {
if (Platform.isIOS) {
final faceCount = await _channel.invokeMethod<int>('getFaceCountFromImage', imagePath);
if (faceCount == null) {
throw 'Face count is null';
}
return faceCount;
} else if (Platform.isAndroid) {
//* You can use the Google ML Kit for Android or iOS to detect faces in an image.
//* See details here: https://developers.google.com/ml-kit/vision/face-detection
}
throw UnsupportedError('Unsupported platform');
} catch (e) {
rethrow;
}
}
}
- Clone Project
git clone https://github.com/enesakbal/face-detection-ios.git
flutter run
created by ea.