A simple Flutter application that uses machine learning to detect faces in real-time using your device's camera.
- Real-time face detection
- Marks detected faces with bounding boxes
- Flutter SDK 3.0.0 or higher
- Dart 2.17.0 or higher
- Android Studio / VS Code
- A physical device with camera (emulators may not work properly with camera features)
-
Clone this repository
git clone https://github.com/krissphi/simple-flutter-face-detection.git
-
Navigate to the project directory
cd simple-flutter-face-detection
-
Install dependencies
flutter pub get
-
Run the app
flutter run
This app uses the following packages:
camera: ^0.10.6
- For accessing the device camerapermission_handler: ^12.0.0+1
- For handling camera permissionspath_provider: ^2.1.1
- For saving face imagesprovider: ^6.1.2
- For state managementgoogle_mlkit_face_detection: ^0.13.1
- For face detection capabilities
Add these to your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
permission_handler: ^12.0.0+1
path_provider: ^2.1.1
camera: ^0.10.6
provider: ^6.1.2
google_mlkit_face_detection: ^0.13.1
- Add camera permission to
android/app/src/main/AndroidManifest.xml
:<uses-permission android:name="android.permission.CAMERA" />
- Add camera usage description to
ios/Runner/Info.plist
:<key>NSCameraUsageDescription</key> <string>This app needs camera access to detect faces</string>
- Launch the app
- Grant camera permissions when prompted
- Point your camera at faces to see detection in action
// Example code for initializing face detection
final faceDetector = GoogleMlKit.vision.faceDetector(
FaceDetectorOptions(
enableClassification: true,
enableLandmarks: true,
enableContours: true,
enableTracking: true,
),
);
// Process image and detect faces
Future<List<Face>> processImage(InputImage inputImage) async {
return await faceDetector.processImage(inputImage);
}
- Capture and save image
- More toggle options (face count, face landmarks, face classification)
- Improve UI
- Improve Code
Common issues and solutions:
-
Camera permission denied
- Go to your device settings and enable camera permission for the app
-
App crashes on startup
- Ensure your device meets the minimum requirements
- Try reinstalling the app
-
Face detection not working
- Ensure good lighting conditions
- Hold the phone steady
- Make sure faces are clearly visible
-
Camera orientation issue
- use camera version 0.10.6 (0.11.0 and above has orientation and mirroring issues)
- Flutter Camera Plugin for the camera implementation
- Google ML Kit Face Detection for the face detection implementation