Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
240
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2022-240
240
Commits
64283776
Commit
64283776
authored
Nov 11, 2022
by
U.D.C.S.WIJESOORIYA
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new face detect file
parent
1fa7cb31
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
130 additions
and
0 deletions
+130
-0
Detect_faces_and_attendence.py
Detect_faces_and_attendence.py
+130
-0
No files found.
Detect_faces_and_attendence.py
0 → 100644
View file @
64283776
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment