MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    Facial Recognition Module - Step by Step Guide

    Scheduled Pinned Locked Moved Troubleshooting
    28 Posts 14 Posters 34.8k Views 22 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      konradwalsh
      last edited by konradwalsh

      Hi all,

      Due to there being no guide on this and I was looking for one… I have decided to just make one…
      I only start figuring it out last night and so I am still setting up. So I will complete this post as I progress.

      I am using the following hardware

      • Raspberry Pi 3

      • Logitech 1080 HD USB Webcam

      • MagicMirror is installed and confirmed functioning correctly.

      The software I am using to do this is Mobaxterm.
      It might be easier to do all of this on the PI directly.

      The steps I have taken to get this working:

      Set up the tools

      1. Facial Recognition Tools

      on the github page click download as zip
      extract zip and remove -master from the end of the folder. Note: its the internal folder that you want with the same name.
      copy to MagicMirror/modules using your FTP or whatever folder browser you are using.

      run this:

      sudo apt-get update
      sudo apt-get install libopencv-dev python-opencv
      sudo npm install python-shell
      sudo apt-get install python-pip
      

      change to the tools module folder

      cd /MagicMirror/modules/MMM-Facial-Recognition-Tools
      

      run

      sudo npm install
      
      pip install -r requirements.txt
      

      Capturing training images

      Run

      python capture.py
      

      Decide whether you want to capture images from your web cam or convert existing .jpg images.
      Enter the name of the person you are about to capture. Images will be stored in a folder named after the captured person in training_data/.
      Follow screen instructions.

      Training model

      Make sure you have captured all your images. Run ``` python train.py ``` The script will automatically scan the directory training_data/ for your images. Wait. You will end up with a training.xml file in the current directory. Copy down the ['name', 'name2','name3'] part because you will later need it for setting up your mirror's face recognition and to test your face recognition model.
      1. Test your steps so far

      Make sure your training.xml from running train.py is in this directory - /MagicMirror/modules/MMM-Facial-Recognition-Tools

      specify the face recognition algorithm in the environment with

      export FACE_ALGORITHM=1
      

      specify your user labels in the environment with

      export FACE_USERS=Alice,Bob,Casey,Doug
      

      Run

      python facerecognition.py.
      

      Success?

      1. Set up the module for the mirror
        on the github page for the MMM-Facial-Recognition page, click download as zip
        extract zip and remove -master from the end of the folder. Note: its the internal folder that you want with the same name.
        copy to MagicMirror/modules using your FTP or whatever folder browser you are using.

      Visit the terminal window and go to your newly create folder - /MagicMirror/Modules/MMM-Facial-Recognition and run

      sudo npm install
      

      Open your config.js file and add:

      {
      	module: 'MMM-Facial-Recognition',
      	config: {
      		// 1=LBPH | 2=Fisher | 3=Eigen
      		recognitionAlgorithm: 1,
      		// Threshold for the confidence of a recognized face before it's considered a
      		// positive match.  Confidence values below this threshold will be considered
      		// a positive match because the lower the confidence value, or distance, the
      		// more confident the algorithm is that the face was correctly detected.
      		lbphThreshold: 50,
      		fisherThreshold: 250,
      		eigenThreshold: 3000,
      		// force the use of a usb webcam on raspberry pi (on other platforms this is always true automatically)
      		useUSBCam: false,
      		// Path to your training xml
      		trainingFile: 'modules/MMM-Facial-Recognition/training.xml',
      		// recognition intervall in seconds (smaller number = faster but CPU intens!)
      		interval: 2,
      		// Logout delay after last recognition so that a user does not get instantly logged out if he turns away from the mirror for a few seconds
      		logoutDelay: 15,
      		// Array with usernames (copy and paste from training script)
      		users: [],
      		//Module set used for strangers and if no user is detected
      		defaultClass: "default",
      		//Set of modules which should be shown for every user
      		everyoneClass: "everyone",
      		// Boolean to toggle welcomeMessage
      		welcomeMessage: true
      	}
      },
      

      Change the line to the actual location. It’s probably still under the Tools Module

      trainingFile: 'modules/MMM-Facial-Recognition/training.xml',
      

      for me, I changed it to

      trainingFile: 'modules/MMM-Facial-Recognition-Tools/training.xml',
      

      Also, change the line called Users to add you list of users that you set up during the Capture.py section above.

      users: ['Joe'],
      

      Finally, change the line, if applicable

      useUSBCam: True,
      

      FINAL STEP!

      For each module in your config.js, set the profile under which it should load. E.g -
      {
      	module: 'example_module',
      	position: 'top_left',
      	//Set your classes here seperated by a space.
      	//Shown for all users
      	classes: 'default everyone',
      },
      {
      	module: 'example_module2',
      	position: 'top_left',
      	//Set your classes here seperated by a space.
      	//Shown for all users
      	classes: 'Joe',
      },
      

      So now the first example module will load for everyone and the second will only load for Joe.


      THIS IS AS FAR AS I AM .. I AM DOCUMENTING AS I GO

      @paviro
      Module Developer - Please feel free to copy this or use as you see fit. I hope it is helpful to anyone else

      M A 2 Replies Last reply Reply Quote 5
      • K Offline
        konradwalsh
        last edited by

        reserved

        1 Reply Last reply Reply Quote 0
        • M Offline
          MichiMauch @konradwalsh
          last edited by

          Hi @konradwalsh Maybe you have an idea for my problem: https://forum.magicmirror.builders/topic/9055/usb-cam-is-not-recognized-but-works

          thanks very much and greeetings
          michael

          M 1 Reply Last reply Reply Quote 0
          • M Offline
            MichiMauch @MichiMauch
            last edited by

            @michimauch said in Facial Recognition Module - Step by Step Guide:

            Hi @konradwalsh Maybe you have an idea for my problem: https://forum.magicmirror.builders/topic/9055/usb-cam-is-not-recognized-but-works

            thanks very much and greeetings
            michael

            Remember to set the name list environment variable FACE_USERS
            Using default FACE_ALGORITM: 1
            Loading training data...
            mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
            mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
            Exception in thread Thread-1:
            Traceback (most recent call last):
              File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
                self.run()
              File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-Tools/lib/picam.py", line 27, in run
                with picamera.PiCamera() as camera:
              File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 431, in __init__
                self._init_camera(camera_num, stereo_mode, stereo_decimate)
              File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 460, in _init_camera
                "Camera is not enabled. Try running 'sudo raspi-config' "
            PiCameraError: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.
            
            Training data loaded!
            OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-U1UwfN/opencv-2.4.9.1+dfsg1/modules/imgproc/src/color.cpp, line 3737
            Traceback (most recent call last):
              File "facerecognition.py", line 58, in <module>
                image = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
            cv2.error: /build/opencv-U1UwfN/opencv-2.4.9.1+dfsg1/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor
            
            I 1 Reply Last reply Reply Quote 0
            • P Offline
              paulGu
              last edited by

              @konradwalsh said in Facial Recognition Module - Step by Step Guide:

              trainingFile: ‘modules/MMM-Facial-Recognition/training.xml’,

              I got a issue when i add the facial module. the Facial-tools work fine, it’s seems like python script issues.

              Could anyone help me with this issue ?

              Whoops! There was an uncaught exception…
              TypeError: PythonShell is not a constructor
              at Class.python_start (/home/csc/MagicMirror/modules/MMM-Facial-Recognition/node_helper.js:11:21)
              at Class.socketNotificationReceived (/home/csc/MagicMirror/modules/MMM-Facial-Recognition/node_helper.js:40:14)
              at Socket. (/home/csc/MagicMirror/modules/node_modules/node_helper/index.js:113:11)
              at Socket.emit (events.js:182:13)
              at /home/csc/MagicMirror/node_modules/socket.io/lib/socket.js:528:12
              at process._tickCallback (internal/process/next_tick.js:61:11)
              MagicMirror will not quit, but it might be a good idea to check why this happened. Maybe no internet connection?
              If you think this really is an issue, please open an issue on GitHub: https://github.com/MichMich/MagicMirror/issues
              ^CShutting down server…

              S 1 Reply Last reply Reply Quote 1
              • S Offline
                sumrag @paulGu
                last edited by

                @paulgu Hi I am having the same issue. Were you able to resolve it?

                1 Reply Last reply Reply Quote 0
                • I Offline
                  Irfan ali
                  last edited by

                  I also have same issue can anybody help me

                  Whoops! There was an uncaught exception…
                  TypeError: PythonShell is not a constructor
                  at Class.python_start (/home/pi/MagicMirror/modules/MMM-Facial-Recognition/node_helper.js:11:21)
                  at Class.socketNotificationReceived (/home/pi/MagicMirror/modules/MMM-Facial-Recognition/node_helper.js:40:14)

                  1 Reply Last reply Reply Quote 0
                  • I Offline
                    Irfan ali @MichiMauch
                    last edited by

                    @MichiMauch Are You using Webcam ?

                    A 1 Reply Last reply Reply Quote 0
                    • N Offline
                      naktah
                      last edited by

                      specify your user labels in the environment with

                      export FACE_USERS=Alice,Bob,Casey,Doug

                      im not sure what to do with this. do i have to write this in the terminal? because everytime i restart my pi, the facial recognition doesnt recognize my face

                      S 1 Reply Last reply Reply Quote 0
                      • S Offline
                        sdetweil @naktah
                        last edited by

                        @naktah u need to put that in your logon profile

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        N 1 Reply Last reply Reply Quote 0
                        • N Offline
                          naktah @sdetweil
                          last edited by

                          @sdetweil im not sure how to do that. can you show me how? im pretty much new to this stuff and this issue has been bothering me for days now.

                          1 Reply Last reply Reply Quote 0
                          • S Offline
                            Sneek
                            last edited by

                            @naktah after writing the following command in the terminal:

                            python tools.train.py
                            

                            you get some output with the users you have setup (remember the order). Assuming it gives you Alice,Bob,Casey,Doug, then you run this in the terminal:

                            export FACE_USERS=Alice,Bob,Casey,Doug
                            
                            
                            1 Reply Last reply Reply Quote 0
                            • A Offline
                              andrewgamalkamel @Irfan ali
                              last edited by

                              @Irfan-ali yes!

                              1 Reply Last reply Reply Quote 0
                              • S Offline
                                SdeGeata
                                last edited by

                                Error on “python capture.py”

                                pi@raspberrypi:~/MagicMirror/modules/MMM-Facial-Recognition-Tools $ python capture.py
                                Traceback (most recent call last):
                                  File "capture.py", line 18, in <module>
                                    import lib.capture as capture
                                  File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-Tools/lib/capture.py", line 21, in <module>
                                    from . import config
                                  File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-Tools/lib/config.py", line 15, in <module>
                                    (CV_MAJOR_VER, CV_MINOR_VER, mv1, mv2) = cv2.__version__.split(".")
                                ValueError: need more than 3 values to unpack
                                
                                
                                A 1 Reply Last reply Reply Quote 0
                                • A Offline
                                  aniketdongare07 @SdeGeata
                                  last edited by

                                  @SdeGeata Got the same error, did you resolve it?

                                  S 1 Reply Last reply Reply Quote 0
                                  • S Offline
                                    sdetweil @aniketdongare07
                                    last edited by sdetweil

                                    @aniketdongare07 @SdeGeata the newer version of python only provide 3 version info strings. So that code needs to drop the 4th parm from the parse request

                                    (CV_MAJOR_VER, CV_MINOR_VER, mv1) = cv2.
                                    

                                    Sam

                                    How to add modules

                                    learning how to use browser developers window for css changes

                                    A M 2 Replies Last reply Reply Quote 0
                                    • A Offline
                                      aniketdongare07 @sdetweil
                                      last edited by

                                      @sdetweil I tried to do what you said and in turn I got the following error:```
                                      Traceback (most recent call last):
                                      File “capture.py”, line 18, in
                                      import lib.capture as capture
                                      File “/home/pi/MagicMirror/modules/MMM-Facial-Recognition-Tools/lib/capture.py”, line 21, in
                                      from . import config
                                      File “/home/pi/MagicMirror/modules/MMM-Facial-Recognition-Tools/lib/config.py”, line 15, in
                                      (CV_MAJOR_VER, CV_MINOR_VER, mv1) = cv2
                                      TypeError: ‘module’ object is not iterable

                                      S 1 Reply Last reply Reply Quote 0
                                      • S Offline
                                        sdetweil @aniketdongare07
                                        last edited by sdetweil

                                        @aniketdongare07 you need the rest of the line cv2… I am on my phone, so cut/paste is a pain

                                        (CV_MAJOR_VER, CV_MINOR_VER, mv1) = cv2.__version__.split(".")
                                        

                                        Sam

                                        How to add modules

                                        learning how to use browser developers window for css changes

                                        O 1 Reply Last reply Reply Quote 0
                                        • A Offline
                                          amsuvarna27 @konradwalsh
                                          last edited by

                                          @konradwalsh
                                          I’m using Raspberry pi CSI camera.
                                          How to do with it.
                                          Please help me.

                                          1 Reply Last reply Reply Quote 0
                                          • O Offline
                                            OGElijah @sdetweil
                                            last edited by

                                            @sdetweil Hey, buddy… I’m not sure what it is you’re telling me to do here. I wrote what you sent into the terminal but it didn’t do anything. In fact, it’s treating the = sign as a syntax error… what am I doing wrong?

                                            S 1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post
                                            Enjoying MagicMirror? Please consider a donation!
                                            MagicMirror created by Michael Teeuw.
                                            Forum managed by Sam, technical setup by Karsten.
                                            This forum is using NodeBB as its core | Contributors
                                            Contact | Privacy Policy