Flutter Blink Detection
is a Flutter package that provides a controller for detecting faces and blinks using the camera feed and Google's ML Kit. It uses google_ml_kit
for face detection and classification, and camera
for capturing real-time images.
- Detects faces using Google's ML Kit.
- Detects blink events and counts the number of blinks.
- Callsbacks for success, no face detected, blink detected, and face detected.
- Simple to use and integrate.
Before using this package, make sure you have the following dependencies in your pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
camera: ^0.11.0+2
google_ml_kit: ^0.19.0
Also, ensure you have enabled the appropriate camera permissions in your AndroidManifest.xml and Info.plist files:
In your AndroidManifest.xml
, add the following permissions:
<uses-permission android:name="android.permission.CAMERA"/>
In your Info.plist
, add the following:
<key>NSCameraUsageDescription</key>
<string>We need access to your camera for face detection</string>
Run flutter pub get
to install the dependencies.
You need to initialize the FaceCameraController with a specified CameraLensDirection, such as front or back:
FaceCameraController faceCameraController = FaceCameraController(
cameraLensDirection: CameraLensDirection.front,
onSuccess: () {
print('Blink detected 2 times. Success callback triggered.');
},
noFaceDetected: () {
print('No face detected.');
},
onBlinkDetected: (blinkCount) {
print('Blinks detected: $blinkCount');
},
onFaceDetected: () {
print('Face detected.');
},
);
Call initialize() to start the camera and begin face detection:
await faceCameraController.initialize(CameraLensDirection.front);
Once the camera feed is active, the controller will detect faces and blinks in real-time. You can use the provided callbacks to handle events:
- onSuccess: Triggered when a specified number of blinks (default is 2) are detected.
- noFaceDetected: Called when no face is detected in the camera feed.
- onBlinkDetected: Returns the count of detected blinks.
- onFaceDetected: Called when a face is detected.
class FaceDetectionScreen extends StatefulWidget {
@override
_FaceDetectionScreenState createState() => _FaceDetectionScreenState();
}
class _FaceDetectionScreenState extends State<FaceDetectionScreen> {
late FaceCameraController faceCameraController;
@override
void initState() {
super.initState();
faceCameraController = FaceCameraController(
cameraLensDirection: CameraLensDirection.front,
onSuccess: () {
print('Blink detected. Success callback triggered.');
},
noFaceDetected: () {
print('No face detected.');
},
onBlinkDetected: (blinkCount) {
print('Blinks detected: $blinkCount');
},
onFaceDetected: () {
print('Face detected.');
},
);
faceCameraController.initialize(CameraLensDirection.front);
}
@override
void dispose() {
faceCameraController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Face Detection'),
),
body: Center(
child: CameraPreview(faceCameraController.cameraController),
),
);
}
}