15.opencv人脸识别

基于dlib进行人脸识别

1685554708671.png

dlib 提供的高精度人脸识别算法是基于深度学习网络ResNet-34实现。该网络基于三百万张照片进行训练,最终获得了人脸检测模型。

下载地址:
https://github.com/davisking/dlib-models/blob/master/dlib_face_recognition_resnet_model_v1.dat.bz2

face_rg.py

# 1 加载库
import cv2
import numpy as np
import face_recognition

# 2 加载图片
liu = cv2.imread("liu.jpeg")
guo = cv2.imread("guo.jpg")

# 3 BGR 转 RGB
liu_RGB = liu[:, :, ::-1]
guo_RGB = guo[:, :, ::-1]

# 4 检测人脸
liu_face = face_recognition.face_locations(liu_RGB)
guo_face = face_recognition.face_locations(guo_RGB)

# 5 人脸特征编码
liu_encoding = face_recognition.face_encodings(liu_RGB, liu_face)[0]
guo_encoding = face_recognition.face_encodings(guo_RGB, guo_face)[0]

# 6 把所有人脸放在一起,当做数据库使用
encodings = [liu_encoding, guo_encoding]
names = ["liu de hua", "guo fu cheng"]

# 7 打开摄像头,读取视频流
cap = cv2.VideoCapture(0)
if not cap.isOpened():
    raise  IOError("Camera Error !")

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)
    # 8 BGR 传 RGB
    frame_RGB = frame[:, :, ::-1]
    # 9 人脸检测
    faces_locations = face_recognition.face_locations(frame_RGB)
    # 10 人脸特征编码
    faces_encodings = face_recognition.face_encodings(frame_RGB, faces_locations)
    # 11 与数据库中的所有人脸进行匹配
    for (top, right, bottom, left), face_encoding in zip(faces_locations, faces_encodings):
        # 12 进行匹配
        matches = face_recognition.compare_faces(encodings, face_encoding)
        # 13 计算距离
        distances = face_recognition.face_distance(encodings, face_encoding)
        min_distance_index = np.argmin(distances) # 0, 1, 2
        # 14 判断:如果匹配,获取名字
        name = "Unknown"
        if matches[min_distance_index]:
            name = names[min_distance_index]
        # 15 绘制人脸矩形框
        cv2.rectangle(frame, (left, top), (right, bottom), (0,255,0), 3)
        # 16 绘制、显示对应人脸的名字
        cv2.rectangle(frame, (left, bottom - 30),(right, bottom), (0,0,255), 3)
        # 17 显示名字
        cv2.putText(frame, name, (left+10 , bottom-10), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1)
    # 18 显示整个效果
    cv2.imshow("face recognition", frame)
    # 19 判断 Q , 退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# 20 关闭所有资源
cap.release()
cv2.destroyAllWindows()

face_recognition 人脸识别

用到的方法:

  1. face_distance(face_encodings, face_to_compare)

  2. face_locations(img, number_of_times_to_upsample=1, model="hog")

  3. face_landmarks(face_image, face_locations=None, model="large")

  4. face_encodings(face_image, known_face_locations=None, num_jitters=1, model="small")

  5. compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)

face_recognition的API文档:
https://face-recognition.readthedocs.io/en/latest/_modules/face_recognition/api.html#face_locations

face_rg_dlib.py

# 1 导入库
import cv2
import dlib
import numpy as np

# 定义:关键点编码为128D
def encoder_face(image, detector, predictor, encoder, upsample=1, jet=1):
    # 检测人脸
    faces = detector(image, upsample)
    # 对每张人脸进行关键点检测
    faces_keypoints = [ predictor(image, face) for face in faces ] # 每张人脸的关键点
    return [ np.array(encoder.compute_face_descriptor(image, face_keypoint, jet)) for face_keypoint in faces_keypoints ]


# 定义:人脸比较,通过欧氏距离
def compare_faces(face_encoding, test_encoding):
    return list(np.linalg.norm(np.array(face_encoding) - np.array(test_encoding), axis=1))

# 定义:人脸比较,输出对应的名称
def comapre_faces_order(face_encoding, test_encoding, names):
    distance = list(np.linalg.norm(np.array(face_encoding) - np.array(test_encoding), axis=1))
    return zip(*sorted(zip(distance, names)))

def main():
    # 2 读取4张图片
    img1 = cv2.imread("guo.jpg")
    img2 = cv2.imread("liu1.jpg")
    img3 = cv2.imread("liu2.jpg")
    img4 = cv2.imread("liu3.jpg")
    test = cv2.imread("liu4.jpg")
    # BGR to RGB
    img1 = img1[:, :, ::-1]
    img2 = img2[:, :, ::-1]
    img3 = img3[:, :, ::-1]
    img4 = img4[:, :, ::-1]
    test = test[:, :, ::-1]

    img_names = ["guo,jpg", "liu1.jpg", "liu2.jpg", "liu3.jpg"]

    # 3  加载人脸检测器
    detector = dlib.get_frontal_face_detector()

    # 4 加载关键点的检测器
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

    # 5 加载人脸特征编码模型
    encoder = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")

    # 6 调用方法:128D特征向量输出
    img1_128D = encoder_face(img1, detector, predictor, encoder)[0]
    img2_128D = encoder_face(img2, detector, predictor, encoder)[0]
    img3_128D = encoder_face(img3, detector, predictor, encoder)[0]
    img4_128D = encoder_face(img4, detector, predictor, encoder)[0]
    test_128D = encoder_face(test, detector, predictor, encoder)[0]

    four_images_128D = [img1_128D, img2_128D, img3_128D, img4_128D]

    # 7 调用方法:比较人脸,计算特征向量之间的距离,判断是否为同一人
    distance = compare_faces(four_images_128D, test_128D)
    print(distance)

    distance, name = comapre_faces_order(four_images_128D, test_128D, img_names)

    print("\n")
    print("distance: {}, \n names: {} ".format(distance, name))

if __name__ == '__main__':
    main()
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355

推荐阅读更多精彩内容