头歌平台-人工智能导论实验(人脸识别)

该博客通过Python的face_recognition库演示了人脸识别的过程,包括定位人脸、绘制人脸边界框、提取特征点以及进行人脸识别。此外,还展示了如何批量处理多张图片,将识别结果保存为标注过的图像。
摘要由CSDN通过智能技术生成
'''****************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, number_of_times_to_upsample=1, model="hog")
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)
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)
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
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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值