Skip to content
Snippets Groups Projects
Commit 6fd73641 authored by Hamza Hamidi's avatar Hamza Hamidi
Browse files

Initial commit

parents
No related merge requests found
.DS_Store 0 → 100644
File added
node_modules
IMAGE_FILES/Elon_Musk.jpg

66.4 KiB

IMAGE_FILES/bilgates.jpg

24 KiB

IMAGE_FILES/hamza.jpg

335 KiB

To run your code, you would need to have the following libraries installed:
Flask
OpenCV
NumPy
face_recognition
\ No newline at end of file
File added
File added
app.py 0 → 100644
from flask import Flask, flash, request, redirect, url_for, render_template, Response, get_flashed_messages
import cv2
import numpy as np
import face_recognition
import os
from werkzeug.utils import secure_filename
from datetime import datetime
import pandas as pd
from attendance import get_attendance_data
UPLOAD_FOLDER = r'/Users/hh/Downloads/Face-Recognition-Attendance-System-main/IMAGE_FILES'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def upload_file():
return render_template('upload.html')
@app.route('/success', methods=['GET', 'POST'])
def success():
if 'file' not in request.files:
return render_template('upload.html')
file = request.files['file']
if file.filename == '':
return render_template('upload.html')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return render_template('upload.html')
else:
return render_template('upload.html')
@app.route('/index')
def index():
messages = get_flashed_messages()
return render_template('index.html', messages=messages)
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 takeAttendence(name):
with open('attendance.csv', 'r+') as f:
mypeople_list = f.readlines()
nameList = []
for line in mypeople_list:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now = datetime.now()
datestring = now.strftime("%d/%m/%Y,%H:%M:%S")
f.writelines(f'\n{name},{datestring}')
def gen():
IMAGE_FILES = []
filename = []
dir_path = r'/Users/hh/Downloads/Face-Recognition-Attendance-System-main/IMAGE_FILES'
for imagess in os.listdir(dir_path):
img_path = os.path.join(dir_path, imagess)
img_path = face_recognition.load_image_file(img_path)
IMAGE_FILES.append(img_path)
filename.append(imagess.split(".", 1)[0])
encodeListknown = encoding_img(IMAGE_FILES)
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
imgc = cv2.resize(img, (0, 0), None, 0.8, 0.8)
imgc = cv2.cvtColor(imgc, cv2.COLOR_BGR2RGB)
fasescurrent = face_recognition.face_locations(imgc)
encode_fasescurrent = face_recognition.face_encodings(imgc, fasescurrent)
for encodeFace, faceloc in zip(encode_fasescurrent, fasescurrent):
matches_face = face_recognition.compare_faces(encodeListknown, encodeFace)
face_distence = face_recognition.face_distance(encodeListknown, encodeFace)
matchindex = np.argmin(face_distence)
if matches_face[matchindex]:
name = filename[matchindex]
y1, x2, y2, x1 = faceloc
y1, x2, y2, x1 = y1*4, x2*4, y2*4, x1*4 # Adjust the coordinates back to the original image size
cv2.rectangle(img, (x1, y1), (x2, y2), (80, 255, 100), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (70, 255, 20), cv2.FILLED)
cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 255, 255), 2)
takeAttendence(name)
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
key = cv2.waitKey(20)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
@app.route('/video_feed')
def video_feed():
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/table')
def table():
messages = get_flashed_messages()
data = get_attendance_data()
return render_template('table.html', messages=messages, table=data, titles=['Attendance Table'])
@app.route('/add_data', methods=['GET','POST'])
def add_data():
if request.method == 'POST':
name = request.form['name']
date = request.form['date']
time = request.form['time']
if not date:
date = datetime.now().strftime("%d/%m/%Y")
if not time:
time = datetime.now().strftime("%H:%M:%S")
with open('attendance.csv', 'a') as f:
if os.stat('attendance.csv').st_size == 0:
f.write('Name,Date,Time\n')
f.write(f'{name},{date},{time}\n')
flash(f'Successfully added {name} to the attendance list!')
return redirect(url_for('table'))
else:
now = datetime.now() # add this line to define datetime
return render_template('add_data.html', datetime=now) # pass datetime as a variable
if __name__ == "__main__":
app.run(debug=True)
Name,Date,Time
HAMZA,04/04/2023,16:03:18
hamza,09/04/2023,14:17:15
Mozhmel,21/04/2023,16:17:15
sha,21/04/2023,18:01:12
bilgates,09/04/2023,21:48:33
test,10/04/2023,14:05:35
tset2,10/04/2023,14:09:35
test3,10/04/2023,17:35:24
awe,10/04/2023,17:43:22
test5,10/04/2023,17:46:05
import pandas as pd
from datetime import datetime
def get_attendance_data():
df = pd.read_csv('attendance.csv')
if 'Time' in df.columns:
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S').dt.time
if 'Date' in df.columns:
df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%Y').dt.date
return df.to_dict(orient='records')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Manually enter attendance</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<!-- As a link -->
<nav class="navbar navbar-light bg-danger">
<a class="navbar-brand" href="{{ url_for('upload_file') }}">FACE RECOGNITION ATTENDANCE PROJECT</a>
</nav>
<div class="container">
<br>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class="flashes">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<div class="row">
<div class="col-sm-12">
<h2>Add Data</h2>
<form action="/add_data" method="POST">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="date">Date:</label>
<input type="text" class="form-control" id="date" name="date" placeholder="dd/mm/yyyy" value="{{ datetime.now().strftime('%d/%m/%Y') }}" required>
</div>
<div class="form-group">
<label for="time">Time:</label>
<input type="text" class="form-control" id="time" name="time" placeholder="hh:mm:ss" value="{{ datetime.now().strftime('%H:%M:%S') }}" required>
</div>
<button type="submit" class="btn btn-primary">Add Data</button>
</form>
</div>
</div>
</div>
<p class="mt-5 mb-3 text-muted text-center">&copy; 2023 by Hamza</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FACE RECOGNITION + ATTENDANCE PROJECT</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.video-container {
display: flex;
justify-content: center;
align-items: center;
height: calc(100vh - 56px);
background-color: #f8f8f8;
}
img {
border: 1px solid black;
}
</style>
</head>
<body>
<nav class="navbar navbar-light bg-danger">
<a class="navbar-brand" href="{{ url_for('upload_file') }}">FACE RECOGNITION ATTENDANCE PROJECT</a>
</nav>
<div class="container d-flex justify-content-center align-items-center" style="height: 80vh;">
<div class="text-center">
<img id="video_feed" src="{{ url_for('video_feed') }}" alt="Video Feed">
<br><br>
<form action="{{ url_for('upload_file') }}" method="POST" enctype="multipart/form-data">
<button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
</div>
<p class="mt-5 mb-3 text-muted text-center">&copy; 2023 by Hamza</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Attendance Table</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<!-- As a link -->
<nav class="navbar navbar-light bg-danger">
<a class="navbar-brand" href="{{ url_for('upload_file') }}">FACE RECOGNITION ATTENDANCE PROJECT</a>
</nav>
<div class="container">
<br>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul class="flashes">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
<div class="row">
<div class="col-sm-12">
<h2>Attendance Table</h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Time</th>
</tr>
</thead>
<tbody>
{% for row in table %}
<tr>
<td>{{ row['Name'] }}</td>
<td>{{ row['Date'].strftime('%d/%m/%Y') }}</td>
<td>{{ row['Time'].strftime('%H:%M:%S') }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if not table %}
<p>No data available</p>
{% endif %}
</div>
</div>
<div class="row">
<div class="col-sm-12">
<h2>Manual Input Data</h2>
<a href="/add_data" class="btn btn-primary">Input data</a>
</div>
</div>
</div>
<p class="mt-5 mb-3 text-muted text-center">&copy; 2023 by Hamza</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Home</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style>
.btn-group-lg > .btn {
padding: 10px 20px;
margin-right: 10px;
}
</style>
</head>
<body>
<!-- As a link -->
<nav class="navbar navbar-light bg-danger">
<a class="navbar-brand" href="{{ url_for('upload_file') }}">FACE RECOGNITION ATTENDANCE PROJECT</a>
</nav>
<div class="container">
<br>
<div class="mx-auto" style="">
<h2>Upload Your Photo</h2>
</div>
<br><br><br>
<form action="/success" method="post" enctype="multipart/form-data">
<div class="input-group mb-3" >
<input type="file" name="file" class="form-control" id="inputGroupFile02">
<div class="col-auto">
<button type="submit" value="Upload" class="btn btn-primary mb-3">Submit</button>
</div>
</div>
</form>
</div>
<div class="mx-auto" style="width: 1150px;">
<ul>
<li class="text-danger">Please Upload Png, Jpg, Jpeg, Gif Only</li>
<li class="text-danger">Have the file name be your name</li>
</ul>
</div>
<div class="mx-auto" style="width: 300px;">
<div class="btn-group btn-group-lg" role="group" aria-label="Button group with nested dropdown">
<a href="http://127.0.0.1:5000/index">
<button type="button" class="btn btn-success">Start camera</button>
</a>
<a href="/table">
<button type="button" class="btn btn-info">View attendance</button>
</a>
</div>
</div>
<p class="mt-5 mb-3 text-muted text-center">&copy; 2023 by Hamza</p>
</body>
</html>
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment