pycharm/vscode 68点面部识别的import dlib ,cv2经验教训

 68点图像识别代码(原文链接):人脸识别Dlib——68个特征点

import dlib
import cv2
 
# 与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
detector = dlib.get_frontal_face_detector()
 
# 使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
# cv2读取图片
img = cv2.imread("kobe.jpeg")
 
# 与人脸检测程序相同,使用detector进行人脸检测 dets为返回的结果
dets = detector(img, 1)
# 使用enumerate 函数遍历序列中的元素以及它们的下标
# 下标k即为人脸序号
# left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
# top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for k, d in enumerate(dets):
    print("dets{}".format(d))
    print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        k, d.left(), d.top(), d.right(), d.bottom()))
 
    # 使用predictor进行人脸关键点识别 shape为返回的结果
    shape = predictor(img, d)
    # 获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
    print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
 
    # 绘制特征点
    for index, pt in enumerate(shape.parts()):
        print('Part {}: {}'.format(index, pt))
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 1, (255, 0, 0), 2)
        #利用cv2.putText输出1-68
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(index+1),pt_pos,font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)
 
cv2.imshow('img', img)
k = cv2.waitKey()
cv2.destroyAllWindows()


# 使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

至于这shape_predictor_68_face_landmarks.dat 在网上下载解压缩到当前文件夹即可。

最初 导入dlib    import dlib的时候

pip install dlib

pycharm/vscode 68点面部识别的import dlib ,cv2经验教训_第1张图片

 下面就是出问题的东西:

PS C:\Users\Kobe\Desktop\python> & C:/Users/Kobe/AppData/Local/Programs/Python/Python39/python.exe c:/Users/Kobe/Desktop/python/111.py
Traceback (most recent call last):
  File "c:\Users\Kobe\Desktop\python\111.py", line 50, in 
    import dlib
