Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
klorydryk
FacialRecoTrackingScore
Commits
f321244b
Commit
f321244b
authored
Jan 22, 2020
by
cb
Browse files
Added an option to put a warning message below the face if suspicious
parent
e1c72495
Changes
1
Hide whitespace changes
Inline
Side-by-side
trackmultiplefaces.py
View file @
f321244b
...
...
@@ -19,7 +19,7 @@ import randomnames
#Make sure that you copy this file from the opencv project to the root of this
#project folder
faceCascade
=
cv2
.
CascadeClassifier
(
'haarcascade_frontalface_default.xml'
)
print
(
faceCascade
.
empty
()
)
#The deisred output width and height
OUTPUT_SIZE_WIDTH
=
775
OUTPUT_SIZE_HEIGHT
=
600
...
...
@@ -45,7 +45,11 @@ def calculateAverageSuspicious(faceSuspicion):
averageSuspicious
=
sum
(
faceSuspicion
)
/
currentFaces
print
(
f
"Average suspicion:
{
averageSuspicious
}
"
)
def
detectAndTrackMultipleFaces
(
start_thread
,
charset
,
draw_person_dangerosity
):
def
detectAndTrackMultipleFaces
(
start_thread
,
charset
,
draw_person_dangerosity
,
add_warning_message
):
#Open the first webcame device
capture
=
cv2
.
VideoCapture
(
0
)
...
...
@@ -217,6 +221,7 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
# Pick up random name: Do we want a thread for that?
# Advantage is that we can pop an "Identifying..." Message
# Disadvantage is that is more "lourd"
if
start_thread
:
#Start a new thread that is used to simulate face recognition.
...
...
@@ -245,6 +250,7 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
rectangle_around_face
(
fid
,
faceTrackers
,
faceSuspicion
,
# If False, colors will not change with suspicion level
draw_person_dangerosity
,
resultImage
)
...
...
@@ -271,30 +277,40 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
width
=
resultImage
.
shape
[
1
]
cv2
.
rectangle
(
resultImage
,
(
int
(
width
-
50
),
0
),
(
int
(
width
),
120
),
color
,
-
1
)
# If Known -> print Name
if
fid
in
faceNames
.
keys
():
# Name's alignment
cv2
.
putText
(
resultImage
,
faceNames
[
fid
]
,
(
t_x
+
int
((
t_w
-
len
(
faceNames
[
fid
])
*
10
)
/
2
+
2
),
int
(
t_y
)),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
faceNames
[
fid
]
,
# Name's alignment
(
t_x
+
int
((
t_w
-
len
(
faceNames
[
fid
])
*
10
)
/
2
+
10
),
int
(
t_y
)
-
10
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
# Add a warning message depending on suspicious level
if
add_warning_message
:
put_warning_message
(
fid
,
faceTrackers
,
faceSuspicion
,
resultImage
,
threshold
=
2
)
# Else print Identifying
else
:
# Detecting
text
=
"Identifying"
cv2
.
putText
(
resultImage
,
text
,
(
t_x
+
int
((
t_w
-
len
(
text
)
*
10
)
/
2
+
2
),
int
(
t_y
)),
(
t_x
+
int
((
t_w
-
len
(
text
)
*
10
)
/
2
+
10
),
int
(
t_y
)),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
(
255
,
255
,
255
),
1
,
cv2
.
LINE_AA
)
#Since we want to show something larger on the screen than the
#original 320x240, we resize the image again
#
...
...
@@ -308,9 +324,6 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
cv2
.
imshow
(
"base-image"
,
baseImage
)
cv2
.
imshow
(
"result-image"
,
resultImage
)
#largeResult)
#To ensure we can also deal with the user pressing Ctrl-C in the console
#we have to check for the KeyboardInterrupt exception and break out of
#the main loop
...
...
@@ -321,6 +334,32 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
cv2
.
destroyAllWindows
()
exit
(
0
)
def
put_warning_message
(
fid
,
faceTrackers
,
faceSuspicion
,
resultImage
,
threshold
=
2
):
'''
Print a Warning message depending on suspicion level
'''
tracked_position
=
faceTrackers
[
fid
].
get_position
()
t_x
=
int
(
tracked_position
.
left
())
t_y
=
int
(
tracked_position
.
top
())
t_w
=
int
(
tracked_position
.
width
())
t_h
=
int
(
tracked_position
.
height
())
suspicion_level
=
faceSuspicion
[
fid
]
if
suspicion_level
>
threshold
:
text
=
"ATTENTION! INDIVIDU DANGEREUX!"
cv2
.
putText
(
resultImage
,
text
,
# Name's alignment
(
t_x
+
int
((
t_w
-
len
(
text
)
*
10
)
/
2
+
10
),
int
(
t_y
)
+
20
+
t_h
),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
RED
,
1
,
cv2
.
LINE_AA
)
def
rectangle_around_face
(
fid
,
faceTrackers
,
faceSuspicion
,
draw_person_dangerosity
,
resultImage
):
'''
Draw a rectangle around's people face
...
...
@@ -336,21 +375,25 @@ def rectangle_around_face(fid, faceTrackers, faceSuspicion, draw_person_dangeros
# If we want to have a dangerosity color per personn
if
draw_person_dangerosity
:
suspicionLevel
=
faceSuspicion
[
fid
]
person_color
=
draw_person_dangerosity_color
(
suspicionLevel
)
person_color
=
pick_person_dangerosity_color
(
suspicionLevel
)
# Else default to rectangleColor
else
:
person_color
=
rectangleColor
cv2
.
rectangle
(
resultImage
,
(
t_x
,
t_y
),
(
t_x
+
t_w
,
t_y
+
t_h
),
person_color
,
2
)
# Draw Rectangle
cv2
.
rectangle
(
resultImage
,
(
t_x
,
t_y
),
(
t_x
+
t_w
,
t_y
+
t_h
),
person_color
,
1
)
def
draw
_person_dangerosity_color
(
suspicionLevel
):
def
pick
_person_dangerosity_color
(
suspicionLevel
,
threshold
=
2
):
'''
Compute person level suspicion -> simplified to 2 levels
Can reintroduce the faceSuspicious dictionnary lated, was more clean
'''
if
suspicionLevel
>
2
:
if
suspicionLevel
>
threshold
:
color
=
RED
else
:
color
=
GREEN
...
...
@@ -360,11 +403,11 @@ def draw_person_dangerosity_color(suspicionLevel):
if
__name__
==
'__main__'
:
detectAndTrackMultipleFaces
(
# False: Names are directly printed on screen, no new thread
start_thread
=
False
,
start_thread
=
False
,
# Select only names with letters and whitespace. Any other value will select printable.
charset
=
"letters"
,
charset
=
"letters"
,
# If True, then rectangle color around people' face will turn red if dangerous
# + a message will pop
draw_person_dangerosity
=
True
draw_person_dangerosity
=
True
,
# Add a warning message if dangerous
add_warning_message
=
True
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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