[关闭]
@Perfect-Demo 2018-12-17T01:30:55.000000Z 字数 1119 阅读 668

(1)人脸检测

opencv+dlib


这里本人用一整个文集模块来记录对opencv以及dlib的学习过程,然后尤其要感谢一位CSND 博主。
对这两个库的学习我将主要参考:
1. opencv3 计算机视觉python语言实现 这本书
2. 博主文章
3. 官方文档

安装这两个库的教程可以参考我的另一篇博文:环境配置

这是一个简单的人脸检测并且标出的小程序,具体实现将直接贴出代码,然后代码意义在注释中讲得也比较清楚。

  1. import dlib
  2. import cv2
  3. import sys
  4. detector = dlib.get_frontal_face_detector() #获取人脸分类器
  5. #这列用命令行的方式传入图片参数(其实就是名字)
  6. for f in sys.argv[1:]:
  7. #用opencv读取图片
  8. img = cv2.imread(f, cv2.IMREAD_COLOR)
  9. # 摘自官方文档:
  10. # image is a numpy ndarray containing either an 8bit grayscale or RGB image.
  11. # opencv读入的图片默认是bgr格式,我们需要将其转换为rgb格式;都是numpy的ndarray类。
  12. b, g, r = cv2.split(img) #分离是三颜色通道
  13. img = cv2.merge([r, g, b]) #把三种颜色组合成rgb新图片
  14. dets = detector(img, 1) #调用dlib库对图片进行识别
  15. print("这张图识别到 {} 张人脸".format(len(dets)))
  16. for index, face in enumerate(dets):
  17. #left()、top()、right()、bottom()都是dlib.rectangle类的方法,对应矩形四条边的位置
  18. print("人脸{}的框位置为left:{} , top:{} , right:{} , bottom{}".format(index, face.left(), face.top(), face.right(), face.bottom()))
  19. #在图片中标注并显示
  20. left = face.left()
  21. top = face.top()
  22. right = face.right()
  23. bottom = face.bottom()
  24. bottom = face.bottom()
  25. cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)
  26. cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)
  27. cv2.imshow(f, img)
  28. # 等待按键,随后退出,销毁窗口
  29. k = cv2.waitKey(0)
  30. cv2.destroyAllWindows()

运行图片我就不贴了,因为这个实在是挺直观的。。。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注