人脸识别系统——Face recognition 人脸识别

第1关:人脸检测

python 复制代码
'''****************BEGIN****************'''
import face_recognition
image_path = './step1/image/children.jpg'
image = face_recognition.load_image_file(image_path)
face_locations = face_recognition.face_locations(image)
print(face_locations)
'''**************** END ****************'''

import cv2
for face_location in face_locations:
    '''****************BEGIN****************'''
    top, right, bottom, left = face_location
    cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)

    '''**************** END ****************'''

# 保存图片
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite("./step1/out/children.jpg", image_rgb)

第2关:人脸特征点获取

python 复制代码
import face_recognition
'''****************BEGIN****************'''
# 获取人脸特征点

image = face_recognition.load_image_file("./step2/image/laugh.jpg")

face_landmarks_list = face_recognition.face_landmarks(image)
print(face_landmarks_list)
'''**************** END ****************'''

import cv2

# 绘制人脸特征点
for face_landmarks in face_landmarks_list:
    '''****************BEGIN****************'''
    for facial_feature in face_landmarks.keys():
        for pt_pos in face_landmarks[facial_feature]:
                cv2.circle(image, pt_pos, 1, (255, 0, 0), 2)


    '''**************** END ****************'''

# 保存图片
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite("./step2/out/laugh.jpg", image_rgb)

第3关:人脸识别

python 复制代码
import face_recognition


def recognition():
    '''****************BEGIN****************'''
    # 导入图片
    known_image_path = "./step3/known_image/cyx1.jpg"
    known_image_cyz = face_recognition.load_image_file(known_image_path)

    unknown_image_1_path = "./step3/unknown_image/cyx2.jpg"
    unknown_image_2_path = "./step3/unknown_image/wlh.jpg"
    unknown_image_1 = face_recognition.load_image_file(unknown_image_1_path)
    unknown_image_2 = face_recognition.load_image_file(unknown_image_2_path)
    '''**************** END ****************'''

    '''****************BEGIN****************'''
    # 编码获取128维特征向量
    cyz_encoding = face_recognition.face_encodings(known_image_cyz)[0]
    unknown_encoding_1 = face_recognition.face_encodings(unknown_image_1)[0]
    unknown_encoding_2 = face_recognition.face_encodings(unknown_image_2)[0]
    '''**************** END ****************'''

    '''****************BEGIN****************'''
    # 比较特征向量值,识别人脸
    face1_result = face_recognition.compare_faces([cyz_encoding], unknown_encoding_1, tolerance=0.5)
    face2_result = face_recognition.compare_faces([cyz_encoding], unknown_encoding_2, tolerance=0.5)
    '''**************** END ****************'''
    return face1_result, face2_result

第4关:人脸识别绘制并展示

python 复制代码
import face_recognition
import cv2

'''****************BEGIN****************'''
# 加载已知图片
known_image_c_path = "./step4/known_image/Caocao.jpg"
known_image_xy_path = "./step4/known_image/XunYu.jpg"
known_image_smy_path = "./step4/known_image/SiMayi.jpg"
known_image_zch_path = "./step4/known_image/ZhangChunhua.jpg"


known_image_cc = face_recognition.load_image_file(known_image_c_path)
known_image_xy = face_recognition.load_image_file(known_image_xy_path)
known_image_smy = face_recognition.load_image_file(known_image_smy_path )
known_image_zch = face_recognition.load_image_file( known_image_zch_path)


'''**************** END ****************'''

'''****************BEGIN****************'''
# 对图片进行编码,获取128维特征向量

caocao_encoding = face_recognition.face_encodings(known_image_cc)[0]
xy_encoding = face_recognition.face_encodings(known_image_xy)[0]
zys_encoding = face_recognition.face_encodings(known_image_smy)[0]
cyz_encoding = face_recognition.face_encodings(known_image_zch)[0]



'''**************** END ****************'''

'''****************BEGIN****************'''
# 存为数组以便之后识别
known_faces = [
    caocao_encoding,
    xy_encoding,
    zys_encoding,
    cyz_encoding
]
'''**************** END ****************'''

'''****************BEGIN****************'''
# 加载待识别图片
unknown_image_1_path = "./step4/unknown_image/Caocao.jpg"
unknown_image_2_path = "./step4/unknown_image/Cuple.jpg"
unknown_image_3_path = "./step4/unknown_image/ZhangChunhua.jpg"
unknown_image_4_path = "./step4/unknown_image/XunYu.jpg"
unknown_image_5_path = './step4/unknown_image/A.jpg'

unknown_image_1 = face_recognition.load_image_file(unknown_image_1_path)
unknown_image_2 = face_recognition.load_image_file(unknown_image_2_path)
unknown_image_3 = face_recognition.load_image_file(unknown_image_3_path)
unknown_image_4 = face_recognition.load_image_file(unknown_image_4_path)
unknown_image_5 = face_recognition.load_image_file(unknown_image_5_path)


'''**************** END ****************'''

'''****************BEGIN****************'''
# 存为数组以遍历识别
unknown_faces = [
    unknown_image_1, 
    unknown_image_2, 
    unknown_image_3,
    unknown_image_4,
    unknown_image_5
]
'''**************** END ****************'''

# 初始化一些变量
face_locations = []
face_encodings = []
face_names = []
frame_number = 0

for frame in unknown_faces:
    face_names = []

    '''****************BEGIN****************'''
    face_locations = face_recognition.face_locations(frame)
    # 对图片进行编码,获取128维特征向量
    face_encodings = face_recognition.face_encodings(frame, face_locations)

    '''**************** END ****************'''

    for face_encoding in face_encodings:

        '''****************BEGIN****************'''
        # 识别图片中人脸是否匹配已知图片
        match = face_recognition.compare_faces(
            known_faces, face_encoding, tolerance=0.5)
        '''**************** END ****************'''

        '''****************BEGIN****************'''
        name = None
        if match[0]:
            name = "Caocao"
        elif match[1]:
            name = "XunYu"
        elif match[2]:
            name = "SiMayi"
        elif match[3]:
            name = 'ZhangChunhua'
        else:
            name = 'Unknown'
        '''**************** END ****************'''

        face_names.append(name)

    # 结果打上标签
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue

        '''****************BEGIN****************'''
        # 绘制脸部区域框
        # 绘制脸部区域框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        # 在脸部区域下面绘制人名
        cv2.rectangle(frame, (left, bottom - 25),
                        (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6),
                    font, 0.5, (255, 255, 255), 1)
        '''**************** END ****************'''

        print(frame[left+6, bottom-6])
        print(frame[left, bottom])

    print(face_locations)
    print(face_names)
    # 保存图片
    image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    path = './step4/out/' + name + str(face_locations[0][0]) + '.jpg'
    cv2.imwrite(path, image_rgb)
相关推荐
小松学前端3 分钟前
第六章 7.0 LinkList
java·开发语言·网络
可峰科技12 分钟前
斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
开发语言·qt
全栈开发圈16 分钟前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
面试鸭20 分钟前
离谱!买个人信息买到网安公司头上???
java·开发语言·职场和发展
小白学大数据21 分钟前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
Python大数据分析@24 分钟前
python操作CSV和excel,如何来做?
开发语言·python·excel
黑叶白树25 分钟前
简单的签到程序 python笔记
笔记·python
Shy96041838 分钟前
Bert完形填空
python·深度学习·bert
上海_彭彭1 小时前
【提效工具开发】Python功能模块执行和 SQL 执行 需求整理
开发语言·python·sql·测试工具·element
334554321 小时前
element动态表头合并表格
开发语言·javascript·ecmascript