-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
147 lines (110 loc) · 3.84 KB
/
app.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from flask import Flask, request, redirect, url_for, render_template, send_file, session
from zipfile import ZipFile
import sqlite3
import os
from detect import detectFaces
from update_encodings import deleteEncodings
app = Flask(__name__)
app.secret_key = 'PeePeePooPoo'
is_logged_in = False
output = []
@app.route('/')
def home():
global is_logged_in
error = session.pop('error', None)
if not is_logged_in:
return render_template('login.html', error=error)
else:
return render_template('index.html', error=error)
@app.route('/login', methods=['POST'])
def login():
email = request.form['email']
password = request.form['password']
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
query = "SELECT email FROM users WHERE email=? and password=?"
cursor.execute(query, (email, password))
results = cursor.fetchall()
connection.close()
global is_logged_in
if len(results) == 1:
is_logged_in = True
if results[0][0] == "[email protected]":
return redirect(url_for('admin'))
else:
is_logged_in = False
session['error'] = "Incorrect username or password"
return redirect(url_for('home'))
@app.route('/register', methods=['POST'])
def register():
email = request.form['email']
password = request.form['password']
connection = sqlite3.connect("user_data.db")
cursor = connection.cursor()
try:
query = "INSERT INTO users (email, password) VALUES (?, ?)"
cursor.execute(query, (email, password))
connection.commit()
global is_logged_in
is_logged_in = True
print("noerror")
except:
print("error")
session['error'] = "Email already exists"
finally:
connection.close()
return redirect(url_for('home'))
@app.route('/logout', methods=['POST'])
def logout():
global is_logged_in
is_logged_in = False
return redirect(url_for('home'))
@app.route('/detect', methods=['GET', 'POST'])
def detect():
global output
reference = request.files['reference']
output = detectFaces(reference)
if output == -1:
session['error'] = "error"
return redirect(url_for('home'))
return render_template('detect.html', output_files=output)
@app.route('/zip_files')
def zip_files():
directory_path = './static/image_storage'
zip_file_path = './files.zip'
with ZipFile(zip_file_path, 'w') as zipf:
for file_name in os.listdir(directory_path):
if file_name in output:
file_path = os.path.join(directory_path, file_name)
zipf.write(file_path, file_name)
return send_file(zip_file_path, as_attachment=True)
@app.route('/admin', methods=['GET'])
def admin():
preview = []
directory = "./static/image_storage"
for file_name in os.listdir(directory):
preview.append(file_name)
return render_template('admin.html', preview_files=preview)
@app.route('/delete', methods=['POST'])
def delete_image():
filename = request.form['filename']
deleteEncodings(filename)
directory = "./static/image_storage"
image_path = os.path.join(directory, filename)
if os.path.exists(image_path):
os.remove(image_path)
print(f"Deleted image: {filename}")
else:
print(f"Image not found: {filename}")
return redirect(url_for('admin'))
@app.route('/upload', methods=['POST'])
def upload_image():
files = request.files.getlist('images')
directory = "./static/image_storage"
for file in files:
if file and file.filename != '':
filename = file.filename
file.save(os.path.join(directory, filename))
return redirect(url_for('admin'))
if __name__ == '__main__':
app.run(debug = True)