Python影像辨識筆記(四):使用dlib辨識器

Yanwei Liu
6 min readApr 25, 2019

--

影片人臉辨識

安裝dlib套件

pip install dlib==19.6.1

偵測

import dlib
import cv2
import imutils

# 讀取圖檔
img = cv2.imread('image.jpg')

# 縮小圖片
img = imutils.resize(img, width=1280)

# Dlib 的人臉偵測器
detector = dlib.get_frontal_face_detector()

# 偵測人臉
face_rects = detector(img, 0)

# 取出所有偵測的結果
for i, d in enumerate(face_rects):
x1 = d.left()
y1 = d.top()
x2 = d.right()
y2 = d.bottom()

# 以方框標示偵測的人臉
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 4, cv2.LINE_AA)

# 顯示結果
cv2.imshow("Face Detection", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

偵測結果與分數

import dlib
import cv2
import imutils

img = cv2.imread('human-20180303-01.jpg')
img = imutils.resize(img, width=1280)
detector = dlib.get_frontal_face_detector()

# 偵測人臉,輸出分數
face_rects, scores, idx = detector.run(img, 0, -1)

for i, d in enumerate(face_rects):
x1 = d.left()
y1 = d.top()
x2 = d.right()
y2 = d.bottom()
text = "%2.2f(%d)" % (scores[i], idx[i])

cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 4, cv2.LINE_AA)

# 標示分數
cv2.putText(img, text, (x1, y1), cv2.FONT_HERSHEY_DUPLEX,
0.7, (255, 255, 255), 1, cv2.LINE_AA)

cv2.imshow("Face Detection", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

影片人臉偵測

import dlib
import cv2
import imutils

# 開啟影片檔案
cap = cv2.VideoCapture('video.mp4')

# 取得畫面尺寸
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 使用 XVID 編碼
fourcc = cv2.VideoWriter_fourcc(*'XVID')

# 建立 VideoWriter 物件,輸出影片至 output.avi,FPS 值為 20.0
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (width, height))

# Dlib 的人臉偵測器
detector = dlib.get_frontal_face_detector()

# 以迴圈從影片檔案讀取影格,並顯示出來
while(cap.isOpened()):
ret, frame = cap.read()

# 偵測人臉
face_rects, scores, idx = detector.run(frame, 0)

# 取出所有偵測的結果
for i, d in enumerate(face_rects):
x1 = d.left()
y1 = d.top()
x2 = d.right()
y2 = d.bottom()
text = "%2.2f(%d)" % (scores[i], idx[i])

# 以方框標示偵測的人臉
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 4, cv2.LINE_AA)

# 標示分數
cv2.putText(frame, text, (x1, y1), cv2.FONT_HERSHEY_DUPLEX,
0.7, (255, 255, 255), 1, cv2.LINE_AA)

# 寫入影格
out.write(frame)

# 顯示結果
cv2.imshow("Face Detection", frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
out.release()
cv2.destroyAllWindows()

即時串流影像人臉偵測

import dlib
import cv2
import imutils

# 開啟影片檔案
cap = cv2.VideoCapture(0)

# Dlib 的人臉偵測器
detector = dlib.get_frontal_face_detector()

# 以迴圈從影片檔案讀取影格,並顯示出來
while(cap.isOpened()):
ret, frame = cap.read()

# 偵測人臉
face_rects, scores, idx = detector.run(frame, 0)

# 取出所有偵測的結果
for i, d in enumerate(face_rects):
x1 = d.left()
y1 = d.top()
x2 = d.right()
y2 = d.bottom()
text = "%2.2f(%d)" % (scores[i], idx[i])

# 以方框標示偵測的人臉
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 4, cv2.LINE_AA)

# 標示分數
cv2.putText(frame, text, (x1, y1), cv2.FONT_HERSHEY_DUPLEX,
0.7, (255, 255, 255), 1, cv2.LINE_AA)

# 顯示結果
cv2.imshow("Face Detection", frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
cv2.destroyAllWindows()

--

--