sairate
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 13 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 1.5 KiB |
BIN
face_database.db
|
@ -11,7 +11,7 @@ max_photos = 10
|
|||
captured_images = []
|
||||
|
||||
# 创建目录以保存照片
|
||||
save_path = "captured_faces"
|
||||
save_path = "./captured_faces"
|
||||
os.makedirs(save_path, exist_ok=True)
|
||||
|
||||
while photo_count < max_photos:
|
||||
|
@ -78,35 +78,46 @@ def add_face_to_database(name, image_path, db_name="face_database.db"):
|
|||
conn.close()
|
||||
|
||||
|
||||
def match_faces(image_path, db_name="face_database.db"):
|
||||
def match_faces(captured_images, db_name="face_database.db", tolerance=0.4):
|
||||
conn = sqlite3.connect(db_name)
|
||||
c = conn.cursor()
|
||||
|
||||
# 加载待匹配图片并生成编码
|
||||
unknown_image = face_recognition.load_image_file(image_path)
|
||||
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
|
||||
|
||||
# 获取数据库中所有存储的人脸编码
|
||||
c.execute("SELECT name, encoding FROM faces")
|
||||
matches = []
|
||||
for row in c.fetchall():
|
||||
name, encoding_blob = row
|
||||
known_encoding = np.frombuffer(encoding_blob, dtype=np.float64)
|
||||
match = face_recognition.compare_faces([known_encoding], unknown_encoding)
|
||||
matches.append((name, match))
|
||||
known_faces = c.fetchall()
|
||||
|
||||
for image_path in captured_images:
|
||||
# 加载待匹配图片并生成编码
|
||||
unknown_image = face_recognition.load_image_file(image_path)
|
||||
face_encodings = face_recognition.face_encodings(unknown_image)
|
||||
|
||||
if len(face_encodings) == 0:
|
||||
print(f"没有人脸 {image_path}")
|
||||
continue # 如果没有检测到人脸,跳过该图片
|
||||
|
||||
unknown_encoding = face_encodings[0]
|
||||
|
||||
for name, encoding_blob in known_faces:
|
||||
known_encoding = np.frombuffer(encoding_blob, dtype=np.float64)
|
||||
match = face_recognition.compare_faces([known_encoding], unknown_encoding, tolerance=tolerance)
|
||||
|
||||
if match[0]: # 如果匹配成功
|
||||
print(f"发现匹配: {name} 在 {image_path}")
|
||||
conn.close()
|
||||
return True # 一旦找到匹配,返回成功
|
||||
|
||||
conn.close()
|
||||
return matches
|
||||
|
||||
return False # 如果所有比较都没有匹配,返回失败
|
||||
|
||||
# 创建人脸数据库
|
||||
create_face_database()
|
||||
|
||||
# 向数据库中添加人脸
|
||||
add_face_to_database("Alice", "captured_faces/face_1.jpg")
|
||||
#add_face_to_database("屈礼", "./db_image/test.jpg")
|
||||
|
||||
# 逐张匹配抓拍的照片
|
||||
if match_faces(captured_images):
|
||||
print("至少一张匹配")
|
||||
else:
|
||||
print("没有匹配")
|
||||
|
||||
# 匹配人脸
|
||||
for captured_image in captured_images:
|
||||
matches = match_faces(captured_image)
|
||||
for name, match in matches:
|
||||
if match[0]:
|
||||
print(f"Match found: {name} in image {captured_image}")
|
||||
|
|