使用face_recognition(一)人脸识别

关于使用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

猜你喜欢

转载自blog.csdn.net/hehangjiang/article/details/78961214