-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfingerprintVotingSystem.py
More file actions
300 lines (241 loc) · 11 KB
/
fingerprintVotingSystem.py
File metadata and controls
300 lines (241 loc) · 11 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import base64
import os
from flask import *
import AES
import AWS_connection
import FingerPrintMatching
import numpy as np
from PIL import Image
import io
import cv2
import FingerprintBitmapHeader
from logger import Logger
citizen = None
numberOfElectionsActive = 2
candidates = None # This variable holds the candidates to be displayed.
elections = None # This variable holds the elections to be displayed.
app = Flask(__name__)
app.secret_key = "KawakiWoAmeku"
result_of_entered_ID = None
fingerprint_machine = None
from functools import wraps
from flask import session, redirect
from datetime import datetime
def add_no_cache_headers(response):
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
return response
@app.after_request
def apply_no_cache(response):
return add_no_cache_headers(response)
def check_authentication(view_func):
@wraps(view_func)
def wrapped_function(*args, **kwargs):
if 'voter' not in session:
return redirect('/')
return view_func(*args, **kwargs)
return wrapped_function
@app.route("/", methods=['POST', 'GET'])
def voting_id_page(): # Main page of voting screen
cursor = AWS_connection.establish_connection()
#cursor.execute("DELETE FROM Vote")
# connection.commit()
session.clear()
return render_template('voting_id_page.html')
@app.route("/fingerprint", methods=['POST'])
def voting_fingerprint_page(): # Fingerprint identification screen
global citizen
entered_id = request.form.get('voter_id')
cursor = AWS_connection.establish_connection()
cursor.execute("SELECT * FROM Citizen WHERE CitizenID = %s", (entered_id,))
citizen = cursor.fetchone()
cursor.execute("SELECT * FROM Vote WHERE CitizenID = %s", (entered_id,))
isvoted = cursor.fetchall()
cursor.execute("SELECT * FROM Election WHERE is_active = %s", (True,))
candidatesElections = cursor.fetchall()
if len(isvoted) == len(candidatesElections):
session.pop('voter', None)
return render_template('voting_id_page.html', error="You have already voted!")
cursor.close()
# connection.close()
if citizen != None and int(entered_id) == citizen[0]:
image1_blob_file = io.BytesIO(citizen[-2])
image_1 = Image.open(image1_blob_file)
image_1 = image_1.convert('L')
image1array = np.array(image_1) # converting the array to an image in base64 in order to display it
image1array = np.array(image1array,
dtype=np.uint8) # this code will be used to display when the voter scans his/her fingerprint from the machine
retval, buffer = cv2.imencode('.png',
image1array) # the fingerprint which is taken from the machine will be displayed for voter to see
image_base64 = base64.b64encode(buffer).decode('utf-8')
session['voter'] = entered_id
return render_template('voting_fingerprint_page.html', voter_fingerprint=image_base64)
# elif isvoted[0] == True:
# return render_template('voting_id_page.html', error="You have already voted!")
else:
return render_template('voting_id_page.html', error="Your ID does NOT exist!")
@app.route("/vote", methods=['POST', 'GET'])
@check_authentication
def voting_vote_page():
# the input values should be the Entered ID's fingerprint and machine fingerprint
# for the demo we can send two images from the database to check the functionality of the function
global elections
if 'voter' not in session:
return redirect('/')
if request.method == 'GET':
return redirect("/")
if request.method == 'POST':
fingerprint = request.files['voter_fingerprint']
cursor = AWS_connection.establish_connection()
cursor.execute("SELECT * FROM Election WHERE is_active= TRUE")
elections = cursor.fetchall()
cursor.close()
# Convert the retrieved binary image data to a PIL Image object
Voterid = session.get('voter')
# image_path = os.path.join("ImageSent", f"{Voterid}.bmp")
binary_data = fingerprint.read() # Image.open(image_path)
matching_result = FingerPrintMatching.Check_Fingerprint(citizen[-2], binary_data) # binary_data
if 'voter' not in session:
return redirect('/')
if matching_result:
return render_template('voting_election_page.html', person=citizen, elections=elections)
else:
return render_template('voting_id_page.html')
@app.route("/candidates", methods=['POST', 'GET'])
@check_authentication
def voting_candidate_page():
if request.method == 'GET':
return redirect('/')
if request.method == 'POST':
election_id = request.form.get('election_id')
if 'voter' not in session:
return redirect('/')
cursor = AWS_connection.establish_connection()
# connectionDB = sqlite3.connect("Government")
# cursor = connectionDB.cursor()
cursor.execute("SELECT * FROM CandidateElection WHERE ElectionID = %s AND is_active = TRUE", (election_id,))
candidatesElections = cursor.fetchall()
candidates = []
image_base64 = []
for i in range(len(candidatesElections)):
cursor.execute("SELECT * FROM Citizen WHERE CitizenID = %s", (candidatesElections[i][1],))
temp_candidates = cursor.fetchone()
candidates.append(temp_candidates)
image_base64.append(base64.b64encode(candidates[i][-1]).decode('utf-8'))
cursor.close()
return render_template("voting_candidate_page.html", candidates=candidates, image=image_base64,
election_id=election_id)
@app.route("/complete", methods=['GET', 'POST'])
@check_authentication
def complete():
if 'voter' not in session:
return redirect('/')
if request.method == 'GET':
return redirect("/")
if request.method == 'POST':
candidate_id = request.form.get('candidate_id')
cursor = AWS_connection.establish_connection()
cursor.execute("UPDATE CandidateElection SET CountOfVote = CountOfVote + 1 WHERE CitizenID = %s", (candidate_id,))
electionID = request.args.get('election_id')
Voterid = session.get('voter')
cursor.execute("INSERT INTO Vote (IsVoted, CitizenID, ElectionID) VALUES (%s,%s,%s)",
(True, Voterid, electionID))
cursor.close()
global elections
election_id_to_remove = electionID
print(type(election_id_to_remove))
# elections = [election for election in elections if election[0] != election_id_to_remove]
i = 0
for election in elections:
print(election[0])
if election[0] == int(election_id_to_remove):
elections.pop(i)
break
if len(elections) != 0:
return render_template("voting_election_page.html", person=citizen, elections=elections)
elif len(elections) == 0:
return render_template("voting_id_page.html", error="Voting successfully completed.")
@app.route("/results", methods=['GET', 'POST'])
def election_results():
# We connect database and pull elections from it to be displayed in results page.
cursor = AWS_connection.establish_connection()
# connectionDB = sqlite3.connect("Government")
# cursor = connectionDB.cursor()
elections = []
cursor.execute("SELECT * FROM Election")
electionList = cursor.fetchall()
cursor.close()
# connectionDB.close()
current_datetime = datetime.now()
for election in electionList:
election_end_str = f"{election[5]} {election[6]}"
election_end = datetime.strptime(election_end_str, "%Y-%m-%d %H:%M:%S")
if election_end < current_datetime:
elections.append(election)
# We have 2 forms in results page. First one is for selecting an election to see results of it,
# and the second one displays the results of the selected election.
form = 1
return render_template("election_results.html", elections=elections, form=1)
# else:
# return render_template("election_results.html", error="No elections being held!", form=1)
@app.route("/calculate", methods=['GET', 'POST'])
def calculate():
election_id = request.form.get('election_id') # We get election id to display votes of its candidates.
# We access the database and pull candidates of selected elections, their counts and pictures.
cursor = AWS_connection.establish_connection()
cursor.execute("SELECT * FROM CandidateElection WHERE ElectionID = %s", (election_id,))
candidatesElections = cursor.fetchall()
candidates = []
image_base64 = []
for i in range(len(candidatesElections)):
cursor.execute("SELECT * FROM Citizen WHERE CitizenID = %s", (candidatesElections[i][1],))
temp_candidates = cursor.fetchone()
candidates.append(temp_candidates)
image_base64.append(base64.b64encode(candidates[i][-1]).decode('utf-8'))
cursor.close()
# connectionDB.close()
percentages = CalculatePercentages(candidatesElections)
percentages = [format(num, ".2f") for num in percentages]
print(percentages)
return render_template("election_results.html", candidates=candidates, candidateElections=candidatesElections,
form=2, image=image_base64, percentages=percentages)
def CalculatePercentages(candidateElections):
# We calculate count percentages of each candidates and return percentages array.
percentages = []
total = 0
for candidate in candidateElections:
total += candidate[0]
for candidate in candidateElections:
percentages.append(candidate[0] / total * 100)
return percentages
@app.route("/GetFingerprint", methods=['POST'])
def GetFingerprint():
Logger.log(f"The Fingerprint with userID {citizen[0]} has been received")
# newList = []
# ImageSent = request.form["EntireImage"] #:list[bytes]
# ImageSent = ImageSent[1:len(ImageSent)-1].split(",")
# for eachToByte in ImageSent:
# newList.append(int(eachToByte).to_bytes(2,"little"))
# print(newList,len(newList))
currByteString_AES = request.form["EntireImage"]
Logger.log("AES Decryption has been started")
currByteString = AES.main("decrypt", "Dondulamanda Şondulamanda Zanga Banga Pondulamanda", currByteString_AES)
Logger.log("AES Decryption has been done")
currByte = str.encode(currByteString, encoding="ISO-8859-1")
if currByteString is None:
# Decryption Failed, Use here to handle it
pass
Logger.log("The Fingerprint file has been created")
ImageSent_FileWriter = open("ImageSent/" + str(citizen[0]) + "-invalid" + ".bmp", "wb")
ImageSent_FileWriter.write(FingerprintBitmapHeader.assembleBMPHeader(
FingerprintBitmapHeader.IMAGE_WIDTH, FingerprintBitmapHeader.IMAGE_HEIGHT,
FingerprintBitmapHeader.IMAGE_DEPTH, True))
ImageSent_FileWriter.write(currByte)
Logger.log("The Fingerprint has been saved")
# for eachByte in newList:
# ImageSent_FileWriter.write(eachByte)
ImageSent_FileWriter.close()
return Response(status=204) # response with status 204 (no content)
if __name__ == '__main__':
app.run(debug=True)