头歌--人脸识别系统--Face recognition 人脸识别

目录

第1关:人脸检测

第2关:人脸特征点获取

第3关:人脸识别

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


第1关:人脸检测

'''****************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关:人脸特征点获取

import face_recognition
'''****************BEGIN****************'''
# 获取人脸特征点
image_path = './step2/image/laugh.jpg'
image = face_recognition.load_image_file(image_path)
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关:人脸识别

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关:人脸识别绘制并展示

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)

你可能感兴趣的:(python,人工智能)