-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVoter_verification.py
133 lines (103 loc) · 4.29 KB
/
Voter_verification.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
import cv2
from pyzbar import pyzbar
import sqlite3
from cryptography.fernet import Fernet
import face_recognition
import numpy as np
import time
# Load the encryption key
with open('encryption_key.key', 'rb') as key_file:
key = key_file.read()
cipher = Fernet(key)
# Connect to the database
conn = sqlite3.connect('voters.db')
cursor = conn.cursor()
def get_voter_by_barcode(barcode):
cursor.execute('SELECT id, name, face_image FROM voters WHERE original_barcode = ?', (barcode,))
return cursor.fetchone()
def verify_face(known_face_image, real_time_image):
known_face_encodings = face_recognition.face_encodings(known_face_image)
real_time_face_encodings = face_recognition.face_encodings(real_time_image)
if not known_face_encodings:
print("Error: No face detected in known face image.")
return False
if not real_time_face_encodings:
print("Error: No face detected in real-time image.")
return False
known_face_encoding = known_face_encodings[0]
real_time_face_encoding = real_time_face_encodings[0]
results = face_recognition.compare_faces([known_face_encoding], real_time_face_encoding)
return results[0]
def scan_barcode():
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open video capture.")
return None
barcode_data = None
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame from video capture.")
break
# Detect barcodes in the frame
barcodes = pyzbar.decode(frame)
if barcodes:
for barcode in barcodes:
barcode_data = barcode.data.decode('utf-8')
(x, y, w, h) = barcode.rect
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
barcode_text = f"{barcode_data} ({barcode.type})"
cv2.putText(frame, barcode_text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
print(f"Scanned Barcode: {barcode_data}")
voter = get_voter_by_barcode(barcode_data)
if voter:
print("Matching barcode found in the database.")
id, name, face_image_blob = voter
known_face_image = cv2.imdecode(np.frombuffer(face_image_blob, np.uint8), cv2.IMREAD_COLOR)
cap.release()
cv2.destroyAllWindows()
return known_face_image
else:
print("No matching barcode found in the database. Invalid barcode.")
else:
pass
#print("No barcode detected.")
cv2.imshow('Barcode Scanner', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
return None
def verify_face_with_camera(known_face_image):
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open video capture.")
return False
print("Please position your face in front of the camera for verification.")
time.sleep(10) # Wait for 10 seconds for the user to position their face
ret, real_time_frame = cap.read()
if not ret:
print("Error: Could not read frame from video capture.")
cap.release()
return False
# Convert the captured frame to RGB (required for face_recognition)
real_time_image_rgb = cv2.cvtColor(real_time_frame, cv2.COLOR_BGR2RGB)
# Display the real-time image for verification
cv2.imshow('Real-Time Image', real_time_frame)
cv2.waitKey(1) # Display the frame for 1 millisecond
is_face_match = verify_face(known_face_image, real_time_image_rgb)
cap.release()
cv2.destroyAllWindows()
return is_face_match
def main():
known_face_image = scan_barcode()
if known_face_image is not None:
is_face_match = verify_face_with_camera(known_face_image)
if is_face_match:
print("Face match successful. Voter is authenticated.")
else:
print("Face match failed. Voter is not authenticated.")
else:
print("Barcode scanning failed or barcode not found in database.")
if __name__ == "__main__":
main()