關於使用face_recognition,安裝方面還是有些坑的。之前用的是python3.5,pip安裝出錯,需要dlib什麼的。按照網上的教程弄,還是有問題,搞了一天搞不定。後來看到說用python3.6比較簡單,就換了個版本,結果pip輕鬆搞定。。。嗯,這是個坑,記錄一下。
關於face_recognition人臉識別的代碼其實挺簡單的,一看就能懂。我還順便記錄了一下時間,代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/1/3 14:12
# @Author : He Hangjiang
# @Site :
# @File : 人臉識別.py
# @Software: PyCharm
import cv2
import face_recognition
import time
timeStart = time.clock()
#讀取圖片並定位
img = face_recognition.load_image_file("face.jpg")
face_locations = face_recognition.face_locations(img)
print(face_locations)
time_1 = time.clock()
timeRec = time_1 - timeStart
print("識別時間:",timeRec)
#調用opencv顯示人臉
image = cv2.imread("face.jpg")
cv2.imshow("ori",image)
#遍歷人臉,並標註
faceNum = len(face_locations)
for i in range(faceNum):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left,top)
end = (right,bottom)
color = (55,255,155)
thickness = 3
cv2.rectangle(image,start,end,color,thickness)
cv2.imshow("recognized",image)
time_2 = time.clock()
timeDraw = time_2 - time_1
print("畫出位置時間:",timeDraw)
cv2.waitKey(0)
cv2.destroyAllWindows()
最後結果:
識別時間: 0.5109334811302357
畫出位置時間: 0.06631770175816731
就結果來說是準確的,但還希望速度能進一步提高。
參考博客:
http://blog.csdn.net/hongbin_xu/article/details/76284134