mkManishKumar/face-recognition
0
1from sklearn.neighbors import KNeighborsClassifier2import cv23import pickle4import numpy as np5import os6import csv7import time8from datetime import datetime9from flask import Flask, render_template, request10 11 12# from win32com.client import Dispatch13 14# def speak(str1):15# speak=Dispatch(("SAPI.SpVoice"))16# speak.Speak(str1)17 18 19facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')20 21with open('data/names.pkl', 'rb') as w:22 LABELS=pickle.load(w)23with open('data/faces_data.pkl', 'rb') as f:24 FACES=pickle.load(f)25 26# print('Shape of Faces matrix --> ', FACES.shape)27 28knn=KNeighborsClassifier(n_neighbors=5)29knn.fit(FACES, LABELS)30 31COL_NAMES = ['NAME', 'TIME']32 33# def take_attendence():34# ret,frame=video.read()35 36# gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)37# faces=facedetect.detectMultiScale(gray, 1.3 ,5)38 39# for (x,y,w,h) in faces:40# crop_img=frame[y:y+h, x:x+w, :]41# resized_img=cv2.resize(crop_img, (50,50)).flatten().reshape(1,-1)42# output=knn.predict(resized_img)43# ts=time.time()44# date=datetime.fromtimestamp(ts).strftime("%d-%m-%Y")45# timestamp=datetime.fromtimestamp(ts).strftime("%H:%M-%S")46# exist=os.path.isfile("Attendance/Attendance_" + date + ".csv")47# cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 1)48# cv2.rectangle(frame,(x,y),(x+w,y+h),(50,50,255),2)49# cv2.rectangle(frame,(x,y-40),(x+w,y),(50,50,255),-1)50# cv2.putText(frame, str(output[0]), (x,y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 1)51# cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)52# attendance=[str(output[0]), str(timestamp)]53 54# speak("Attendance Taken..")55 56# if exist:57# with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile:58# writer=csv.writer(csvfile)59# writer.writerow(attendance)60# csvfile.close()61# else:62# with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile:63# writer=csv.writer(csvfile)64# writer.writerow(COL_NAMES)65# writer.writerow(attendance)66# csvfile.close()67# # if k==ord('q'):68# # break69# # video.release()70# # cv2.destroyAllWindows()71 72 73 74app = Flask(__name__)75 76@app.route('/')77def index():78 return render_template('index.html')79 80@app.route('/atten')81def atten():82 return render_template('atten.html')83 84@app.route('/take_attendance', methods=['POST'])85def take_attendance():86 video=cv2.VideoCapture(0)87 ret, frame = video.read()88 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)89 faces = facedetect.detectMultiScale(gray, 1.3, 5)90 91 for (x,y,w,h) in faces:92 crop_img=frame[y:y+h, x:x+w, :]93 resized_img=cv2.resize(crop_img, (50,50)).flatten().reshape(1,-1)94 output=knn.predict(resized_img)95 ts=time.time()96 date=datetime.fromtimestamp(ts).strftime("%d-%m-%Y")97 timestamp=datetime.fromtimestamp(ts).strftime("%H:%M-%S")98 exist=os.path.isfile("Attendance/Attendance_" + date + ".csv")99 cv2.rectangle(frame, (x,y), (x+w, y+h), (0,0,255), 1)100 cv2.rectangle(frame,(x,y),(x+w,y+h),(50,50,255),2)101 cv2.rectangle(frame,(x,y-40),(x+w,y),(50,50,255),-1)102 cv2.putText(frame, str(output[0]), (x,y-15), cv2.FONT_HERSHEY_COMPLEX, 1, (255,255,255), 1)103 cv2.rectangle(frame, (x,y), (x+w, y+h), (50,50,255), 1)104 attendance=[str(output[0]), str(timestamp)]105 106 speak("Attendance Taken..")107 108 if exist:109 with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile:110 writer=csv.writer(csvfile)111 writer.writerow(attendance)112 csvfile.close()113 else:114 with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile:115 writer=csv.writer(csvfile)116 writer.writerow(COL_NAMES)117 writer.writerow(attendance)118 csvfile.close()119 video.release()120 121 return "Attendance taken successfully!"122 123if __name__ == '__main__':124 app.run(debug=True)