diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/captured_faces/face_1.jpg b/captured_faces/face_1.jpg index ba707e2..32ee353 100644 Binary files a/captured_faces/face_1.jpg and b/captured_faces/face_1.jpg differ diff --git a/captured_faces/face_10.jpg b/captured_faces/face_10.jpg index d9aef25..ca1c473 100644 Binary files a/captured_faces/face_10.jpg and b/captured_faces/face_10.jpg differ diff --git a/captured_faces/face_2.jpg b/captured_faces/face_2.jpg index 1cbfe9a..ef48f6c 100644 Binary files a/captured_faces/face_2.jpg and b/captured_faces/face_2.jpg differ diff --git a/captured_faces/face_3.jpg b/captured_faces/face_3.jpg index 28a0999..aa29cc1 100644 Binary files a/captured_faces/face_3.jpg and b/captured_faces/face_3.jpg differ diff --git a/captured_faces/face_4.jpg b/captured_faces/face_4.jpg index d91fd64..12628f2 100644 Binary files a/captured_faces/face_4.jpg and b/captured_faces/face_4.jpg differ diff --git a/captured_faces/face_5.jpg b/captured_faces/face_5.jpg index 66ae048..24d9b48 100644 Binary files a/captured_faces/face_5.jpg and b/captured_faces/face_5.jpg differ diff --git a/captured_faces/face_6.jpg b/captured_faces/face_6.jpg index 938c29e..8da5cce 100644 Binary files a/captured_faces/face_6.jpg and b/captured_faces/face_6.jpg differ diff --git a/captured_faces/face_7.jpg b/captured_faces/face_7.jpg index f072522..75e1685 100644 Binary files a/captured_faces/face_7.jpg and b/captured_faces/face_7.jpg differ diff --git a/captured_faces/face_8.jpg b/captured_faces/face_8.jpg index bd4fd90..87abd78 100644 Binary files a/captured_faces/face_8.jpg and b/captured_faces/face_8.jpg differ diff --git a/captured_faces/face_9.jpg b/captured_faces/face_9.jpg index a8e069a..675a0e9 100644 Binary files a/captured_faces/face_9.jpg and b/captured_faces/face_9.jpg differ diff --git a/face_database.db b/face_database.db deleted file mode 100644 index 7abde58..0000000 Binary files a/face_database.db and /dev/null differ diff --git a/scanf_face.py b/scanf_face.py index c8d21f2..1ca5efc 100644 --- a/scanf_face.py +++ b/scanf_face.py @@ -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}")