diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e3577df..d526a73 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -7,18 +7,7 @@
-
-
-
-
-
-
-
-
-
-
-
@@ -237,7 +226,7 @@
-
+
@@ -295,7 +284,7 @@
-
+
diff --git a/app.py b/app.py
index e06d472..4b949a8 100644
--- a/app.py
+++ b/app.py
@@ -1,4 +1,4 @@
-from flask import Flask, render_template, request, redirect, url_for
+from flask import Flask, render_template, request, redirect, url_for, flash
from flask_socketio import SocketIO, emit
import sqlite3
import os
@@ -7,7 +7,9 @@ import face_recognition
import numpy as np
app = Flask(__name__)
-app.config['UPLOAD_FOLDER'] = './static/db_image/' # 设置文件上传路径
+app.config['UPLOAD_FOLDER'] = './static/db_image' # 设置文件上传路径
+app.secret_key = 'TG1pSXF40uyHMu0OVrVt4ZsaLLqGekElefvewbI1hzD9UZLV71efR60trjAtsRXg' # Set the secret key for session management
+
socketio = SocketIO(app, async_mode='eventlet')
def create_face_database(db_name="face_database.db"):
@@ -68,7 +70,6 @@ def info_person():
c = conn.cursor()
if request.method == 'POST':
- # 添加新人员
if 'add' in request.form:
name = request.form['name']
identity = request.form['identity']
@@ -77,10 +78,14 @@ def info_person():
if image:
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
image.save(image_path)
- add_face_to_database(name, identity, image_path)
+
+ try:
+ add_face_to_database(name, identity, image_path)
+ flash('Face added successfully!')
+ except Exception as e:
+ flash(f'Error adding face: {e}')
return redirect(url_for('info_person'))
- # 更新人员信息
elif 'update' in request.form:
id = request.form['id']
name = request.form['name']
@@ -88,38 +93,52 @@ def info_person():
image = request.files.get('image_path')
if image:
- image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
- image.save(image_path)
+ # Generate new image path
+ new_image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
+ image.save(new_image_path)
- # 使用face_recognition生成新的面部编码
- loaded_image = face_recognition.load_image_file(image_path)
- face_encodings = face_recognition.face_encodings(loaded_image)
+ # Load the new image and generate new face encoding
+ try:
+ loaded_image = face_recognition.load_image_file(new_image_path)
+ face_encodings = face_recognition.face_encodings(loaded_image)
+
+ if face_encodings:
+ encoding = np.array(face_encodings[0]).tobytes()
+ new_image_path = "db_image/" + image.filename # Relative path for DB
+ else:
+ flash("No face detected in the uploaded image.")
+ return redirect(url_for('info_person'))
+ except Exception as e:
+ flash(f'Error processing image: {e}')
+ return redirect(url_for('info_person'))
+
+ # Update record in the database
+ c.execute("UPDATE faces SET name=?, identity=?, image_path=?, encoding=? WHERE id=?",
+ (name, identity, new_image_path, encoding, id))
+ flash('Face updated successfully!')
- if face_encodings:
- 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']
+ # If no new image is uploaded, keep existing image path and encoding
+ c.execute("UPDATE faces SET name=?, identity=? WHERE id=?",
+ (name, identity, id))
+ flash('Information updated successfully!')
- c.execute("UPDATE faces SET name=?, identity=?, image_path=?, encoding=? WHERE id=?",
- (name, identity, image_path, encoding, id))
-
- # 删除人员信息
elif 'delete' in request.form:
id = request.form['id']
c.execute("DELETE FROM faces WHERE id=?", (id,))
+ flash('Face deleted successfully!')
conn.commit()
- # 获取所有人员记录
+ # Fetch all records to display
c.execute("SELECT * FROM faces")
persons = c.fetchall()
conn.close()
return render_template('info_person.html', persons=persons)
+
+
@socketio.on('connect')
def handle_connect():
print('Client connected')
diff --git a/face_database.db b/face_database.db
index a482e37..da03f06 100644
Binary files a/face_database.db and b/face_database.db differ