MCarlomagno / FaceRecognitionAuth

😀🤳 Simple face recognition authentication (Sign up + Sign in) written in Flutter using Tensorflow Lite and Firebase ML vision library.
BSD 3-Clause "New" or "Revised" License
389 stars 194 forks source link

The code is also not working in flutter 2.16.1 #36

Closed RobbyGit closed 2 years ago

RobbyGit commented 2 years ago

I pulled the code then did a flutter pub update. Installed all the ndk requirements. Upgraded to Android 31 / update gradles/ update kotlin

I used a pixel 2 and a pixel 5. The code picks up my face in sign in but not in sigh up. I think its something to do with the camera dimensions but I would know where to look right now.

SiddharthOza00 commented 2 years ago

Can you share your code please? @RobbyGit

KALU-KELECHI-GABRIEL commented 2 years ago

@SiddharthOza00 I had the same issue no face is detected on sign up but on the sign in everything seems fine. This is the sign-up.dart file.

import 'dart:async';
import 'dart:io';
import 'dart:math' as math;
import 'package:face_net_authentication/locator.dart';
import 'package:face_net_authentication/pages/widgets/FacePainter.dart';
import 'package:face_net_authentication/pages/widgets/auth-action-button.dart';
import 'package:face_net_authentication/pages/widgets/camera_header.dart';
import 'package:face_net_authentication/services/camera.service.dart';
import 'package:face_net_authentication/services/ml_service.dart';
import 'package:face_net_authentication/services/face_detector_service.dart';
import 'package:camera/camera.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:flutter/material.dart';

class SignUp extends StatefulWidget {
  const SignUp({Key key}) : super(key: key);

  @override
  SignUpState createState() => SignUpState();
}

class SignUpState extends State<SignUp> {
  String imagePath;
  Face faceDetected;
  Size imageSize;

  bool _detectingFaces = false;
  bool pictureTaken = false;

  bool _initializing = false;

  bool _saving = false;
  bool _bottomSheetVisible = false;

  // service injection
  FaceDetectorService _faceDetectorService = locator<FaceDetectorService>();
  CameraService _cameraService = locator<CameraService>();
  MLService _mlService = locator<MLService>();

  @override
  void initState() {
    super.initState();
    _start();
  }

  @override
  void dispose() {
    _cameraService.dispose();
    super.dispose();
  }

  _start() async {
    setState(() => _initializing = true);
    await _cameraService.initialize();
    setState(() => _initializing = false);

    _frameFaces();
  }

