├── Votes.csv ├── requirements.txt ├── README.md ├── background.png ├── data ├── names.pkl └── faces_data.pkl ├── add_faces.py └── give_vote.py /Votes.csv: -------------------------------------------------------------------------------- 1 | 12341234,NOTA,25-05-2024,20:11-29 2 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | opencv-python 2 | scikit-learn 3 | pywin32 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virajj014/smartElectionGfg/HEAD/README.md -------------------------------------------------------------------------------- /background.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virajj014/smartElectionGfg/HEAD/background.png -------------------------------------------------------------------------------- /data/names.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virajj014/smartElectionGfg/HEAD/data/names.pkl -------------------------------------------------------------------------------- /data/faces_data.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/virajj014/smartElectionGfg/HEAD/data/faces_data.pkl -------------------------------------------------------------------------------- /add_faces.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import pickle 3 | import numpy as np 4 | import os 5 | 6 | if not os.path.exists('data/'): 7 | os.makedirs('data/') 8 | 9 | video = cv2.VideoCapture(0) 10 | facedetect= cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') 11 | faces_data = [] 12 | 13 | i=0 14 | name = input("Enter your aadhar number: ") 15 | framesTotal=51 16 | captureAfterFrame=2 17 | 18 | while True: 19 | ret, frame = video.read() 20 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 21 | faces=facedetect.detectMultiScale(gray, 1.3 ,5) 22 | for (x, y, w, h) in faces: 23 | crop_img = frame[y:y+h, x:x+w] 24 | resized_img = cv2.resize(crop_img, (50, 50)) 25 | if len(faces_data)<= framesTotal and i%captureAfterFrame==0: 26 | faces_data.append(resized_img) 27 | i=i+1 28 | cv2.putText(frame, str(len(faces_data)),(50,50),cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 1 ) 29 | cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1) 30 | 31 | 32 | cv2.imshow('frame', frame) 33 | k=cv2.waitKey(1) 34 | if k== ord('q') or len(faces_data) >= framesTotal: 35 | break 36 | 37 | 38 | video.release() 39 | cv2.destroyAllWindows() 40 | 41 | # print(len(faces_data)) 42 | faces_data = np.asarray(faces_data) 43 | faces_data = faces_data.reshape((framesTotal, -1)) 44 | print(faces_data) 45 | 46 | 47 | 48 | if 'names.pkl' not in os.listdir('data/'): 49 | names=[name]*framesTotal 50 | with open('data/names.pkl', 'wb') as f: 51 | pickle.dump(names, f) 52 | else: 53 | with open('data/names.pkl', 'rb') as f: 54 | names=pickle.load(f) 55 | names=names+[name]*framesTotal 56 | with open('data/names.pkl', 'wb') as f: 57 | pickle.dump(names, f) 58 | 59 | 60 | if 'faces_data.pkl' not in os.listdir('data/'): 61 | with open('data/faces_data.pkl', 'wb') as f: 62 | pickle.dump(faces_data, f) 63 | else: 64 | with open('data/faces_data.pkl', 'rb') as f: 65 | faces=pickle.load(f) 66 | faces=np.append(faces, faces_data, axis=0) 67 | with open('data/faces_data.pkl', 'wb') as f: 68 | pickle.dump(faces, f) -------------------------------------------------------------------------------- /give_vote.py: -------------------------------------------------------------------------------- 1 | from sklearn.neighbors import KNeighborsClassifier 2 | 3 | import cv2 4 | import pickle 5 | import numpy as np 6 | import os 7 | import csv 8 | import time 9 | from datetime import datetime 10 | from win32com.client import Dispatch 11 | 12 | 13 | def speak(str1): 14 | speak = Dispatch(("SAPI.SpVoice")) 15 | speak.Speak(str1) 16 | 17 | 18 | video = cv2.VideoCapture(0) 19 | facedetect = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') 20 | if not os.path.exists('data/'): 21 | os.makedirs('data/') 22 | 23 | with open('data/names.pkl', 'rb') as f: 24 | LABELS = pickle.load(f) 25 | 26 | with open('data/faces_data.pkl', 'rb') as f: 27 | FACES = pickle.load(f) 28 | 29 | knn = KNeighborsClassifier(n_neighbors=5) 30 | knn.fit(FACES, LABELS) 31 | imgBackground = cv2.imread("background.png") 32 | 33 | COL_NAMES = ['NAME', 'VOTE', 'DATE', 'TIME'] 34 | 35 | def check_if_exists(value): 36 | try: 37 | with open("Votes.csv", "r") as csvfile: 38 | reader = csv.reader(csvfile) 39 | for row in reader: 40 | if row and row[0] == value: 41 | return True 42 | except FileNotFoundError: 43 | print("File not found or unable to open the CSV file.") 44 | return False 45 | 46 | while True: 47 | ret, frame = video.read() 48 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 49 | faces = facedetect.detectMultiScale(gray, 1.3, 5) 50 | 51 | output = None # Initialize output to a default value 52 | for (x, y, w, h) in faces: 53 | crop_img = frame[y:y+h, x:x+w] 54 | resized_img = cv2.resize(crop_img, (50, 50)).flatten().reshape(1, -1) 55 | output = knn.predict(resized_img) 56 | ts = time.time() 57 | date = datetime.fromtimestamp(ts).strftime("%d-%m-%Y") 58 | timestamp = datetime.fromtimestamp(ts).strftime("%H:%M-%S") 59 | exist = os.path.isfile("Votes.csv") 60 | cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 1) 61 | cv2.rectangle(frame, (x, y), (x+w, y+h), (50, 50, 255), 2) 62 | cv2.rectangle(frame, (x, y-40), (x+w, y), (50, 50, 255), -1) 63 | cv2.putText(frame, str(output[0]), (x, y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 1) 64 | cv2.rectangle(frame, (x, y), (x+w, y+h), (50, 50, 255), 1) 65 | attendance = [output[0], timestamp] 66 | 67 | imgBackground[370:370 + 480, 225:225 + 640] = frame 68 | cv2.imshow('frame', imgBackground) 69 | k = cv2.waitKey(1) 70 | 71 | if output is not None: 72 | voter_exist = check_if_exists(output[0]) 73 | if voter_exist: 74 | speak("YOU HAVE ALREADY VOTED") 75 | break 76 | 77 | if k == ord('1'): 78 | speak("YOUR VOTE HAS BEEN RECORDED") 79 | time.sleep(5) 80 | if exist: 81 | with open("Votes.csv", "a") as csvfile: 82 | writer = csv.writer(csvfile) 83 | attendance = [output[0], "BJP", date, timestamp] 84 | writer.writerow(attendance) 85 | else: 86 | with open("Votes.csv", "a") as csvfile: 87 | writer = csv.writer(csvfile) 88 | writer.writerow(COL_NAMES) 89 | attendance = [output[0], "BJP", date, timestamp] 90 | writer.writerow(attendance) 91 | speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS") 92 | break 93 | 94 | if k == ord('2'): 95 | speak("YOUR VOTE HAS BEEN RECORDED") 96 | time.sleep(5) 97 | if exist: 98 | with open("Votes.csv", "a") as csvfile: 99 | writer = csv.writer(csvfile) 100 | attendance = [output[0], "CONGRESS", date, timestamp] 101 | writer.writerow(attendance) 102 | else: 103 | with open("Votes.csv", "a") as csvfile: 104 | writer = csv.writer(csvfile) 105 | writer.writerow(COL_NAMES) 106 | attendance = [output[0], "CONGRESS", date, timestamp] 107 | writer.writerow(attendance) 108 | speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS") 109 | break 110 | 111 | if k == ord('3'): 112 | speak("YOUR VOTE HAS BEEN RECORDED") 113 | time.sleep(5) 114 | if exist: 115 | with open("Votes.csv", "a") as csvfile: 116 | writer = csv.writer(csvfile) 117 | attendance = [output[0], "AAP", date, timestamp] 118 | writer.writerow(attendance) 119 | else: 120 | with open("Votes.csv", "a") as csvfile: 121 | writer = csv.writer(csvfile) 122 | writer.writerow(COL_NAMES) 123 | attendance = [output[0], "AAP", date, timestamp] 124 | writer.writerow(attendance) 125 | speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS") 126 | break 127 | 128 | if k == ord('4'): 129 | speak("YOUR VOTE HAS BEEN RECORDED") 130 | time.sleep(5) 131 | if exist: 132 | with open("Votes.csv", "a") as csvfile: 133 | writer = csv.writer(csvfile) 134 | attendance = [output[0], "NOTA", date, timestamp] 135 | writer.writerow(attendance) 136 | else: 137 | with open("Votes.csv", "a") as csvfile: 138 | writer = csv.writer(csvfile) 139 | writer.writerow(COL_NAMES) 140 | attendance = [output[0], "NOTA", date, timestamp] 141 | writer.writerow(attendance) 142 | speak("THANK YOU FOR PARTICIPATING IN THE ELECTIONS") 143 | break 144 | 145 | video.release() 146 | cv2.destroyAllWindows() 147 | --------------------------------------------------------------------------------