0

I am writing a Blender addon for working with 3D face scans. I want to find landmarks (the eyebrows, the corners of the eyes, the mouth line, etc.) in an image of the scanned face so I can map them to the model. The Dlib library is ideal for finding these landmarks, but I have been unable to install it in Blender's Python (so I can call it from the addon).

I had no problem installing Dlib in a standalone Python (3.6.4) version I also have installed. As a work around I am currently calling this Python from a subprocess in the addon, but this is very slow and makes the addon difficult to install on another computer.

Here is the output when I try to install Dlib with pip in Blender's Python (Blender is version 2.81 with Python 3.7.4):

C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\bin> ./python -m pip install dlib --user
Collecting dlib
  Using cached dlib-19.19.0.tar.gz (3.2 MB)
Installing collected packages: dlib
    Running setup.py install for dlib ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\bin\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\setup.py'"'"'; __file__='"'"'C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\David\AppData\Local\Temp\pip-record-k5d8lob9\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\David\AppData\Roaming\Python\Python37\Include\dlib'
         cwd: C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\
    Complete output (66 lines):
    running install
    running build
    running build_py
    package init file 'dlib\__init__.py' not found (or not a regular file)
    running build_ext
    Building extension for Python 3.7.4 (default, Aug 13 2019, 14:10:15) [MSC v.1916 64 bit (AMD64)]
    Invoking CMake setup: 'cmake C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\tools\python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\build\lib.win-amd64-3.7 -DPYTHON_EXECUTABLE=C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\bin\python.exe -DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\build\lib.win-amd64-3.7 -A x64'
    -- Building for: Visual Studio 15 2017
    -- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
    -- The C compiler identification is MSVC 19.16.27030.1
    -- The CXX compiler identification is MSVC 19.16.27030.1
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
    -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
    -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Found PythonInterp: C:/Program Files/Blender Foundation/Blender 2.81/2.81/python/bin/python.exe (found version "3.7.4")
    CMake Error at C:/Users/David/AppData/Local/Temp/pip-install-x18bl7c_/dlib/dlib/external/pybind11/tools/FindPythonLibsNew.cmake:158 (message):
      Python libraries not found
    Call Stack (most recent call first):
      C:/Users/David/AppData/Local/Temp/pip-install-x18bl7c_/dlib/dlib/external/pybind11/tools/pybind11Tools.cmake:16 (find_package)
      C:/Users/David/AppData/Local/Temp/pip-install-x18bl7c_/dlib/dlib/external/pybind11/CMakeLists.txt:33 (include)


    -- Configuring incomplete, errors occurred!
    See also "C:/Users/David/AppData/Local/Temp/pip-install-x18bl7c_/dlib/build/temp.win-amd64-3.7/Release/CMakeFiles/CMakeOutput.log".
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\setup.py", line 261, in <module>
        'Topic :: Software Development',
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\site-packages\setuptools\__init__.py", line 145, in setup
        return distutils.core.setup(**attrs)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\core.py", line 148, in setup
        dist.run_commands()
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\dist.py", line 966, in run_commands
        self.run_command(cmd)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\site-packages\setuptools\command\install.py", line 61, in run
        return orig.install.run(self)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\command\install.py", line 545, in run
        self.run_command('build')
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\command\build.py", line 135, in run
        self.run_command(cmd_name)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\distutils\dist.py", line 985, in run_command
        cmd_obj.run()
      File "C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\setup.py", line 135, in run
        self.build_extension(ext)
      File "C:\Users\David\AppData\Local\Temp\pip-install-x18bl7c_\dlib\setup.py", line 172, in build_extension
        subprocess.check_call(cmake_setup, cwd=build_folder)
      File "C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\lib\subprocess.py", line 347, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['cmake', 'C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\tools\\python', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\build\\lib.win-amd64-3.7', '-DPYTHON_EXECUTABLE=C:\\Program Files\\Blender Foundation\\Blender 2.81\\2.81\\python\\bin\\python.exe', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\build\\lib.win-amd64-3.7', '-A', 'x64']' returned non-zero exit status 1.
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:\Program Files\Blender Foundation\Blender 2.81\2.81\python\bin\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\setup.py'"'"'; __file__='"'"'C:\\Users\\David\\AppData\\Local\\Temp\\pip-install-x18bl7c_\\dlib\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\David\AppData\Local\Temp\pip-record-k5d8lob9\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\David\AppData\Roaming\Python\Python37\Include\dlib' Check the logs for full command output.

While I would like to use Dlib in Blender, I am open to other methods of finding the facial landmarks in an image. Either way, I need another user of the addon (or the addon itself) to be able to install the required libraries without too much work.

In case it is helpful, here is a video demonstrating using Dlib in Blender.

Please let me know if I can clarify anything.

Thanks, David

  • I have recently done `headpose estimation` using dlib landmarks on `blender 2.82`. You can look into this setup method if it helps, https://stackoverflow.com/questions/60529676/can-i-import-tensorflow-and-keras-in-maya-blender/60530736#60530736. – B200011011 Apr 29 '20 at 03:12

2 Answers2

0

I encounter the same issue while trying to do some real-time face rig using blender, laptop webcam and open cv following tutorial made by Gadget Workbench (that you can find here : https://www.youtube.com/watch?v=O7nNO3FLkLU ). Here is my solution.

--> As mentionned in the Blender documentation, you shouldn't use different version of python to use modules

The Python (major, minor) version must match the one that Blender comes with. Therefor you can’t use Python 3.6 with Blender built to use Python 3.7.

They also mentionned workarounds if you need to use your OS python or add some extensions. (doc is here). You could also use Anaconda instead of a classic install of Python, to be able to switch quickly your python main version. Once you're sure you have the same python OS version than Blender packed python version, you're good to go :)

I managed to fix this error, using only the python packed with blender, by updating pip.

1- First, run the pip update command (once you're in the python folder inside your Blender installation directory):

.\python -m pip install --upgrade pip

2- Then you can install dlib

.\python -m pip install dlib

3- To verify that everything is OK, you can open your blender project, go inside the Script tab (or simply open a console pannel wherever you want in Blender) and try typing

import dlib

It should no longer raise errors inside Blender, or inside your OS console.

Ruli
  • 2,592
  • 12
  • 30
  • 40
B.Pader
  • 101
  • 2
  • Thanks! I suspect it may have been something with the current version of Dlib when I tried, since updating pip fixed it for you. I ended up creating a [C++ Python extension](https://docs.python.org/3/extending/extending.html) that internally used Dlib. – David Brock Nov 11 '20 at 19:42
0

I would suggest running Dlib in ordinary python and storing the output co-ordinates in some other txt, csv or some image files. Then import OS module in blender-python's script and read the files made by ordinary python. In this way, you can communicate between ordinary python and blender's python. Another option is to use sys module. import sys module in blender's py script and pass arguments while invoking blender-python script from command line. Alternatively, you can also use blender's python from ordinary python. Import subprocess module in ordinary python and run your py script as subprocess. It is similar to running a script from terminal or windows cmd. Blender's python comes with basic modules like sys or OS pre-installed. You can use them to communicate with ordinary python. You would be able to gain advantages from both different versions of python.

YadneshD
  • 396
  • 2
  • 12