python dlib 年龄 性别_python dlib学习(一):人脸检测
1、環境安裝
Windows: 舊版本安裝pip install xxx.whl。以下是whl文件地址: ? Python Package Index
? 最新版本安裝:不要嫌麻煩,先裝上visual studio2015 (C++模塊)。
? 具體的記不清了,裝上cmake和boost,然后pip install dlib。
Ubuntu: sudo apt-get install build-essential cmake
? sudo apt-get install libgtk-3-dev
? sudo apt-get install libboost-all-dev
? pip install dlib
? 安裝過程慢,耐心等。
2、程序
? 注:程序中使用了python-opencv、dlib,使用前請配置好環境。 ? 程序中已有注釋。
-*- coding: utf-8 -*-
import sys
import dlib
import cv2
detector = dlib.get_frontal_face_detector() #獲取人臉分類器
# 傳入的命令行參數
for f in sys.argv[1:]:
# opencv 讀取圖片,并顯示
img = cv2.imread(f, cv2.IMREAD_COLOR)
# 摘自官方文檔:
# image is a numpy ndarray containing either an 8bit grayscale or RGB image.
# opencv讀入的圖片默認是bgr格式,我們需要將其轉換為rgb格式;都是numpy的ndarray類。
b, g, r = cv2.split(img) # 分離三個顏色通道
img2 = cv2.merge([r, g, b]) # 融合三個顏色通道生成新圖片
dets = detector(img, 1) #使用detector進行人臉檢測 dets為返回的結果
print("Number of faces detected: {}".format(len(dets))) # 打印識別到的人臉個數
# enumerate是一個Python的內置方法,用于遍歷索引
# index是序號;face是dets中取出的dlib.rectangle類的對象,包含了人臉的區域等信息
# left()、top()、right()、bottom()都是dlib.rectangle類的方法,對應矩形四條邊的位置
for index, face in enumerate(dets):
print('face {}; left {}; top {}; right {}; bottom {}'.format(index,face.left(), face.top(), face.right(), face.bottom()))
# 在圖片中標注人臉,并顯示
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)
cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)
cv2.imshow(f, img)
# 等待按鍵,隨后退出,銷毀窗口
k = cv2.waitKey(0)
cv2.destroyAllWindows()
總結
以上是生活随笔為你收集整理的python dlib 年龄 性别_python dlib学习(一):人脸检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python指定条件分类输出_pytho
- 下一篇: JAVA入门级教学之(逻辑(布尔)运算符