openface+conda python.10 排坑记录
任务
发现openface好像还有一个python版本的,跑起来试试。
注意
我希望把博客写得既能帮自己从实践中总结经验,又能帮助做同样事情的同学快速找到参考。如果你有任何关于博客内容和排版的建议,请在评论区告诉我,谢谢~
成功流程
按照参考博客来。
- 我的系统是20.04,新建了一个python3.10的conda环境;
conda create -n OPENFACE python=3.10
conda activate OPENFACE
- 手动安装requirements.txt的python依赖,不要根据他的版本要求自动下载,大概率出错,我直接无视版本安装没问题。中间缺什么依赖安装什么依赖。
- 根据setup.py安装openface
- 打开python环境尝试import openface,把缺失的依赖安装好
- 在openface目录下复制上述博客的代码,做少许改动,然后运行。
踩坑回顾
1. requirements依赖错误
症状描述
执行:
pip3 install -r requirements.txt
报错:
Collecting pandas<0.18,>=0.13 (from -r requirements.txt (line 3))
Using cached pandas-0.17.1.zip (7.7 MB)
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [15 lines of output]
/tmp/pip-install-wc34lmil/pandas_25f6faa563c9402db75b67e3a03f6b26/setup.py:30: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
import pkg_resources
/home/lcy-magic/anaconda3/envs/OPENFACE/lib/python3.10/site-packages/setuptools/__init__.py:80: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
!!
********************************************************************************
Requirements should be satisfied by a PEP 517 installer.
If you are using pip, you can try `pip install --use-pep517`.
********************************************************************************
!!
dist.fetch_build_eggs(dist.setup_requires)
error in pandas setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Expected end or semicolon (after version specifier)
pytz >= 2011k
~~~~~~~^
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
解决办法
尝试升级pip
pip3 install --upgrade pip
还是同样报错。
因为报错建议使用-use-pep517,是一种不同于setuptools的新的包构建方式。所以估计还是包的依赖问题。先尝试升级setuptools:
pip3 install --upgrade setuptools
报错:
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
rosdep 0.22.2 requires catkin-pkg>=0.4.0, which is not installed.
rosdep 0.22.2 requires PyYAML>=3.1, which is not installed.
rosdistro 0.9.0 requires catkin-pkg, which is not installed.
rosdistro 0.9.0 requires PyYAML, which is not installed.
rosinstall 0.7.8 requires catkin-pkg, which is not installed.
rosinstall 0.7.8 requires pyyaml, which is not installed.
rosinstall-generator 0.1.23 requires catkin-pkg>=0.1.28, which is not installed.
rosinstall-generator 0.1.23 requires PyYAML, which is not installed.
Successfully installed setuptools-69.0.3
问题不大,成功安装了。只是缺少点依赖。全安装上就行了:
pip3 install catkin-pkg PyYAML rospkg
但还是同样报错。
那试试用报错建议的方法吧:
pip3 install --use-pep517 -r requirements.txt
结果还是不行,报错太长就不贴出来了,应该还是依赖问题。
按照参考博客和里面评论的建议做了,也还是不行。
然后按照参考博客指定setuptools版本,虽然pandas解决了,但后面的包还是有问题。但我当时用的是python3 -m pip install --upgrade pip
,但我用python3其实对应的是系统的解释器,我的conda的解释器要用python,改成python -m pip install --upgrade pip
说不定可以。反正我已经解决了,就不尝试了。
最后一气之下,决定不按照requirements的版本来了,直接都上最新版本:
pip3 install numpy scipy pandas scikit-learn nose nolearn
这样暂时都没问题,看看后面会不会出依赖问题吧。
2. 依赖缺失错误
症状描述
用setup.py安装openface:
python setup.py install
然后进入python,去import openface,会提示没有cv2和dlib。
解决办法
这都是小问题。直接安装上就行了:
pip3 install opencv-python dlib
成功:
3. 读不了摄像头
症状描述
我在openface目录下,复制了参考博客的程序,并运行,报错:
[ WARN:0@0.541] global cap_v4l.cpp:997 open VIDEOIO(V4L2:/dev/video1): can't open camera by index
[ERROR:0@0.660] global obsensor_uvc_stream_channel.cpp:159 getStreamChannelGroup Camera index out of range
解决方法
也是很简单问题,就是读取摄像头的索引是1,我电脑就一个摄像头,编号就是0,改一下就行。然后再加一条语句,可以调整窗口大小。修改后的代码如下:
import threading
import cv2
import os
import openface
fileDir = os.path.dirname(os.path.realpath(__file__))
modelDir = os.path.join(fileDir, 'models')
dlibModelDir = os.path.join(modelDir, 'dlib')
align = openface.AlignDlib(os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))
class OpcvCapture(threading.Thread):
def __init__(self, win_name, cam_name):
super().__init__()
self.cam_name = cam_name
self.win_name = win_name
# self.frame = np.zeros([400, 400, 3], np.uint8)
def run(self):
capture = cv2.VideoCapture(self.cam_name)
while (True):
# 获取一帧
ret, frame = capture.read()
# 获取的帧送入检测,绘制检测结果后返回,自拍模式做镜像
show_img = self._detector(frame, mirror=True)
cv2.namedWindow(self.win_name, cv2.WINDOW_NORMAL)
cv2.imshow(self.win_name, show_img)
cv2.waitKey(1)
def _detector(self, frame, mirror=False):
show_img = cv2.flip(frame, flipCode=1) if mirror else frame
rects = align.getAllFaceBoundingBoxes(show_img)
if len(rects) > 0:
bb = align.findLandmarks(show_img, rects[0])
for pt in bb:
cv2.circle(show_img, pt, 3, [0, 0, 255], thickness=-1)
return show_img
if __name__ == "__main__":
camera1 = OpcvCapture("camera1", 0)
camera1.start()
感谢源代码来源的分享,非常感谢。