Commit 64283776 authored by U.D.C.S.WIJESOORIYA's avatar U.D.C.S.WIJESOORIYA

new face detect file

parent 1fa7cb31
import numpy as np
import face_recognition as fr
import cv2
import pickle
import datetime
import time
from imutils import face_utils as face
from scipy.spatial import distance
import os
import firebase_admin
from firebase_admin import db
from firebase_admin import credentials
from datetime import date
import json
current_path = os.path.abspath(os.path.join(os.path.dirname(__file__)))
encodingsFile = current_path+"\\encoding.pickle"
dis = 30
cred = credentials.Certificate("safedrive.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://safedrive-4fdc8-default-rtdb.firebaseio.com/'
})
ref = db.reference('sd/')
user_At_ref = ref.child('Attendence')
user_ref = ref.child('User')
def getAttendence(name):
# name = name.replace(".","_")
now = date.today()
week_ago = now - datetime.timedelta(days=7)
count = 0
snapshot = user_At_ref.get()
for key, val in snapshot.items():
d = val['date_of_detected']
date_object = datetime.datetime.strptime(d , '%Y-%m-%d %H:%M:%S').date()
if(val['name'] == name and (date_object == week_ago or date_object > week_ago)):
count += 1
print("Total trips per this week: "+str(count))
print("Distance: "+str(dis*count*2)+ " km")
def getID(name):
snapshot = user_ref.get()
for key, val in snapshot.items():
print(val)
if(val['name'] == name):
print("Passenger Name :"+name)
print("Passenger ID: "+str(val['id']))
return val['id']
def MarkAttendence(name):
name1 = name.replace(".","_")
#getID(name)
now = str(datetime.datetime.now())
now = now[:-7]
# dt = str(now.strftime('%H:%M:%S'))
today = str(date.today())
val = ''+name1+"_"+today
user_At_ref.child(val).set({
'date_of_detected': now,
'name': name
})
def main():
video_capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)
# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(encodingsFile, "rb").read())
known_face_encoding = data["encodings"]
known_face_names = data["names"]
fps_couter = 0
fps_to_display = 'initializing..'
fps_timer = time.time()
while True:
ret, frame = video_capture.read()
fps_couter+=1
frame_new = cv2.flip(frame,1)
if time.time()-fps_timer>=1.0:
fps_to_display=fps_couter
fps_timer=time.time()
fps_couter=0
cv2.putText(frame, "FPS :"+str(fps_to_display), (frame.shape[1]-100, frame.shape[0]-10),\
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
# change the colors of frame to RGB
rgb_frame = frame [:, :, ::-1]
face_location = fr.face_locations(rgb_frame)
face_encodings = fr.face_encodings(rgb_frame)
for (top, right, bottom, left), face_encoding in zip(face_location, face_encodings):
matches = fr.compare_faces(known_face_encoding, face_encoding)
name = "Unknown"
face_distances = fr.face_distance(known_face_encoding, face_encoding)
best_match_index = np.argmin(face_distances)
#print(known_face_encoding[best_match_index])
if matches[best_match_index]:
name = known_face_names[best_match_index]
MarkAttendence(name)
# frame over face. rectangle vertices, frame color, frame thickness
cv2.rectangle(frame, (left, top), (right, bottom), (0,0,255), 2)
# frame to display name
cv2.rectangle(frame, (left, bottom -35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
gray = cv2.cvtColor(frame_new, cv2.COLOR_BGR2GRAY)
cv2.imshow('Webcam_facerecognition', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
main()
getAttendence('U.D.C.S.Wijesooriya')
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment