import face_recognition import cv2 def compareFaces(known_image, name): known_face_encoding = face_recognition.face_encodings(known_image)[0] for i in range(len(face_locations)): # face_Locations的长度就代表有多少张脸 top1, right1, bottom1, left1 = face_locations[i] face_image = unknown_image[top1:bottom1, left1:right1] face_encoding = face_recognition.face_encodings(face_image) if face_encoding: result = {} matches = face_recognition.compare_faces([unknown_face_encodings[i]], known_face_encoding, tolerance=0.39) if True in matches: print('在未知图片中找到了已知面孔') result['face_encoding'] = face_encoding result['is_view'] = True result['location'] = face_locations[i] result['face_id'] = i + 1 result['face_name'] = name results.append(result) if result['is_view']: print('已知面孔匹配照片上的第{}张脸!!'.format(result['face_id'])) unknown_image = face_recognition.load_image_file('qt.jpg') known_image1 = face_recognition.load_image_file('cs.png') known_image2 = face_recognition.load_image_file('cs1.png') results = [] unknown_face_encodings = face_recognition.face_encodings(unknown_image) face_locations = face_recognition.face_locations(unknown_image) compareFaces(known_image1, 'cs') compareFaces(known_image2, 'cs1') view_faces = [i for i in results if i['is_view']] if len(view_faces) > 0: for view_face in view_faces: top, right, bottom, left = view_face['location'] start = (left, top) end = (right, bottom) cv2.rectangle(unknown_image, start, end, (0, 0, 255), thickness=2) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(unknown_image, view_face['face_name'], (left + 6, bottom + 16), font, 1.0, (255, 255, 255), thickness=1) cv2.imshow('windows', unknown_image) cv2.waitKey()
时间: 2024-04-10 20:32:33 浏览: 140
dlib_face_recognition_resnet_model_v1.dat.zip_ResNet_dlib_face r
5星 · 资源好评率100%
这段代码看起来是使用了 face_recognition 和 cv2 库来进行人脸比对和标记。它加载了一个未知的图片和两个已知的图片,然后通过 face_recognition 库提取出人脸的编码和位置。接着使用 compareFaces 函数来比对已知图片中的人脸和未知图片中的人脸,并将匹配结果保存在 results 列表中。最后,将匹配到的人脸在未知图片上用矩形框和文字标记出来,并显示在窗口中。
需要注意的是,这段代码中的路径和文件名是固定的,你可能需要根据实际情况修改。另外,由于我是文本 AI,并不能直接运行和显示图片,所以你需要在合适的环境中运行这段代码。
阅读全文