learn opencv-Ubuntu安装Dlib

Ubuntu安装Dlib教程
本文提供了在Ubuntu上安装Dlib的详细步骤,包括操作系统库、Python库的安装及Dlib的编译。还介绍了如何使用虚拟环境管理和安装Python库。

参考:
https://github.com/spmallick/learnopencv


Ubuntu安装Dlib

在这篇文章中,我们将提供如何在Ubuntu上安装Dlib的分步说明。

第1步:安装操作系统库

sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libx11-dev libatlas-base-dev
sudo apt-get install libgtk-3-dev libboost-python-dev

第2步:安装Python库

sudo apt-get install python-dev python-pip python3-dev python3-pip
sudo -H pip2 install -U pip numpy
sudo -H pip3 install -U pip numpy

我们将使用虚拟环境来安装Python库。 为了将您的项目环境和全球环境分开,通常是一个很好的做法。

# Install virtual environment
sudo pip2 install virtualenv virtualenvwrapper
sudo pip3 install virtualenv virtualenvwrapper
echo "# Virtual Environment Wrapper" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

############ For Python 2 ############
# create virtual environment
mkvirtualenv facecourse-py2 -p python2
workon facecourse-py2

# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython

# quit virtual environment
deactivate
######################################

############ For Python 3 ############
# create virtual environment
mkvirtualenv facecourse-py3 -p python3
workon facecourse-py3

# now install python libraries within this virtual environment
pip install numpy scipy matplotlib scikit-image scikit-learn ipython

# quit virtual environment
deactivate
######################################

第3步:编译DLib

步骤3.1:编译C ++二进制文件

Dlib的创建者戴维斯·金(Davis King)建议在您的代码中使用CMake来使用Dlib。
但是,如果你想使用Dlib作为库,请按照下列步骤操作:

wget http://dlib.net/files/dlib-19.6.tar.bz2
tar xvf dlib-19.6.tar.bz2
cd dlib-19.6/
mkdir build
cd build
cmake ..
cmake --build . --config Release
sudo make install
sudo ldconfig
cd ..

现在你可以使用pkg-config来提供路径到Dlib的include目录和链接Dlib库文件。

pkg-config --libs --cflags dlib-1

第3.2步:编译Python模块

激活Python虚拟环境。

############ For Python 2 ############
workon facecourse-py2

############ For Python 3 ############
workon facecourse-py3

现在让我们编译并安装Dlib的Python模块。

# move to dlib's root directory
cd dlib-19.6
python setup.py install
# clean up(this step is required if you want to build dlib for both Python2 and Python3)
rm -rf dist
rm -rf tool/python/build
rm python_examples/dlib.so

我们已经清理了几个文件和目录,因为Dlib为Python2和Python3创建了具有相同名称的Python模块。假设你在Python2虚拟环境中运行了setup.py,它将在python_examples目录下生成dlib.so。现在,如果停用Python2虚拟环境,激活Python3虚拟环境并运行setup.py文件,它将用python_examples目录(用Python3编译)替换python_examples目录中的dlib.so(用Python2编译)。当你尝试在这个目录下运行任何python_example时,它会导入这个dlib.so,而不是位于site-packages或者dist-packages目录中的一个,并且抛出一个错误。虽然这个错误不会发生,但是dlib.so的本地副本在当前目录中不存在,但是最好删除本地副本以避免混淆。

为了保持一致性,我们使用相同的源代码安装了Dlib的Python和C ++二进制文件。

如果您只打算使用Dlib的Python模块,则还可以使用pip为Dlib安装Python绑定。

pip install dlib

现在你可以退出Python的虚拟环境。

deactivate

现在,无论何时要运行使用Dlib的Python脚本,都必须使用workon命令激活虚拟环境。

(venv) E:\pycharm\study\dorm_face_recognition\model_training>pip install dlib Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting dlib Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/f4/f8949b18ec1df2ef05fc2ea1d1dd82ff2d050b8704b7d0d088017315c221/dlib-20.0.0.tar.gz (3.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 6.6 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: dlib Building wheel for dlib (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for dlib (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [48 lines of output] running bdist_wheel running build running build_ext Traceback (most recent call last): File "E:\python3.9.13\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "E:\python3.9.13\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "E:\pycharm\study\venv\Scripts\cmake.exe\__main__.py", line 4, in <module> ModuleNotFoundError: No module named 'cmake' ================================================================================ ================================================================================ ================================================================================ CMake is not installed on your system! Or it is possible some broken copy of cmake is installed on your system. It is unfortunately very common for python package managers to include broken copies of cmake. So if the error above this refers to some file path to a cmake file inside a python or anaconda or miniconda path then you should delete that broken copy of cmake from your computer. Instead, please get an official copy of cmake from one of these known good sources of an official cmake: - cmake.org (this is how windows users should get cmake) - apt install cmake (for Ubuntu or Debian based systems) - yum install cmake (for Redhat or CenOS based systems) On a linux machine you can run `which cmake` to see what cmake you are actually using. If it tells you it's some cmake from any kind of python packager delete it and install an official cmake. More generally, cmake is not installed if when you open a terminal window and type cmake --version you get an error. So you can use that as a very basic test to see if you have cmake installed. That is, if cmake --version doesn't run from the same terminal window from which you are reading this error message, then you have not installed cmake. Windows users should take note that they need to tell the cmake installer to add cmake to their PATH. Since you can't run commands that are not in your PATH. This is how the PATH works on Linux as well, but failing to add cmake to the PATH is a particularly common problem on windows and rarely a problem on Linux. ================================================================================ ================================================================================ ================================================================================ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Failed to build dlib ERROR: Could not build wheels for dlib, which is required to install pyproject.toml-based projects
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值