-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgive_vote.py
146 lines (127 loc) · 5.21 KB
/
give_vote.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
from sklearn.neighbors import KNeighborsClassifier
import cv2
import pickle
import numpy as np
import os
import csv
import time
from datetime import datetime
from win32com.client import Dispatch
def speak(str1):
speak = Dispatch(("SAPI.SpVoice"))
speak.Speak(str1)
video = cv2.VideoCapture(0)
facedetect = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
if not os.path.exists('data/'):
os.makedirs('data/')
with open('data/names.pkl', 'rb') as f:
LABELS = pickle.load(f)
with open('data/faces_data.pkl', 'rb') as f:
FACES = pickle.load(f)
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(FACES, LABELS)
imgBackground = cv2.imread("background.png")
COL_NAMES = ['NAME', 'VOTE', 'DATE', 'TIME']
def check_if_exists(value):
try:
with open("Votes.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row and row[0] == value:
return True
except FileNotFoundError:
print("File not found or unable to open the CSV file.")
return False
while True:
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = facedetect.detectMultiScale(gray, 1.3, 5)
output = None # Initialize output to a default value
for (x, y, w, h) in faces:
crop_img = frame[y:y+h, x:x+w]
resized_img = cv2.resize(crop_img, (50, 50)).flatten().reshape(1, -1)
output = knn.predict(resized_img)
ts = time.time()
date = datetime.fromtimestamp(ts).strftime("%d-%m-%Y")
timestamp = datetime.fromtimestamp(ts).strftime("%H:%M-%S")
exist = os.path.isfile("Votes.csv")
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 1)
cv2.rectangle(frame, (x, y), (x+w, y+h), (50, 50, 255), 2)
cv2.rectangle(frame, (x, y-40), (x+w, y), (50, 50, 255), -1)
cv2.putText(frame, str(output[0]), (x, y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1)
cv2.rectangle(frame, (x, y), (x+w, y+h), (50, 50, 255), 1)
attendance = [output[0], timestamp]
imgBackground[370:370 + 480, 225:225 + 640] = frame
cv2.imshow('frame', imgBackground)
k = cv2.waitKey(1)
if output is not None:
voter_exist = check_if_exists(output[0])
if voter_exist:
speak("YOU HAVE ALREADY VOTED")
break
if k == ord('1'):
speak("YOUR VOTE HAS BEEN RECORDED")
time.sleep(5)
if exist:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
attendance = [output[0], "BJP", date, timestamp]
writer.writerow(attendance)
else:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(COL_NAMES)
attendance = [output[0], "BJP", date, timestamp]
writer.writerow(attendance)
speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS")
break
if k == ord('2'):
speak("YOUR VOTE HAS BEEN RECORDED")
time.sleep(5)
if exist:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
attendance = [output[0], "CONGRESS", date, timestamp]
writer.writerow(attendance)
else:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(COL_NAMES)
attendance = [output[0], "CONGRESS", date, timestamp]
writer.writerow(attendance)
speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS")
break
if k == ord('3'):
speak("YOUR VOTE HAS BEEN RECORDED")
time.sleep(5)
if exist:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
attendance = [output[0], "AAP", date, timestamp]
writer.writerow(attendance)
else:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(COL_NAMES)
attendance = [output[0], "AAP", date, timestamp]
writer.writerow(attendance)
speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS")
break
if k == ord('4'):
speak("YOUR VOTE HAS BEEN RECORDED")
time.sleep(5)
if exist:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
attendance = [output[0], "NOTA", date, timestamp]
writer.writerow(attendance)
else:
with open("Votes.csv", "a") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(COL_NAMES)
attendance = [output[0], "NOTA", date, timestamp]
writer.writerow(attendance)
speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS")
break
video.release()
cv2.destroyAllWindows()