裸机Ubuntu16.04配置Dlib19.4 (Python人脸检测例子)

Windows版本Dlib配置链接:  地址

1.准备工作

Dlib库下载链接: 地址


首先需要Cmake以及编译C++成python程序的工具【参考1

  1. sudo apt-get install libboost-python-dev cmake  


如果没有setuptools工具的需要安装,指令如下:

python2.x:

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python


python3.x

wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python3.4

2.安装dlib

接着cd到Dlib的目录运行,参考:地址

运行:(如果提示没权限记得 sudo su)

  1. python setup.py install  


3.测试

  1. python  
  2. import dlib  
无任何异常说明安装成功~


4.运行人脸检测

下面来运行经典的Dlib的landmark~


先安装下pip,参考链接:地址

  1. sudo apt-get install python-pip python-dev build-essential   
  2. sudo pip install --upgrade pip   
  3. sudo pip install --upgrade virtualenv   

然后安装skimage.io模块,参考: 地址
  1. sudo pip install scikit-image  
或者
  1. sudo apt-get install python-skimage  


人脸检测


新建fr.py

[python] view plain copy
  1. #!/usr/bin/python  
  2. # The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt  
  3. #  
  4. #   This example program shows how to find frontal human faces in an image.  In  
  5. #   particular, it shows how you can take a list of images from the command  
  6. #   line and display each on the screen with red boxes overlaid on each human  
  7. #   face.  
  8. #  
  9. #   The examples/faces folder contains some jpg images of people.  You can run  
  10. #   this program on them and see the detections by executing the  
  11. #   following command:  
  12. #       ./face_detector.py ../examples/faces/*.jpg  
  13. #  
  14. #   This face detector is made using the now classic Histogram of Oriented  
  15. #   Gradients (HOG) feature combined with a linear classifier, an image  
  16. #   pyramid, and sliding window detection scheme.  This type of object detector  
  17. #   is fairly general and capable of detecting many types of semi-rigid objects  
  18. #   in addition to human faces.  Therefore, if you are interested in making  
  19. #   your own object detectors then read the train_object_detector.py example  
  20. #   program.    
  21. #  
  22. #  
  23. # COMPILING/INSTALLING THE DLIB PYTHON INTERFACE  
  24. #   You can install dlib using the command:  
  25. #       pip install dlib  
  26. #  
  27. #   Alternatively, if you want to compile dlib yourself then go into the dlib  
  28. #   root folder and run:  
  29. #       python setup.py install  
  30. #   or  
  31. #       python setup.py install --yes USE_AVX_INSTRUCTIONS  
  32. #   if you have a CPU that supports AVX instructions, since this makes some  
  33. #   things run faster.    
  34. #  
  35. #   Compiling dlib should work on any operating system so long as you have  
  36. #   CMake and boost-python installed.  On Ubuntu, this can be done easily by  
  37. #   running the command:  
  38. #       sudo apt-get install libboost-python-dev cmake  
  39. #  
  40. #   Also note that this example requires scikit-image which can be installed  
  41. #   via the command:  
  42. #       pip install scikit-image  
  43. #   Or downloaded from http://scikit-image.org/download.html.   
  44.   
  45. import sys  
  46.   
  47. import dlib  
  48. from skimage import io  
  49.   
  50.   
  51. detector = dlib.get_frontal_face_detector()  
  52. win = dlib.image_window()  
  53.   
  54. for f in sys.argv[1:]:  
  55.     print("Processing file: {}".format(f))  
  56.     img = io.imread(f)  
  57.     # The 1 in the second argument indicates that we should upsample the image  
  58.     # 1 time.  This will make everything bigger and allow us to detect more  
  59.     # faces.  
  60.     dets = detector(img, 1)  
  61.     print("Number of faces detected: {}".format(len(dets)))  
  62.     for i, d in enumerate(dets):  
  63.         print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(  
  64.             i, d.left(), d.top(), d.right(), d.bottom()))  
  65.   
  66.     win.clear_overlay()  
  67.     win.set_image(img)  
  68.     win.add_overlay(dets)  
  69.     dlib.hit_enter_to_continue()  
  70.   
  71.   
  72. # Finally, if you really want to you can ask the detector to tell you the score  
  73. # for each detection.  The score is bigger for more confident detections.  
  74. # The third argument to run is an optional adjustment to the detection threshold,  
  75. # where a negative value will return more detections and a positive value fewer.  
  76. # Also, the idx tells you which of the face sub-detectors matched.  This can be  
  77. # used to broadly identify faces in different orientations.  
  78. if (len(sys.argv[1:]) > 0):  
  79.     img = io.imread(sys.argv[1])  
  80.     dets, scores, idx = detector.run(img, 1, -1)  
  81.     for i, d in enumerate(dets):  
  82.         print("Detection {}, score: {}, face_type:{}".format(  
  83.             d, scores[i], idx[i]))  

运行:
  1. python fr.py 1.jpeg  

效果如图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值