ModuleNotFoundError: No module named 'dlib'
PS C:\Users\Kobe\Desktop\python> pip install dlib
Collecting dlib
  Using cached dlib-19.24.0.tar.gz (3.2 MB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: dlib
  Building wheel for dlib (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [59 lines of output]
      running bdist_wheel
      running build
      running build_py
      package init file 'tools\python\dlib\__init__.py' not found (or not a regular file)
      running build_ext
      Building extension for Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
      Invoking CMake setup: 'cmake C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\tools\python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\build\lib.win-amd64-3.9 -DPYTHON_EXECUTABLE=C:\Users\Kobe\AppData\Local\Programs\Python\Python39\python.exe -DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\build\lib.win-amd64-3.9 -A x64'
      -- Building for: NMake Makefiles
      CMake Error at CMakeLists.txt:5 (message):
      
      
      
        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      
      
        You must use Visual Studio to build a python extension on windows.  If you
        are getting this error it means you have not installed Visual C++.  Note
        that there are many flavors of Visual Studio, like Visual Studio for C#
        development.  You need to install Visual Studio for C++.


        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!




      -- Configuring incomplete, errors occurred!
      Traceback (most recent call last):
        File "", line 2, in 
        File "", line 34, in 
        File "C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\setup.py", line 222, in 
          setup(
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\site-packages\setuptools\__init__.py", line 153, in setup
          return distutils.core.setup(**attrs)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\core.py", line 148, in setup
          dist.run_commands()
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 985, in run_command
          cmd_obj.run()
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\site-packages\wheel\bdist_wheel.py", line 299, in run
          self.run_command('build')
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 985, in run_command
          cmd_obj.run()
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\command\build.py", line 135, in run
          self.run_command(cmd_name)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 985, in run_command
          cmd_obj.run()
        File "C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\setup.py", line 134, in run
          self.build_extension(ext)
        File "C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\setup.py", line 171, in build_extension
          subprocess.check_call(cmake_setup, cwd=build_folder)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 373, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['cmake', 'C:\\Users\\Kobe\\AppData\\Local\\Temp\\pip-install-nu4_zfos\\dlib_fbfcc097f4ed4e9bade91ae853124c59\\tools\\python', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\\Users\\Kobe\\AppData\\Local\\Temp\\pip-install-nu4_zfos\\dlib_fbfcc097f4ed4e9bade91ae853124c59\\build\\lib.win-amd64-3.9', '-DPYTHON_EXECUTABLE=C:\\Users\\Kobe\\AppData\\Local\\Programs\\Python\\Python39\\python.exe', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\\Users\\Kobe\\AppData\\Local\\Temp\\pip-install-nu4_zfos\\dlib_fbfcc097f4ed4e9bade91ae853124c59\\build\\lib.win-amd64-3.9', '-A', 'x64']' returned non-zero exit status 1.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for dlib
  Running setup.py clean for dlib
Failed to build dlib
Installing collected packages: dlib
  Running setup.py install for dlib ... error
  error: subprocess-exited-with-error

  × Running setup.py install for dlib did not run successfully.
  │ exit code: 1
  ╰─> [61 lines of output]
      running install
      running build
      running build_py
      package init file 'tools\python\dlib\__init__.py' not found (or not a regular file)
      running build_ext
      Building extension for Python 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
      Invoking CMake setup: 'cmake C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\tools\python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\build\lib.win-amd64-3.9 -DPYTHON_EXECUTABLE=C:\Users\Kobe\AppData\Local\Programs\Python\Python39\python.exe -DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\build\lib.win-amd64-3.9 -A x64'
      -- Building for: NMake Makefiles
      CMake Error at CMakeLists.txt:5 (message):



        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


        You must use Visual Studio to build a python extension on windows.  If you
        are getting this error it means you have not installed Visual C++.  Note
        that there are many flavors of Visual Studio, like Visual Studio for C#
        development.  You need to install Visual Studio for C++.


        !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


          setup(
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\site-packages\setuptools\__init__.py", line 153, in setup
          return distutils.core.setup(**attrs)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\core.py", line 148, in setup
          dist.run_commands()
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 966, in run_commands
          self.run_command(cmd)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 985, in run_command
          cmd_obj.run()
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\site-packages\setuptools\command\install.py", line 61, in run
          return orig.install.run(self)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\command\install.py", line 546, in run
          self.run_command('build')
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 985, in run_command
          cmd_obj.run()
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\command\build.py", line 135, in run
          self.run_command(cmd_name)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\cmd.py", line 313, in run_command
          self.distribution.run_command(command)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\distutils\dist.py", line 985, in run_command
          cmd_obj.run()
        File "C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\setup.py", line 134, in run
          self.build_extension(ext)
        File "C:\Users\Kobe\AppData\Local\Temp\pip-install-nu4_zfos\dlib_fbfcc097f4ed4e9bade91ae853124c59\setup.py", line 171, in build_extension
          subprocess.check_call(cmake_setup, cwd=build_folder)
        File "C:\Users\Kobe\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 373, in check_call
          raise CalledProcessError(retcode, cmd)
      subprocess.CalledProcessError: Command '['cmake', 'C:\\Users\\Kobe\\AppData\\Local\\Temp\\pip-install-nu4_zfos\\dlib_fbfcc097f4ed4e9bade91ae853124c59\\tools\\python', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\\Users\\Kobe\\AppData\\Local\\Temp\\pip-install-nu4_zfos\\dlib_fbfcc097f4ed4e9bade91ae853124c59\\build\\lib.win-amd64-3.9', '-DPYTHON_EXECUTABLE=C:\\Users\\Kobe\\AppData\\Local\\Programs\\Python\\Python39\\python.exe', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE=C:\\Users\\Kobe\\AppData\\Local\\Temp\\pip-install-nu4_zfos\\dlib_fbfcc097f4ed4e9bade91ae853124c59\\build\\lib.win-amd64-3.9', '-A', 'x64']' returned non-zero exit status 1.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure
× Encountered error while trying to install package.╰─> dlib

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

以及对应的好多方法但是都没有解决问题,比如:

pycharm/vscode 68点面部识别的import dlib ,cv2经验教训_第2张图片

还有说:

                        其实是缺少Cmake包和CBoot包,安装这两个包即可
                        python -m pip install CMake -i https://pypi.tuna.tsinghua.edu.cn/simple/
                        python -m pip install Boost -i https://pypi.tuna.tsinghua.edu.cn/simple/
                        安装之后再安装dlib即可
                        python -m pip install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple/

基本上都没有解决在vscode上遇到的问题。

最后找到一句话(因为这句话在pycharm上直接运行即可,但是在vscode上面还是不行,而且用的python.exe用的是Anaconda里面的,可能里面有些包全一些)

conda install -c conda-forge dlib

但是还是有问题

 这俩链接配合着使用即可(注意要是第一个链接就可以安装上conda,那就不需要第二个链接了)

vscode conda : 无法将“conda”项识别为 cmdlet、函数、脚本文件或可运行程序的名称--解决方案

The current user does not have write permissions to the target envi ronment.

基本上dlib安装不上的原因也解决差不多了。

下面就是import cv2的问题。

import cv2出错ModuleNotFoundError: No module named ‘cv2’

pip install opencv-python

 还是不行,看下面一个文章即可:解决安装opencv-python后ImportError: No module named cv2错误

我在Anaconda的文件夹里:D:\date\Anaconda\Lib\site-packages 没找到CV的文件夹,也就是说我的pip install opencv-python这个命令尽管执行了,但是没有加到Anaconda这里面的目录里,所以我要安装到特定目录下。

pip install --target='D:/date/Anaconda/Lib/site-packages ' opencv-python

现在是添加到Anaconda目录下面了,所以成功

之后做了一个基本的给人脸部分位置画框的东西:

import dlib
import cv2

# 与人脸检测相同,使用dlib自带的frontal_face_detector作为人脸检测器
detector = dlib.get_frontal_face_detector()

# 使用官方提供的模型构建特征提取器
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# cv2读取图片
img = cv2.imread("kobe.jpg")

# 与人脸检测程序相同,使用detector进行人脸检测 dets为返回的结果
dets = detector(img)
# 使用enumerate 函数遍历序列中的元素以及它们的下标
# 下标k即为人脸序号
# left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
# top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for k, d in enumerate(dets):
    print("dets{}".format(d))
    print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        k, d.left(), d.top(), d.right(), d.bottom()))

    # 使用predictor进行人脸关键点识别 shape为返回的结果
    shape = predictor(img,d)  # d 是 开始内部人脸检测的边界框的位置信息
    # 获取第一个和第二个点的坐标(相对于图片而不是框出来的人脸)
    print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))  # shape.part(1)跟shape.parts(1)区别?
    print()

    # 绘制特征点
    for index, pt in enumerate(shape.parts()):  # index就是第几个点的位置?
        print('Part {}: {}'.format(index + 1, pt))
        pt_pos = (pt.x, pt.y)
        cv2.circle(img, pt_pos, 1, (255, 0, 0),2)  # 画圈 cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]])

        # 利用cv2.putText输出1-68
        font = cv2.FONT_HERSHEY_SIMPLEX  # 字体选择
        cv2.putText(img, str(index + 1), pt_pos, font, 0.3, (0, 0, 255), 1, cv2.LINE_AA)

    # print("\n\n\n\n")
    # minX = shape.part(17)
    # minY = shape.part(17)
    # maxX = shape.part(17)
    # maxY = shape.part(17)
    # for location, pt in enumerate(shape.parts()):
    #     if 18<=location  & location<22:
    #         if pt.x<=minX.x:
    #             minX = pt
    #         if pt.x>=maxX.x:
    #             maxX = pt
    #         if pt.y<=minY.y:
    #             minY = pt
    #         if pt.y>=maxY.y:
    #             maxY = pt
    # print(minX.x,maxX.x,minY.y,maxY.y)
    minX = shape.part(0).x-2
    minY = shape.part(19).y-1
    maxX = shape.part(28).x+1
    maxY = shape.part(40).y+12
    cv2.rectangle(img, (minX,minY), (maxX, maxY), (0, 255, 0), 1)
    cv2.rectangle(img, (shape.part(1).x-1,shape.part(40).y+12), (shape.part(51).x+1, shape.part(51).y), (0, 255, 0), 1)
    cv2.rectangle(img, (shape.part(4).x-1,shape.part(51).y), (shape.part(57).x+1, shape.part(57).y+1), (0, 255, 0), 1)
# 18-22(左眉毛)  37-42(左眼睛)  23-27(右眉毛) 43-48(右眼) 28-36(鼻子)   49-68 (嘴巴)


cv2.imshow('img', img)
k = cv2.waitKey()
cv2.destroyAllWindows()

pycharm/vscode 68点面部识别的import dlib ,cv2经验教训_第3张图片

 

总结:因为在pycharm上使用的很快,基本上没有什么问题,所有的问题大部分都在vscode上,所以以上的操作都是在VScode上的。遇到的问题基本上就是这些了。

你可能感兴趣的:(pycharm,ide,python,conda,vscode)