i’m sorry i just learned about flask and python and use these to make my project before this i run this code the website can open, but after i alter code the website shown cannot be reached. Below are my code:
from flask import Flask, flash, render_template, request, redirect, url_for, session, Response
from werkzeug.utils import secure_filename
from flask_mysqldb import MySQL
import cv2
import numpy as np
import face_recognition
from datetime import datetime
import re
import os
import sys
UPLOAD_FOLDER = r'C:python_programVisitor-Management-System-FacialRecognitionfaces'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
return '.' in filename and
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
app = Flask(__name__)
app.secret_key = 'ab2123445'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'visitor-management-system-facialrecognition'
mysql = MySQL(app)
@app.route('/')
@app.route('/login', methods =['GET', 'POST'])
def login():
mesage = ''
if request.method == 'POST' and 'email' in request.form and 'password' in request.form:
email = request.form['email']
password = request.form['password']
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM user WHERE email = % s AND password = % s', (email, password, ))
columns = cursor.description
users = cursor.fetchall()
for user in users:
user_dict = {}
for (index, column) in enumerate(columns):
user_dict[column[0]] = user[index]
if user_dict:
session['loggedin'] = True
session['userid'] = user_dict['id']
session['name'] = user_dict['name']
session['email'] = user_dict['email']
session['role'] = user_dict['role']
mesage = 'Logged in successfully !'
return redirect(url_for('dashboard'))
else:
mesage = 'Please enter correct email / password !'
return render_template('login.html', mesage = mesage)
@app.route("/dashboard", methods =['GET', 'POST'])
def dashboard():
if 'loggedin' in session:
return render_template("dashboard.html")
return redirect(url_for('login'))
@app.route("/users", methods =['GET', 'POST'])
def users():
if 'loggedin' in session:
cursor = mysql.connection.cursor()
cursor.execute("SELECT * FROM user WHERE role = 'user'")
users = cursor.fetchall()
return render_template("users.html", users = users)
return redirect(url_for('login'))
@app.route("/save_user", methods =['GET', 'POST'])
def save_user():
msg = ''
if 'loggedin' in session:
cursor = mysql.connection.cursor()
if request.method == 'POST' and 'role' in request.form and 'name' in request.form and 'email' in request.form and 'id' in request.form and 'TelNo' in request.form and 'purpose' in request.form and 'MemName' in request.form:
name = request.form['name']
email = request.form['email']
role = request.form['role']
userId = request.form['id']
TelNO = request.form['TelNo']
purpose = request.form['purpose']
MemName = request.form['MemName']
action = request.form['action']
filename = ''
if 'image_data' in request.json:
image_data = request.json['image_data']
filename = take_attendance(image_data) # Function to process and save image data
if action == 'updateUser':
userId = request.form['userid']
cursor.execute('UPDATE user SET name= %s, email= %s,TelNo= %s,purpose= %s,MemName= %s, picture= %s, role= %s WHERE id = %s', (name, email,TelNO,purpose,filename,MemName, role, (userId, ), ))
mysql.connection.commit()
return redirect(url_for('users'))
elif request.method == 'POST':
msg = 'Please fill out the form !'
return redirect(url_for('users'))
return redirect(url_for('login'))
@app.route("/edit_user", methods =['GET', 'POST'])
def edit_user():
msg = ''
if 'loggedin' in session:
editUserId = request.args.get('userid')
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM user WHERE id = % s', (editUserId, ))
users = cursor.fetchall()
return render_template("edit_user.html", users = users)
return redirect(url_for('login'))
@app.route("/view_user", methods =['GET', 'POST'])
def view_user():
if 'loggedin' in session:
viewUserId = request.args.get('userid')
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM user WHERE id = % s', (viewUserId, ))
user = cursor.fetchone()
return render_template("view_user.html", user = user)
return redirect(url_for('login'))
@app.route("/password_change", methods =['GET', 'POST'])
def password_change():
mesage = ''
if 'loggedin' in session:
changePassUserId = request.args.get('userid')
if request.method == 'POST' and 'password' in request.form and 'confirm_pass' in request.form and 'userid' in request.form :
password = request.form['password']
confirm_pass = request.form['confirm_pass']
userId = request.form['userid']
if not password or not confirm_pass:
mesage = 'Please fill out the form !'
elif password != confirm_pass:
mesage = 'Confirm password is not equal!'
else:
cursor = mysql.connection.cursor()
cursor.execute('UPDATE user SET password =% s WHERE id =% s', (password, (userId, ), ))
mysql.connection.commit()
mesage = 'Password updated !'
elif request.method == 'POST':
mesage = 'Please fill out the form !'
return render_template("password_change.html", mesage = mesage, changePassUserId = changePassUserId)
return redirect(url_for('login'))
@app.route("/delete_user", methods =['GET'])
def delete_user():
if 'loggedin' in session:
deleteUserId = request.args.get('userid')
cursor = mysql.connection.cursor()
cursor.execute('DELETE FROM user WHERE id = % s', (deleteUserId, ))
mysql.connection.commit()
return redirect(url_for('users'))
return redirect(url_for('login'))
@app.route('/logout')
def logout():
session.pop('loggedin', None)
session.pop('userid', None)
session.pop('email', None)
return redirect(url_for('login'))
@app.route('/register', methods =['GET', 'POST'])
def register():
mesage = ''
if request.method == 'POST' and 'name' in request.form and 'password' in request.form and 'email' in request.form :
name = request.form['name']
password = request.form['password']
email = request.form['email']
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM user WHERE email = % s', (email, ))
account = cursor.fetchone()
if account:
mesage = 'Account already exists !'
elif not re.match(r'[^@]+@[^@]+.[^@]+', email):
mesage = 'Invalid email address !'
elif not name or not password or not email:
mesage = 'Please fill out the form !'
else:
cursor.execute('INSERT INTO user (name, email, password) VALUES (%s, %s, %s)', (name, email, password,))
mysql.connection.commit()
mesage = 'You have successfully registered !'
elif request.method == 'POST':
mesage = 'Please fill out the form !'
return render_template('register.html', mesage = mesage)
# Route to add a new user
@app.route('/add_user', methods=['GET', 'POST'])
def add_user():
if request.method == 'POST':
# Fetch form data
name = request.form['name']
email = request.form['email']
password = request.form['password']
# Save user data to MySQL database
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users (name, email, password) VALUES (%s, %s, %s)", (name, email, password))
mysql.connection.commit()
cur.close()
return redirect(url_for('users'))
return render_template('add_user.html')
@app.route('/register_visitor', methods=['GET', 'POST'])
def register_visitor():
if request.method == 'POST':
visitor_name = request.form['visitor_name']
email = request.form['email']
visit_date = request.form['visit_date']
purpose = request.form['purpose']
member_name = request.form['member_name']
if 'picture' in request.files:
picture_file = request.files['picture']
if picture_file and allowed_file(picture_file.filename):
upload_folder = os.path.join(app.root_path, 'static/uploads')
picture_filename = secure_filename(picture_file.filename)
picture_path = os.path.join(upload_folder, picture_filename)
picture_file.save(picture_path)
cur = mysql.connection.cursor()
cur.execute("INSERT INTO visitors (visitor_name, email, visit_date, purpose, member_name, picture_path) VALUES (%s, %s, %s, %s, %s, %s)", (visitor_name, email, visit_date, purpose, member_name, picture_path))
mysql.connection.commit()
cur.close()
return redirect(url_for('visitors'))
return render_template('register_visitor.html')
def capture_image():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Capture Image', frame)
key = cv2.waitKey(1)
if key == ord('q'):
picture_path = os.path.join(app.config['UPLOAD_FOLDER'], 'captured_image.jpg')
cv2.imwrite(picture_path, frame)
break
cap.release()
cv2.destroyAllWindows()
return 'captured_image.jpg'
# Route to display list of visitors
@app.route('/visitors')
def visitors():
cur = mysql.connection.cursor()
cur.execute("SELECT * FROM visitors")
visitors = cur.fetchall()
cur.close()
return render_template('visitors.html', visitors=visitors)
@app.route("/attendance", methods =['GET', 'POST'])
def attendance():
if 'loggedin' in session:
userId = request.args.get('userid')
return render_template("attendance.html", userid = userId)
return redirect(url_for('login'))
def generate(userImage):
IMAGE_FILES = []
filename = []
imageDir = r'C:python_programVisitor-Management-System-FacialRecognitionfaces'
if userImage:
img_path = os.path.join(imageDir, userImage)
#print(img_path)
img_path = face_recognition.load_image_file(img_path)
IMAGE_FILES.append(img_path)
filename.append(userImage.split(".", 1)[0])
def encoding_img(IMAGE_FILES):
encodeList = []
for img in IMAGE_FILES:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
def addAttendence(name):
with open('attendence.csv', 'r+') as f:
mypeople_list = f.readlines()
dateList = []
now = datetime.now()
datestring = now.strftime('%m/%d/%Y')
for line in mypeople_list:
entry = line.split(',')
dateList.append(entry[1])
if datestring not in dateList:
timestring = now.strftime('%H:%M:%S')
f.writelines(f'n{name},{datestring}')
encodeListknown = encoding_img(IMAGE_FILES)
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
imgc = cv2.resize(img, (0, 0), None, 0.25, 0.25)
# converting image to RGB from BGR
imgc = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
fasescurrent = face_recognition.face_locations(imgc)
encode_fasescurrent = face_recognition.face_encodings(imgc, fasescurrent)
# faceloc- one by one it grab one face location from fasescurrent
# than encodeFace grab encoding from encode_fasescurrent
# we want them all in same loop so we are using zip
for encodeFace, faceloc in zip(encode_fasescurrent, fasescurrent):
matches_face = face_recognition.compare_faces(encodeListknown, encodeFace)
face_distence = face_recognition.face_distance(encodeListknown, encodeFace)
# print(face_distence)
# finding minimum distence index that will return best match
matchindex = np.argmin(face_distence)
if matches_face[matchindex]:
name = filename[matchindex].upper()
putText = 'Captured'
y1, x2, y2, x1 = faceloc
# multiply locations by 4 because we above we reduced our webcam input image by 0.25
# y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (255, 0, 0), 2, cv2.FILLED)
cv2.putText(img, putText, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
addAttendence(name) # taking name for attendence function above
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--framern'b'Content-Type: image/jpegrnrn' + frame + b'rn')
key = cv2.waitKey(20)
if key == 27:
cap.release()
cv2.destroyAllWindows()
break
@app.route('/take_attendance')
def take_attendance():
userId = request.args.get('userid')
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM user WHERE id = % s', (userId, ))
user = cursor.fetchone()
if(user['picture']):
return Response(generate(user['picture']),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
app.run(debug=True)
i want to change the register_visitor code with another face_recognition function with this code,
import tkinter as tk
import csv
import cv2
import os
import numpy as np
from PIL import Image
import pandas as pd
import datetime
import time
window = tk.Tk()
window.title("STUDENT ATTENDANCE USING FACE RECOGNITION SYSTEM")
window.geometry('800x500')
dialog_title = 'QUIT'
dialog_text = "are you sure?"
window.configure(background='green')
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
def clear():
std_name.delete(0, 'end')
res = ""
label4.configure(text=res)
def clear2():
std_number.delete(0, 'end')
res = ""
label4.configure(text=res)
def takeImage():
name = (std_name.get())
Id = (std_number.get())
if name.isalpha():
cam = cv2.VideoCapture(0)
harcascadePath = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(harcascadePath)
sampleNum = 0
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.1, 3)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
sampleNum = sampleNum + 1
# store each student picture with its name and id
cv2.imwrite("TrainingImages " + name + "." + Id + '.' + str(sampleNum) + ".jpg",
gray[y:y + h, x:x + h])
cv2.imshow('FACE RECOGNIZER', img)
if cv2.waitKey(100) & 0xFF == ord('q'):
break
# stop the camera when the number of picture exceed 50 pictures for each student
if sampleNum > 50:
break
cam.release()
cv2.destroyAllWindows()
# print the student name and id after a successful face capturing
res = 'Student details saved with: n Matric number : ' + Id + ' and Full Name: ' + name
row = [Id, name]
with open('studentDetailss.csv', 'a+') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
label4.configure(text=res)
else:
if name.isalpha():
res = "Enter correct Matric Number"
label4.configure(text=res)
def getImagesAndLabels(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faces = []
Ids = []
for imagePath in imagePaths:
pilImage = Image.open(imagePath).convert('L')
imageNp = np.array(pilImage, 'uint8')
Id = int(os.path.split(imagePath)[-1].split(".")[1])
faces.append(imageNp)
Ids.append(Id)
return faces, Ids
def trainImage():
recognizer = cv2.face.LBPHFaceRecognizer_create()
harcascadePath = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(harcascadePath)
faces, Id = getImagesAndLabels("TrainingImages")
recognizer.train(faces, np.array(Id))
recognizer.save("Trainner.yml")
res = "Image Trained"
label4.configure(text=res)
def trackImage():
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("Trainner.yml")
harcascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(harcascadePath)
df = pd.read_csv("studentDetailss.csv")
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
cam = cv2.VideoCapture(0)
# create a dataframe to hold the student id,name,date and time
col_names = {'Id', 'Name', 'Date', 'Time'}
attendance = pd.DataFrame(columns=col_names)
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.1, 3)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
Id, conf = recognizer.predict(gray[y:y + h, x:x + w])
# a confidence less than 50 indicates a good face recognition
if conf < 60:
ts = time.time()
date = datetime.datetime.fromtimestamp(ts).strftime('%d-%m-%Y')
timeStamp = datetime.datetime.fromtimestamp(ts).strftime('%H:%M')
aa = df.loc[df['ID'] == Id]['NAME'].values
tt = str(Id) + "-" + aa
attendance.loc[len(attendance)] = [Id, aa, date, timeStamp]
row2 = [Id, aa, date, timeStamp]
# open the attendance file for update
with open('AttendanceFile.csv', 'a+') as csvFile2:
writer2 = csv.writer(csvFile2)
writer2.writerow(row2)
csvFile2.close()
# print attendance updated on the notification board of the GUI
res = 'ATTENDANCE UPDATED WITH DETAILS'
label4.configure(text=res)
else:
Id = 'Unknown'
tt = str(Id)
# store the unknown images in the images unknown folder
if conf > 65:
noOfFile = len(os.listdir("ImagesUnknown")) + 1
cv2.imwrite("ImagesUnknownImage" + str(noOfFile) + ".jpg", img[y:y + h, x:x + w])
res = 'ID UNKNOWN, ATTENDANCE NOT UPDATED'
label4.configure(text=res)
# To avoid duplication in the attendance file.
attendance = attendance.drop_duplicates(subset=['Id'], keep='first')
# show the student id and name
cv2.putText(img, str(tt), (x, y + h - 10), font, 0.8, (255, 255, 255), 1)
cv2.imshow('FACE RECOGNIZER', img)
if cv2.waitKey(1000) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
label1 = tk.Label(window, background="green", fg="black", text="Name :", width=10, height=1,
font=('Helvetica', 16))
label1.place(x=83, y=40)
std_name = tk.Entry(window, background="yellow", fg="black", width=25, font=('Helvetica', 14))
std_name.place(x=280, y=41)
label2 = tk.Label(window, background="green", fg="black", text="Matric Number :", width=14, height=1,
font=('Helvetica', 16))
label2.place(x=100, y=90)
std_number = tk.Entry(window, background="yellow", fg="black", width=25, font=('Helvetica', 14))
std_number.place(x=280, y=91)
clearBtn1 = tk.Button(window, background="red", command=clear, fg="white", text="CLEAR", width=8, height=1,
activebackground="red", font=('Helvetica', 10))
clearBtn1.place(x=580, y=42)
clearBtn2 = tk.Button(window, background="red", command=clear2, fg="white", text="CLEAR", width=8,
activebackground="red", height=1, font=('Helvetica', 10))
clearBtn2.place(x=580, y=92)
label3 = tk.Label(window, background="green", fg="red", text="Notification", width=10, height=1,
font=('Helvetica', 20, 'underline'))
label3.place(x=320, y=155)
label4 = tk.Label(window, background="yellow", fg="black", width=55, height=4, font=('Helvetica', 14, 'italic'))
label4.place(x=95, y=205)
takeImageBtn = tk.Button(window, command=takeImage, background="yellow", fg="black", text="CAPTURE IMAGE",
activebackground="red",
width=15, height=3, font=('Helvetica', 12))
takeImageBtn.place(x=130, y=360)
trainImageBtn = tk.Button(window, command=trainImage, background="yellow", fg="black", text="TRAINED IMAGE",
activebackground="red",
width=15, height=3, font=('Helvetica', 12))
trainImageBtn.place(x=340, y=360)
trackImageBtn = tk.Button(window, command=trackImage, background="yellow", fg="black", text="TRACK IMAGE", width=12,
activebackground="red", height=3, font=('Helvetica', 12))
trackImageBtn.place(x=550, y=360)
window.mainloop()
New contributor
delph7 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.