commit d08975e735604024b74040e07c45286b1a913e6f Author: sairate Date: Thu Aug 22 16:30:26 2024 +0800 Signed-off-by: sairate diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/face.iml b/.idea/face.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/face.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..74b7078 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,25 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5f497bf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..da2856e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/add_face.py b/add_face.py new file mode 100644 index 0000000..6a1463f --- /dev/null +++ b/add_face.py @@ -0,0 +1,35 @@ +import cv2 +import face_recognition +import sqlite3 +import numpy as np + +from match_face import save_face_encoding + + +def add_new_face(name): + cap = cv2.VideoCapture(0) + + while True: + ret, frame = cap.read() + rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + + face_locations = face_recognition.face_locations(rgb_frame) + face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) + + if face_encodings: + # 假设只处理第一张检测到的人脸 + encoding = face_encodings[0] + save_face_encoding(name, encoding) + print(f"Face of {name} saved!") + break + + cv2.imshow('Capture New Face', frame) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + cap.release() + cv2.destroyAllWindows() + + +# 示例用法 +add_new_face("John Doe") diff --git a/captured_faces/face_1.jpg b/captured_faces/face_1.jpg new file mode 100644 index 0000000..ba707e2 Binary files /dev/null and b/captured_faces/face_1.jpg differ diff --git a/captured_faces/face_10.jpg b/captured_faces/face_10.jpg new file mode 100644 index 0000000..d9aef25 Binary files /dev/null and b/captured_faces/face_10.jpg differ diff --git a/captured_faces/face_2.jpg b/captured_faces/face_2.jpg new file mode 100644 index 0000000..1cbfe9a Binary files /dev/null and b/captured_faces/face_2.jpg differ diff --git a/captured_faces/face_3.jpg b/captured_faces/face_3.jpg new file mode 100644 index 0000000..28a0999 Binary files /dev/null and b/captured_faces/face_3.jpg differ diff --git a/captured_faces/face_4.jpg b/captured_faces/face_4.jpg new file mode 100644 index 0000000..d91fd64 Binary files /dev/null and b/captured_faces/face_4.jpg differ diff --git a/captured_faces/face_5.jpg b/captured_faces/face_5.jpg new file mode 100644 index 0000000..66ae048 Binary files /dev/null and b/captured_faces/face_5.jpg differ diff --git a/captured_faces/face_6.jpg b/captured_faces/face_6.jpg new file mode 100644 index 0000000..938c29e Binary files /dev/null and b/captured_faces/face_6.jpg differ diff --git a/captured_faces/face_7.jpg b/captured_faces/face_7.jpg new file mode 100644 index 0000000..f072522 Binary files /dev/null and b/captured_faces/face_7.jpg differ diff --git a/captured_faces/face_8.jpg b/captured_faces/face_8.jpg new file mode 100644 index 0000000..bd4fd90 Binary files /dev/null and b/captured_faces/face_8.jpg differ diff --git a/captured_faces/face_9.jpg b/captured_faces/face_9.jpg new file mode 100644 index 0000000..a8e069a Binary files /dev/null and b/captured_faces/face_9.jpg differ diff --git a/face_database.db b/face_database.db new file mode 100644 index 0000000..7abde58 Binary files /dev/null and b/face_database.db differ diff --git a/scanf_face.py b/scanf_face.py new file mode 100644 index 0000000..c8d21f2 --- /dev/null +++ b/scanf_face.py @@ -0,0 +1,112 @@ +import cv2 +import face_recognition +import os +import sqlite3 +import numpy as np + +# 初始化摄像头 +cap = cv2.VideoCapture(0) +photo_count = 0 +max_photos = 10 +captured_images = [] + +# 创建目录以保存照片 +save_path = "captured_faces" +os.makedirs(save_path, exist_ok=True) + +while photo_count < max_photos: + ret, frame = cap.read() + if not ret: + break + + # 将图像转换为RGB颜色 + rgb_frame = frame[:, :, ::-1] + + # 检测人脸 + face_locations = face_recognition.face_locations(rgb_frame) + + for face_location in face_locations: + top, right, bottom, left = face_location + face_image = frame[top:bottom, left:right] + + # 保存抓拍的照片 + image_path = os.path.join(save_path, f"face_{photo_count + 1}.jpg") + cv2.imwrite(image_path, face_image) + captured_images.append(image_path) + + photo_count += 1 + if photo_count >= max_photos: + break + + # 显示结果 + cv2.imshow("Capturing Faces", frame) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +cap.release() +cv2.destroyAllWindows() + +print(f"Captured {photo_count} images.") + + +def create_face_database(db_name="face_database.db"): + conn = sqlite3.connect(db_name) + c = conn.cursor() + c.execute('''CREATE TABLE IF NOT EXISTS faces + (id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + encoding BLOB NOT NULL)''') + conn.commit() + conn.close() + + +def add_face_to_database(name, image_path, db_name="face_database.db"): + conn = sqlite3.connect(db_name) + c = conn.cursor() + + # 加载图片并生成编码 + image = face_recognition.load_image_file(image_path) + face_encodings = face_recognition.face_encodings(image) + + if face_encodings: + face_encoding = face_encodings[0] + # 将编码转换为可以存储的格式 + encoding_blob = np.array(face_encoding).tobytes() + c.execute("INSERT INTO faces (name, encoding) VALUES (?, ?)", + (name, encoding_blob)) + conn.commit() + conn.close() + + +def match_faces(image_path, db_name="face_database.db"): + 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)) + + conn.close() + return matches + + +# 创建人脸数据库 +create_face_database() + +# 向数据库中添加人脸 +add_face_to_database("Alice", "captured_faces/face_1.jpg") + +# 匹配人脸 +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}")