diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index cd6ea3b..e3577df 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,6 +7,17 @@
+
+
+
+
+
+
+
+
+
+
+
@@ -72,7 +83,7 @@
"vue.rearranger.settings.migration": "true"
}
}
-
+
@@ -170,8 +181,8 @@
-
+
@@ -226,7 +237,7 @@
-
+
@@ -281,11 +292,11 @@
-
+
-
-
+
+
\ No newline at end of file
diff --git a/app.py b/app.py
index d49598d..e06d472 100644
--- a/app.py
+++ b/app.py
@@ -4,12 +4,51 @@ import sqlite3
import os
import eventlet
import face_recognition
+import numpy as np
app = Flask(__name__)
-app.config['UPLOAD_FOLDER'] = './static/db_image' # 设置文件上传路径
+app.config['UPLOAD_FOLDER'] = './static/db_image/' # 设置文件上传路径
socketio = SocketIO(app, async_mode='eventlet')
-# 从数据库中获取匹配日志记录
+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,
+ identity TEXT NOT NULL,
+ image_path TEXT NOT NULL,
+ encoding BLOB NOT NULL)''')
+
+ c.execute('''CREATE TABLE IF NOT EXISTS match_logs
+ (id INTEGER PRIMARY KEY AUTOINCREMENT,
+ name TEXT NOT NULL,
+ identity TEXT NOT NULL,
+ image_path TEXT NOT NULL,
+ match_time TEXT NOT NULL)''')
+ conn.commit()
+ conn.close()
+
+def add_face_to_database(name, identity, image_path, db_name="face_database.db"):
+ """将人脸信息添加到数据库"""
+ conn = sqlite3.connect(db_name)
+ c = conn.cursor()
+
+ # 将图片路径转换为相对路径
+ relative_image_path = os.path.relpath(image_path, start='./static')
+
+ 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, identity, image_path, encoding) VALUES (?, ?, ?, ?)",
+ (name, identity, relative_image_path, encoding_blob))
+ conn.commit()
+ conn.close()
+
def get_match_logs(db_name="face_database.db"):
conn = sqlite3.connect(db_name)
c = conn.cursor()
@@ -18,13 +57,11 @@ def get_match_logs(db_name="face_database.db"):
conn.close()
return logs
-# 首页,展示匹配记录
@app.route('/')
def index():
logs = get_match_logs()
return render_template('index.html', logs=logs)
-# 人员信息页面,展示人员信息并进行CRUD操作
@app.route('/info_person', methods=['GET', 'POST'])
def info_person():
conn = sqlite3.connect('face_database.db')
@@ -37,26 +74,11 @@ def info_person():
identity = request.form['identity']
image = request.files['image_path']
- # 保存上传的图片并生成编码
if image:
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
image.save(image_path)
-
- # 使用face_recognition生成面部编码
- loaded_image = face_recognition.load_image_file(image_path)
- face_encodings = face_recognition.face_encodings(loaded_image)
-
- if face_encodings:
- encoding = ','.join(map(lambda x: format(x, 'b'), face_encodings[0]))
- image_path = "db_image/"+image.filename # 仅保存文件名以便后续使用
- else:
- return "No face detected in the uploaded image."
- else:
- image_path = ""
- encoding = ""
-
- c.execute("INSERT INTO faces (name, identity, image_path, encoding) VALUES (?, ?, ?, ?)",
- (name, identity, image_path, encoding))
+ add_face_to_database(name, identity, image_path)
+ return redirect(url_for('info_person'))
# 更新人员信息
elif 'update' in request.form:
@@ -74,13 +96,12 @@ def info_person():
face_encodings = face_recognition.face_encodings(loaded_image)
if face_encodings:
- encoding = ','.join(map(str, face_encodings[0])) # 将编码转换为字符串存储
- image_path = image.filename # 仅保存文件名
+ encoding = np.array(face_encodings[0]).tobytes()
else:
return "No face detected in the uploaded image."
else:
- image_path = request.form['current_image_path'] # 使用当前的图片路径
- encoding = request.form['encoding'] # 使用现有的编码
+ image_path = request.form['current_image_path']
+ encoding = request.form['encoding']
c.execute("UPDATE faces SET name=?, identity=?, image_path=?, encoding=? WHERE id=?",
(name, identity, image_path, encoding, id))
@@ -99,13 +120,11 @@ def info_person():
return render_template('info_person.html', persons=persons)
-# 处理 WebSocket 连接
@socketio.on('connect')
def handle_connect():
print('Client connected')
emit('update', {'logs': get_match_logs()})
-# 发送更新到客户端
def send_updates():
while True:
# 模拟实时数据更新
@@ -113,5 +132,6 @@ def send_updates():
eventlet.sleep(5) # 每 5 秒发送一次更新
if __name__ == '__main__':
+ create_face_database() # Ensure the database is created
socketio.start_background_task(send_updates)
socketio.run(app, debug=True)
diff --git a/captured_faces/face_1.jpg b/captured_faces/face_1.jpg
index b2ac2f7..014253a 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 e3ab11e..ece9ebe 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 18db0d1..08c2d77 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 715b3f9..e2d659f 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 0bc38a7..a7721a2 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 98548ea..a791106 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 f99c23a..2714b56 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 1262ef1..2eb7b04 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 3f9ba65..88e260c 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 47cf2e3..ee49a99 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
index 9857941..a482e37 100644
Binary files a/face_database.db and b/face_database.db differ
diff --git a/scanf_face.py b/scanf_face.py
index 4694766..74b5816 100644
--- a/scanf_face.py
+++ b/scanf_face.py
@@ -108,7 +108,7 @@ def log_match(name, identity, db_image_path, db_name, log_file):
# create_face_database()
#
# # 向数据库中添加人脸
-# add_face_to_database("李四", "居民", "./static/db_image/test1.jpg")
+#add_face_to_database("李四", "居民", "./static/db_image/test1.jpg")
# add_face_to_database("张三", "居民", "./static/db_image/test2.jpg")
# add_face_to_database("王五", "居民", "./static/db_image/test3.jpg")
diff --git a/static/test1.jpg b/static/test1.jpg
new file mode 100644
index 0000000..dec6cce
Binary files /dev/null and b/static/test1.jpg differ