This commit is contained in:
sairate 2024-08-23 09:12:01 +08:00
parent d08975e735
commit 033364897b
13 changed files with 38 additions and 21 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

View File

@ -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()
# 获取数据库中所有存储的人脸编码
c.execute("SELECT name, encoding FROM faces")
known_faces = c.fetchall()
for image_path in captured_images:
# 加载待匹配图片并生成编码
unknown_image = face_recognition.load_image_file(image_path)
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
face_encodings = face_recognition.face_encodings(unknown_image)
c.execute("SELECT name, encoding FROM faces")
matches = []
for row in c.fetchall():
name, encoding_blob = row
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)
matches.append((name, match))
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}")