python dlib 年龄 性别_python dlib学习(一):人脸检测
1、環(huán)境安裝
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
? 安裝過(guò)程慢,耐心等。
2、程序
? 注:程序中使用了python-opencv、dlib,使用前請(qǐng)配置好環(huán)境。 ? 程序中已有注釋。
-*- coding: utf-8 -*-
import sys
import dlib
import cv2
detector = dlib.get_frontal_face_detector() #獲取人臉?lè)诸?lèi)器
# 傳入的命令行參數(shù)
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讀入的圖片默認(rèn)是bgr格式,我們需要將其轉(zhuǎn)換為rgb格式;都是numpy的ndarray類(lèi)。
b, g, r = cv2.split(img) # 分離三個(gè)顏色通道
img2 = cv2.merge([r, g, b]) # 融合三個(gè)顏色通道生成新圖片
dets = detector(img, 1) #使用detector進(jìn)行人臉檢測(cè) dets為返回的結(jié)果
print("Number of faces detected: {}".format(len(dets))) # 打印識(shí)別到的人臉個(gè)數(shù)
# enumerate是一個(gè)Python的內(nèi)置方法,用于遍歷索引
# index是序號(hào);face是dets中取出的dlib.rectangle類(lèi)的對(duì)象,包含了人臉的區(qū)域等信息
# left()、top()、right()、bottom()都是dlib.rectangle類(lèi)的方法,對(duì)應(yīng)矩形四條邊的位置
for index, face in enumerate(dets):
print('face {}; left {}; top {}; right {}; bottom {}'.format(index,face.left(), face.top(), face.right(), face.bottom()))
# 在圖片中標(biāo)注人臉,并顯示
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)
# 等待按鍵,隨后退出,銷(xiāo)毀窗口
k = cv2.waitKey(0)
cv2.destroyAllWindows()
總結(jié)
以上是生活随笔為你收集整理的python dlib 年龄 性别_python dlib学习(一):人脸检测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python指定条件分类输出_pytho
- 下一篇: python的六大数据类型中可以改变_在