  Future<void> onShot() async {
    if (faceDetected == null) {
      showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            content: Text('No face detected!'),
          );
        },
      );

      return false;
    } else {
      _saving = true;
      await Future.delayed(Duration(milliseconds: 500));
      await _cameraService.cameraController.stopImageStream();
      await Future.delayed(Duration(milliseconds: 200));
      XFile file = await _cameraService.takePicture();
      imagePath = file.path;

      setState(() {
        _bottomSheetVisible = true;
        pictureTaken = true;
      });

      return true;
    }
  }

  _frameFaces() {
    imageSize = _cameraService.getImageSize();

    _cameraService.cameraController.startImageStream((image) async {
      if (_cameraService.cameraController != null) {
        if (_detectingFaces) return;

        _detectingFaces = true;

        try {
          await _faceDetectorService.detectFacesFromImage(image);

          if (_faceDetectorService.faces.isNotEmpty) {
            if (_saving) {
              _mlService.setCurrentPrediction(image, faceDetected);
              setState(() {
                _saving = false;
              });
            }
          } else {
            setState(() {
              faceDetected = null;
            });
          }

          _detectingFaces = false;
        } catch (e) {
          print(e);
          _detectingFaces = false;
        }
      }
    });
  }

  _onBackPressed() {
    Navigator.of(context).pop();
  }

  _reload() {
    setState(() {
      _bottomSheetVisible = false;
      pictureTaken = false;
    });
    this._start();
  }

  @override
  Widget build(BuildContext context) {
    final double mirror = math.pi;
    final width = MediaQuery.of(context).size.width;
    final height = MediaQuery.of(context).size.height;

    Widget body;
    if (_initializing) {
      body = Center(
        child: CircularProgressIndicator(),
      );
    }

    if (!_initializing && pictureTaken) {
      body = Container(
        width: width,
        height: height,
        child: Transform(
            alignment: Alignment.center,
            child: FittedBox(
              fit: BoxFit.cover,
              child: Image.file(File(imagePath)),
            ),
            transform: Matrix4.rotationY(mirror)),
      );
    }

    if (!_initializing && !pictureTaken) {
      body = Transform.scale(
        scale: 1.0,
        child: AspectRatio(
          aspectRatio: MediaQuery.of(context).size.aspectRatio,
          child: OverflowBox(
            alignment: Alignment.center,
            child: FittedBox(
              fit: BoxFit.fitHeight,
              child: Container(
                width: width,
                height:
                    width * _cameraService.cameraController.value.aspectRatio,
                child: Stack(
                  fit: StackFit.expand,
                  children: <Widget>[
                    CameraPreview(_cameraService.cameraController),
                    CustomPaint(
                      painter:
                          FacePainter(face: faceDetected, imageSize: imageSize),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    }

    return Scaffold(
        body: Stack(
          children: [
            body,
            CameraHeader(
              "SIGN UP",
              onBackPressed: _onBackPressed,
            )
          ],
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        floatingActionButton: !_bottomSheetVisible
            ? AuthActionButton(
                onPressed: onShot,
                isLogin: false,
                reload: _reload,
              )
            : Container());
  }
}
SiddharthOza00 commented 2 years ago

I am getting these errors. Can someone help me? @RobbyGit @KALU-KELECHI-GABRIEL @guruhyes Error2 Error1 Error3

guruhyes commented 2 years ago

its because variable faceDetected not set in _frameFaces, yo need to set faceDetected , see image below file sign-up.dart function _frameFaces() image

I dunno its the right way or not, but its should work

KALU-KELECHI-GABRIEL commented 2 years ago

@guruhyes I added the line it still does not work as expected. If you could share your entire sign-up.dart file please.

KALU-KELECHI-GABRIEL commented 2 years ago

@SiddharthOza00 try setting ur android studio to point at the correct SDK version

follow the steps here https://stackoverflow.com/questions/56938436/first-flutter-app-error-cannot-resolve-symbol-properties

guruhyes commented 2 years ago

@KALU-KELECHI-GABRIEL

here is my code

`import 'dart:async'; import 'dart:io'; import 'dart:math' as math; import 'package:face_net_authentication/locator.dart'; import 'package:face_net_authentication/pages/widgets/FacePainter.dart'; import 'package:face_net_authentication/pages/widgets/auth-action-button.dart'; import 'package:face_net_authentication/pages/widgets/camera_header.dart'; import 'package:face_net_authentication/services/camera.service.dart'; import 'package:face_net_authentication/services/ml_service.dart'; import 'package:face_net_authentication/services/face_detector_service.dart'; import 'package:camera/camera.dart'; import 'package:google_ml_kit/google_ml_kit.dart'; import 'package:flutter/material.dart';

class SignUp extends StatefulWidget { const SignUp({Key key}) : super(key: key);

@override SignUpState createState() => SignUpState(); }

class SignUpState extends State { String imagePath; Face faceDetected; Size imageSize;

bool _detectingFaces = false; bool pictureTaken = false;

bool _initializing = false;

bool _saving = false; bool _bottomSheetVisible = false;

// service injection FaceDetectorService _faceDetectorService = locator(); CameraService _cameraService = locator(); MLService _mlService = locator();

@override void initState() { super.initState(); _start(); }

@override void dispose() { _cameraService.dispose(); super.dispose(); }

_start() async { setState(() => _initializing = true); await _cameraService.initialize(); setState(() => _initializing = false);

_frameFaces();

}

Future onShot() async { if (faceDetected == null) { showDialog( context: context, builder: (context) { return AlertDialog( content: Text('No face detected!'), ); }, );

  return false;
} else {
  _saving = true;
  await Future.delayed(Duration(milliseconds: 500));
  //await _cameraService.cameraController.stopImageStream();
  //await Future.delayed(Duration(milliseconds: 200));
  XFile file = await _cameraService.takePicture();
  imagePath = file.path;

  setState(() {
    _bottomSheetVisible = true;
    pictureTaken = true;
  });

  return true;
}

}

_frameFaces() { imageSize = _cameraService.getImageSize();

_cameraService.cameraController.startImageStream((image) async {
  if (_cameraService.cameraController != null) {
    if (_detectingFaces) return;

    _detectingFaces = true;

    try {
      await _faceDetectorService.detectFacesFromImage(image);

      if (_faceDetectorService.faces.isNotEmpty) {
        setState(() {
          faceDetected = _faceDetectorService.faces[0];
        });
        if (_saving) {
          _mlService.setCurrentPrediction(image, faceDetected);
          setState(() {
            _saving = false;
          });
        }
      } else {
        setState(() {
          faceDetected = null;
        });
      }

      _detectingFaces = false;
    } catch (e) {
      print(e);
      _detectingFaces = false;
    }
  }
});

}

_onBackPressed() { Navigator.of(context).pop(); }

_reload() { setState(() { _bottomSheetVisible = false; pictureTaken = false; }); this._start(); }

@override Widget build(BuildContext context) { final double mirror = math.pi; final width = MediaQuery.of(context).size.width; final height = MediaQuery.of(context).size.height;

Widget body;
if (_initializing) {
  body = Center(
    child: CircularProgressIndicator(),
  );
}

if (!_initializing && pictureTaken) {
  body = Container(
    width: width,
    height: height,
    child: Transform(
        alignment: Alignment.center,
        child: FittedBox(
          fit: BoxFit.cover,
          child: Image.file(File(imagePath)),
        ),
        transform: Matrix4.rotationY(mirror)),
  );
}

if (!_initializing && !pictureTaken) {
  body = Transform.scale(
    scale: 1.0,
    child: AspectRatio(
      aspectRatio: MediaQuery.of(context).size.aspectRatio,
      child: OverflowBox(
        alignment: Alignment.center,
        child: FittedBox(
          fit: BoxFit.fitHeight,
          child: Container(
            width: width,
            height:
                width * _cameraService.cameraController.value.aspectRatio,
            child: Stack(
              fit: StackFit.expand,
              children: <Widget>[
                CameraPreview(_cameraService.cameraController),
                CustomPaint(
                  painter:
                      FacePainter(face: faceDetected, imageSize: imageSize),
                ),
              ],
            ),
          ),
        ),
      ),
    ),
  );
}

return Scaffold(
    body: Stack(
      children: [
        body,
        CameraHeader(
          "SIGN UP",
          onBackPressed: _onBackPressed,
        )
      ],
    ),
    floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    floatingActionButton: !_bottomSheetVisible
        ? AuthActionButton(
            onPressed: onShot,
            isLogin: false,
            reload: _reload,
          )
        : Container());

} } `

KALU-KELECHI-GABRIEL commented 2 years ago

thanks a lot @guruhyes . This works just had to change the @OverRide to @override. Thank you.

RobbyGit commented 2 years ago

@KALU-KELECHI-GABRIEL Can you show me where you updated this

KALU-KELECHI-GABRIEL commented 2 years ago

@RobbyGit I don't understand the question

SiddharthOza00 commented 2 years ago

If you have the code on your github can you share your updated project with the latest changes? @KALU-KELECHI-GABRIEL

RobbyGit commented 2 years ago

@KALU-KELECHI-GABRIEL Sorry I didnt finish. I added the lines that @guruhyes suggested and it does now see a face but I get when I hit capture. What do you get .

CameraException(No camera is streaming images, stopImageStream was called when no camera is streaming images.)

RobbyGit commented 2 years ago

@KALU-KELECHI-GABRIEL . I commented out await _cameraService.cameraController.stopImageStream(); and now its working .. Have to see what else has broken

guruhyes commented 2 years ago

@KALU-KELECHI-GABRIEL . I commented out await _cameraService.cameraController.stopImageStream(); and now its working .. Have to see what else has broken

Yes commented will work, take a look to my code function onShot()

SiddharthOza00 commented 2 years ago

Can someone help me with the errors in the images I sent before?

KALU-KELECHI-GABRIEL commented 2 years ago

Can someone help me with the errors in the images I sent before?

I did check up

MCarlomagno commented 2 years ago

Hi Guys, sorry for the late answer, I just updated the project to null safety. I'm going to work on the issues found later, thanks for contribuiting!