Spaces:
Runtime error
Runtime error
| from sklearn.neighbors import KNeighborsClassifier | |
| import cv2 | |
| import pickle | |
| import numpy as np | |
| import os | |
| import csv | |
| import time | |
| from datetime import datetime | |
| from flask import Flask, render_template, request | |
| # from win32com.client import Dispatch | |
| # def speak(str1): | |
| # speak=Dispatch(("SAPI.SpVoice")) | |
| # speak.Speak(str1) | |
| facedetect=cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml') | |
| with open('data/names.pkl', 'rb') as w: | |
| LABELS=pickle.load(w) | |
| with open('data/faces_data.pkl', 'rb') as f: | |
| FACES=pickle.load(f) | |
| # print('Shape of Faces matrix --> ', FACES.shape) | |
| knn=KNeighborsClassifier(n_neighbors=5) | |
| knn.fit(FACES, LABELS) | |
| COL_NAMES = ['NAME', 'TIME'] | |
| # def take_attendence(): | |
| # ret,frame=video.read() | |
| # gray=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| # faces=facedetect.detectMultiScale(gray, 1.3 ,5) | |
| # 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("Attendance/Attendance_" + date + ".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=[str(output[0]), str(timestamp)] | |
| # speak("Attendance Taken..") | |
| # if exist: | |
| # with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile: | |
| # writer=csv.writer(csvfile) | |
| # writer.writerow(attendance) | |
| # csvfile.close() | |
| # else: | |
| # with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile: | |
| # writer=csv.writer(csvfile) | |
| # writer.writerow(COL_NAMES) | |
| # writer.writerow(attendance) | |
| # csvfile.close() | |
| # # if k==ord('q'): | |
| # # break | |
| # # video.release() | |
| # # cv2.destroyAllWindows() | |
| app = Flask(__name__) | |
| def index(): | |
| return render_template('index.html') | |
| def atten(): | |
| return render_template('atten.html') | |
| def take_attendance(): | |
| video=cv2.VideoCapture(0) | |
| ret, frame = video.read() | |
| gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| faces = facedetect.detectMultiScale(gray, 1.3, 5) | |
| 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("Attendance/Attendance_" + date + ".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=[str(output[0]), str(timestamp)] | |
| speak("Attendance Taken..") | |
| if exist: | |
| with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile: | |
| writer=csv.writer(csvfile) | |
| writer.writerow(attendance) | |
| csvfile.close() | |
| else: | |
| with open("Attendance/Attendance_" + date + ".csv", "+a") as csvfile: | |
| writer=csv.writer(csvfile) | |
| writer.writerow(COL_NAMES) | |
| writer.writerow(attendance) | |
| csvfile.close() | |
| video.release() | |
| return "Attendance taken successfully!" | |
| if __name__ == '__main__': | |
| app.run(debug=True) |