• face_recognition 人脸识别模块


    face_recognition 人脸识别模块

    安装之前,必须先安装dlib库

    1. 基于dlib库,进行了二次封装。号称是世界上最简洁的人脸识别库。

    2. 训练数据集: 是麻省理工大学下属的学院 利用13000多张照片为基础。主要是以欧美人为主。

     在 command line下安装模块时:

    F:Python_AIvenvScripts> pip install face_recoginiton

    • load_image_file : 加载要识别的人脸图像,返回Numpy数组,返回的是图片像素的特征向量(大小,方向)
    • face_loctions: 定位图中人脸的坐标位置。返回矩形框的location(top,right,bottom,left)
    • face_landmarks: 识别人脸的特征点; 返回68个特征点的坐标位置(chin....)
    • face_encodings: 获取图像中所有人脸的编码信息(向量)
    • compare_faces: 由面部编码信息进行面部识别匹配。
    #!/usr/bin/env python
    # !_*_ coding:utf-8 _*_
    
    import face_recognition
    from PIL import Image, ImageDraw
    import cv2
    
    # face_image = face_recognition.load_image_file('imgs/twis001.jpg')
    face_image = cv2.imread('imgs/twis001.jpg')
    face_marks_list = face_recognition.face_landmarks(face_image)
    
    pil_image = Image.fromarray(face_image)
    d = ImageDraw.Draw(pil_image)
    
    for face_marks in face_marks_list:
        facial_features = [
            'chin',
            'left_eyebrow',
            'right_eyebrow',
            'nose_bridge',
            'nose_tip',
            'left_eye',
            'right_eye',
            'bottom_lip'
        ]
        for facial_feature in facial_features:
            # print("{}: 特征位置:{}".format(facial_feature, face_marks[facial_feature]))
            # d.line(face_marks[facial_feature], fill=(0, 255, 0), width=5)
            # d.point(face_marks[facial_feature], fill=(255, 0, 0))
            for p in face_marks[facial_feature]:
                print(p)
                cv2.circle(face_image, p, 1, (0, 255, 0), 1)
    
    cv2.imshow("images", face_image)
    cv2.imwrite("twis01.jpg",face_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # pil_image.show()
    #!/usr/bin/env python
    # !_*_ coding:utf-8 _*_
    
    import face_recognition
    from PIL import Image, ImageDraw, ImageFont
    
    image_known = face_recognition.load_image_file('imgs/yangmi/yangmidanr00.jpg')
    image_unknown = face_recognition.load_image_file('imgs/yangmi/yangmihe02.jpg')
    
    image_known_encodings = face_recognition.face_encodings(image_known)[0]
    
    face_locations = face_recognition.face_locations(image_unknown)
    
    results = []
    
    for i in range(len(face_locations)):
        top, right, bottom, left = face_locations[i]
        face_image = image_unknown[top:bottom, left:right]
        face_encoding = face_recognition.face_encodings(face_image)
        if face_encoding:
            result = {}
            matches = face_recognition.compare_faces(face_encoding, image_known_encodings, tolerance=0.5)
            if True in matches:
                print('在未知图片中找到了已知面孔')
                result['face_encoding'] = face_encoding
                result['is_view'] = True
                result['location'] = face_locations[i]
                result['face_id'] = i + 1
                results.append(result)
    
                if result['is_view']:
                    print("已知面孔匹配照片上的第{}张脸!".format(result['face_id']))
    
    pil_image = Image.fromarray(image_unknown)
    draw = ImageDraw.Draw(pil_image)
    view_face_locations = [i['location'] for i in results if i['is_view']]
    for location in view_face_locations:
        top, right, bottom, left = location
        draw.rectangle([(left, top), (right, bottom)], outline=(0, 255, 0), width=2)
        font = ImageFont.truetype("consola.ttf", 20, encoding='unic')
        draw.text((left, top - 20), "yangmi", (255, 0, 0), font=font)
    
    pil_image.show()
    
    # 可以试着用cv2来画框,和写字 puttext
    #!/usr/bin/env python
    # !_*_ coding:utf-8 _*_
    
    import face_recognition
    import cv2
    
    img_known = face_recognition.load_image_file("imgs/joedan/cows.jpeg")
    img_unkown = face_recognition.load_image_file("imgs/joedan/joedan01.jpg")
    
    face_encodings_known = face_recognition.face_encodings(img_known)
    face_encodings_unknow = face_recognition.face_encodings(img_unkown)[0]
    matches = face_recognition.compare_faces(face_encodings_known, face_encodings_unknow, tolerance=0.5)
    print(matches)
    
    locations = face_recognition.face_locations(img_known)
    print(locations)
    if True in matches:
        index = matches.index(True)
        match = locations[index]
        print(match)
        top, right, bottom, left = match
        cv2.rectangle(img_known, (left, top), (right, bottom), (0, 0, 255), 2)
    
    cv2.imshow("images", img_known)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
  • 相关阅读:
    基于STM32F103C8T6的超声波测距示例
    ST-LINK 到 SWD接线图
    MDK破解版下载
    CompletableFuture Quasar 等并发编程
    c++中参数传递和函数返回简析
    c++中冒号(:)和双冒号(::)的用法
    c++中,size_typt, size_t, ptrdiff_t 简介
    c++中,保证头文件只被编译一次,避免多重包含的方法
    时间序列分析之一次指数平滑法
    Openv2.1基本数据类型
  • 原文地址:https://www.cnblogs.com/xuwenwei/p/14541841.html
Copyright © 2020-2023  润新知