FACE RECOGNITION IN VOTING SYSTEM USING PYTHON
SOURCE CODE:
PROJECT STRUCTURE
face_voting_system
dataset # Stores registered user face images
db/[Link] # SQLite database
[Link] # Register new voter
[Link] # Voting logic with face recognition
[Link] # Admin view to see vote counts
train_encodings.py # Create face encodings from dataset
[Link]
INSTALLING PACKAGES:
pip install opencv-python face_recognition numpy sqlite3
USER REGISTRATION ([Link])
import cv2 import os
name = input("Enter your name: ").strip() folder = f'dataset/{name}' [Link](folder, exist_ok=True)
cap = [Link](0)
print("Capturing face... Press 'q' to quit.")
count = 0
while True:
ret, frame = [Link]()
if not ret:
break [Link]("Register - Press 's' to Save", frame)
k = [Link](1)
if k % 256 == ord('s'):
img_path = f"{folder}/{name}_{count}.jpg"
[Link](img_path, frame)
print(f"[INFO] Image saved: {img_path}")
count += 1 elif k % 256 == ord('q') or count >= 5:
break
[Link]()
[Link]()
GENERATE ENCODINGS (TRAIN_ENCODINGS.PY)
import face_recognition
import os
import pickle
encodings = []
names = []
for user_folder in [Link]("dataset"):
user_path = [Link]("dataset", user_folder)
for img_file in [Link](user_path):
img_path = [Link](user_path, img_file)
image = face_recognition.load_image_file(img_path)
face_locations = face_recognition.face_locations(image)
if face_locations:
face_encoding = face_recognition.face_encodings(image, face_locations)[0]
[Link](face_encoding)
[Link](user_folder)
data = {"encodings": encodings, "names": names}
with open("[Link]", "wb") as f:
[Link](data, f)
print("[INFO] Face encodings saved.")
VOTING SYSTEM ([Link])
import cv2
import face_recognition
import pickle
import sqlite3 from datetime
import datetime
with open("[Link]", "rb") as f:
data = [Link](f)
conn = [Link]('db/[Link]')
cursor = [Link]()
[Link]("CREATE TABLE IF NOT EXISTS votes (name TEXT, time TEXT)")
[Link]()
cap = [Link](0)
print("[INFO] Scanning face...")
while True:
ret, frame = [Link]()
rgb = [Link](frame, cv2.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(rgb)
encodings = face_recognition.face_encodings(rgb, boxes)
for encoding in encodings:
matches = face_recognition.compare_faces(data["encodings"],
encoding)
name = "Unknown"
if True in matches:
matchedIdx = [Link](True)
name = data["names"][matchedIdx]
[Link]("SELECT * FROM votes WHERE name=?", (name,))
result = [Link]()
if result:
print(f"[WARN] {name} has already voted.")
else:
print(f"[SUCCESS] Vote recorded for {name}")
[Link]("INSERT INTO votes VALUES (?, ?)", (name,
[Link]().strftime("%Y-%m-%d %H:%M:%S")))
[Link]()
[Link]("Voting - Press 'q' to quit", frame)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()
[Link]()
VIEW RESULTS ([Link])
import sqlite3
conn = [Link]('db/[Link]')
cursor = [Link]()
[Link]("SELECT name, COUNT(*) as count FROM votes GROUP BY name")
results = [Link]()
print("=== Voting Results ===")
for name, count in results:
print(f"{name}: {count} vote(s)")
SAMPLE OUTPUT:
1. During Registration
Enter your name: Muhasina
Output:
• webcam opens and shows a live preview.
• press "S" to save face images (at least 5 recommended).
• Press "Q" to finish.
Console Output:
[INFO] Image saved: dataset/Muhasina/Muhasina_0.jpg
[INFO] Image saved: dataset/Muhasina/Muhasina_1.jpg
What Happens:
• Folder dataset/Muhasina/ is created.
• 5 images of your face are saved.
[Link] Face Encodings (train_encodings.py)
[INFO] Face encodings saved.
What Happens:
• A file named [Link] is created.
• It stores face embeddings and names.
3. Voting ([Link])
Output Behavior:
• Webcam starts.
• It scans your face live.
If you are recognized and haven’t voted yet
[SUCCESS] Vote recorded for Alice.
If you already voted:
[WARN] Alice has already voted.
If you’re not recognized:
[INFO] Unknown person detected. Vote not counted.
GUI Window:
A live webcam feed with your face, showing a box if detected.
• Press Q to exit.
4. Viewing Results ([Link])
OUTPUT:
=== Voting Results ===
Alice: 1 vote(s)
Bob: 1 vote(s)
This pulls data from [Link] and counts the votes per person.