nischi / MMM-Face-Reco-DNN

Face recognition with opencv and deep neural network
MIT License
91 stars 46 forks source link

cv2 module installed in a virtenv #5

Closed ivanovd closed 5 years ago

ivanovd commented 5 years ago

I have followed the instructions for compiling and building the OpenCV 4.1, but I did it as per instruction in a virtual enviornment, however MagicMirror is started outside of that virtual enviornment and cv2 is not there and the error that I am getting is:

tail -50  /home/pi/.pm2/logs/mm-error.log
        response: [Circular],
        originalHost: 'api.spotify.com',
        originalHostHeaderName: 'host',
        responseContent: [Circular],
        _destdata: true,
        _ended: true,
        _callbackCalled: true },
     toJSON: [Function: responseToJSON],
     caseless: Caseless { dict: [Object] },
     body: { error: [Object] } } }
ATTENTION: default value of option force_s3tc_enable overridden by environment.
(node:7628) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
SyntaxError: Unexpected token T in JSON at position 0
    at JSON.parse (<anonymous>)
    at PythonShell.asJson (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:358:21)
    at /home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:310:42
    at Array.forEach (<anonymous>)
    at PythonShell.recieveInternal (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:306:15)
    at PythonShell.receiveStderr (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:290:21)
    at Socket.<anonymous> (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:108:18)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:279:12)
    at readableAddChunk (_stream_readable.js:260:13)
{ Error: ImportError: No module named 'cv2'
    at PythonShell.parseError (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:246:21)
    at terminateIfNeeded (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:129:32)
    at ChildProcess.<anonymous> (/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/node_modules/python-shell/index.js:121:13)
    at ChildProcess.emit (events.js:182:13)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:237:12)
    ----- Python Traceback -----
    File "modules/MMM-Face-Reco-DNN/tools/facerecognition.py", line 5, in <module>
      from stream import VideoStream
    File "/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/tools/stream.py", line 2, in <module>
      from webcam import WebcamVideoStream
    File "/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/tools/webcam.py", line 3, in <module>
      import cv2
  traceback:
   'Traceback (most recent call last):\n  File "modules/MMM-Face-Reco-DNN/tools/facerecognition.py", line 5, in <module>\n    from stream import VideoStream\n  File "/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/tools/stream.py", line 2, in <module>\n    from webcam import WebcamVideoStream\n  File "/home/pi/MagicMirror/modules/MMM-Face-Reco-DNN/tools/webcam.py", line 3, in <module>\n    import cv2\nImportError: No module named \'cv2\'\n',
  executable: 'python3',
  options: null,
  script: 'modules/MMM-Face-Reco-DNN/tools/facerecognition.py',
  args:
   [ '--cascade=modules/MMM-Face-Reco-DNN/tools/haarcascade_frontalface_default.xml',
     '--encodings=modules/MMM-Face-Reco-DNN/tools/encodings.pickle',
     '--usePiCamera=0',
     '--method=dnn',
     '--detectionMethod=hog',
     '--interval=2000',
     '--output=0' ],
  exitCode: 1 }

Can I take "out" cv2 outside of the virtual enviornment or somehow make MagicMirror use that inside of the virt env?

ivanovd commented 5 years ago
pi@raspberrypi:~ $ workon cv
(cv) pi@raspberrypi:~ $ python
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
ivanovd commented 5 years ago

Hmm, although my main Python installation is on 2.7 cv2 module seems to be working outside of the virt env:

pi@raspberrypi:~ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
>>> exit()

Any ideas why the above error is then?

nischi commented 5 years ago
pi@spiegel:~ $ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'

hmm looks the same to me.

Have you add every config in the module or do you use the default configuration? can you send me the exact config do you have? then i test that on my machine.

ivanovd commented 5 years ago

Here is the config of the module:

{
    module: 'MMM-Face-Reco-DNN',
    config: {
      // Logout 15 seconds after user was not detecte anymore, if they will be detected between this 15 Seconds, they delay will start again
      logoutDelay: 15000,
      // How many time the recognition starts, with a RasPi 3+ it would be good every 2 seconds
      checkInterval: 2000,
      // 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',
      // XML to recognize with haarcascae
      cascade: 'modules/MMM-Face-Reco-DNN/tools/haarcascade_frontalface_default.xml',
      // Pre encoded pickle with the faces
      encodings: 'modules/MMM-Face-Reco-DNN/tools/encodings.pickle',
      // You wanna use pi camera or usb / builtin (1 = raspi camera, 0 = other camera)
      usePiCamera: 0,
      // Method of face detection (dnn = deep neural network, haar = haarcascade)
      method: 'dnn',
      // Which face detection model to use. "hog" is less accurate but faster on CPUs. "cnn" is a more accurate deep-learning model which is GPU/CUDA accelerated (if available).
      detectionMethod: 'hog',
      // How fast in ms should the modules hide and show (face effect)
      animationSpeed: 0
    }
},

I use a PS3 Eye USB camera

ivanovd commented 5 years ago

I tried symlinking the virtenv module to the outside python3 but that didn't work. If you manage to replicate this and it is a bug then fine, otherwise do not waste your time, I started building OpenCV 4.1 and dlib on a new SD card, once I am ready I will post the results here.

nischi commented 5 years ago

seems python-shell use python3 instead of python. Can you run the same with python3

pi@spiegel:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.0'
ivanovd commented 5 years ago

I ran it with python3 and indeed the cv2 module was not linked outside of the virt env. I tried multiple solutions to link them and finally gave up and now I am recompiling and building OpenCV 4.1 on a fresh installation. I will report back when finished. I am really looking forward to testing this.

nischi commented 5 years ago

i read through the documentation of python-shell which i use. maybe it make sense to add a configuration that you can manually set the path to python. so you can use also the virtual env specialy for the face recognition

ivanovd commented 5 years ago

It would be great if you could do that. Otherwise somebody else who followed the tuttorial at https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/ might run into the same issue as me :)

Keep up the great work nischi

nischi commented 5 years ago

@ivanovd Can you test thew new version with the pythonPath options please? Can't really test it on my machine, but should work :)

Edit I have successfully testet it on my mac

ivanovd commented 5 years ago

I got it to work and WOW ... it is so much better, accurate and fast than Facial_Recognition_OCV3 ... I am really happy with the result.

Great job @nischi

nischi commented 5 years ago

Thanks a lot. If you change detectionMethod to cnn it will much more accurate, but it need power. And thats nothing for a pi. Maybe a pi 4, i need to buy one and test :)