-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup_students.py
More file actions
58 lines (47 loc) · 2.06 KB
/
signup_students.py
File metadata and controls
58 lines (47 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from flask import Flask, render_template, request
import mysql.connector
import pickle
import face_recognition
app = Flask(__name__, template_folder='C:/Users/surya/Desktop/Python project facial recognition/')
# MySQL connection
db = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="attendance_records"
)
cursor = db.cursor()
# Route for the signup page
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
uploaded_image = request.files['image']
# Save the uploaded image to a folder
image_path = 'C:/Users/surya/Desktop/Python project facial recognition/image paths/' + uploaded_image.filename
uploaded_image.save(image_path)
# Check if the uploaded image contains any face
if detect_face(image_path):
# Generate encoding from the uploaded image
encoding = generate_encoding(image_path)
# Insert the new user and encoding into the database
cursor.execute("INSERT INTO students (username, password, face_encodings, image_path) VALUES (%s, %s, %s, %s)",
(username, password, pickle.dumps(encoding), image_path))
db.commit()
return "User signed up successfully"
else:
return "No face detected in the uploaded image. Please upload an image containing a face."
return render_template('signup.html')
# Function to detect face in the image
def detect_face(image_path):
image = face_recognition.load_image_file(image_path)
face_locations = face_recognition.face_locations(image)
return len(face_locations) > 0
# Function to generate encoding from the image
def generate_encoding(image_path):
image = face_recognition.load_image_file(image_path)
encoding = face_recognition.face_encodings(image)[0]
return encoding
if __name__ == '__main__':
app.run(debug=True)