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
2b799a4c
Commit
2b799a4c
authored
Jan 24, 2020
by
klorydryk
Browse files
replace average rectangle by gradient, and use french logo as cursor
parent
c0aa4f99
Changes
2
Hide whitespace changes
Inline
Side-by-side
logo.png
0 → 100644
View file @
2b799a4c
12.9 KB
trackmultiplefaces.py
View file @
2b799a4c
...
...
@@ -6,7 +6,7 @@
#Import the OpenCV and dlib libraries
import
cv2
import
dlib
from
colour
import
Color
import
threading
import
time
...
...
@@ -29,9 +29,16 @@ WHITE =(255, 255, 255)
RED
=
(
0
,
0
,
255
)
BLUE
=
(
255
,
0
,
0
)
PURPLE
=
(
127
,
0
,
255
)
ORANGE
=
(
0
,
165
,
255
)
GREEN
=
(
0
,
255
,
0
)
suspiciousColors
=
{
0
:(
0
,
0
,
0
),
1
:
GREEN
,
2
:
PURPLE
,
3
:
RED
}
suspiciousColors
=
{
0
:(
0
,
0
,
0
),
1
:
GREEN
,
2
:
ORANGE
,
3
:
RED
}
avSusRectWidth
=
50
cursorSize
=
(
50
,
20
)
cursor
=
cv2
.
imread
(
'logo.png'
)
# Read the file
cursor
=
cv2
.
resize
(
cursor
,
cursorSize
)
averageSuspicious
=
0
...
...
@@ -43,13 +50,34 @@ rectangleColor = (0,165,255)
rectangleColorSuspicious
=
RED
# Suspicious threshold
SUSPICIOUS_THRESHOLD
=
2
SUSPICIOUS_THRESHOLD
=
2
#We are not doing really face recognition
def
doRecognizePerson
(
faceNames
,
fid
):
time
.
sleep
(
2
)
faceNames
[
fid
]
=
randomnames
.
rand_name
(
charset
)
#"Person " + str(fid)
def
displayGradientOn
(
resultImage
,
value
):
red
=
Color
(
"red"
)
orange
=
Color
(
"orange"
)
colors
=
list
(
red
.
range_to
(
orange
,
10
))
colors
+=
list
(
orange
.
range_to
(
Color
(
"green"
),
10
))
width
=
resultImage
.
shape
[
1
]
pixelColorSize
=
5
i
=
0
for
color
in
colors
:
h
=
color
.
hex
.
lstrip
(
'#'
)
if
(
len
(
h
)
==
6
):
cv2
.
rectangle
(
resultImage
,
(
int
(
width
-
avSusRectWidth
),
i
*
pixelColorSize
),
(
int
(
width
),
(
i
+
1
)
*
pixelColorSize
),
tuple
(
int
(
h
[
j
:
j
+
2
],
16
)
for
j
in
(
4
,
2
,
0
)),
-
1
)
else
:
cv2
.
rectangle
(
resultImage
,
(
int
(
width
-
avSusRectWidth
),
i
*
pixelColorSize
),
(
int
(
width
),
(
i
+
1
)
*
pixelColorSize
),
tuple
(
int
(
h
[
j
:
j
+
2
],
16
)
for
j
in
(
2
,
1
,
0
)),
-
1
)
i
+=
1
x_offset
=
width
-
avSusRectWidth
y_offset
=
int
((
i
)
*
pixelColorSize
*
(
2
-
(
value
-
1
))
/
2
)
resultImage
[
y_offset
:
y_offset
+
cursor
.
shape
[
0
],
x_offset
:
x_offset
+
cursor
.
shape
[
1
]]
=
cursor
def
calculateAverageSuspicious
(
faceSuspicion
):
global
averageSuspicious
...
...
@@ -58,8 +86,8 @@ def calculateAverageSuspicious(faceSuspicion):
averageSuspicious
=
sum
(
faceSuspicion
)
/
currentFaces
print
(
f
"Average suspicion:
{
averageSuspicious
}
"
)
def
detectAndTrackMultipleFaces
(
start_thread
,
charset
,
def
detectAndTrackMultipleFaces
(
start_thread
,
charset
,
draw_person_dangerosity
,
add_warning_message
):
...
...
@@ -111,12 +139,12 @@ def detectAndTrackMultipleFaces(start_thread,
#overlayed rectangle for the largest face
if
FULLSCREEN
==
True
:
resultImage
=
fullSizeBaseImage
.
copy
()
cv2
.
setWindowProperty
(
'result-image'
,
cv2
.
setWindowProperty
(
'result-image'
,
cv2
.
WND_PROP_FULLSCREEN
,
cv2
.
WINDOW_FULLSCREEN
)
else
:
resultImage
=
baseImage
.
copy
()
cv2
.
setWindowProperty
(
'result-image'
,
cv2
.
setWindowProperty
(
'result-image'
,
cv2
.
WND_PROP_FULLSCREEN
,
cv2
.
WINDOW_NORMAL
)
...
...
@@ -229,20 +257,20 @@ def detectAndTrackMultipleFaces(start_thread,
faceTrackers
[
currentFaceID
]
=
tracker
# Pick up random name: Do we want a thread for that?
# 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.
#Start a new thread that is used to simulate face recognition.
t
=
threading
.
Thread
(
target
=
doRecognizePerson
,
args
=
(
faceNames
,
currentFaceID
))
t
.
start
()
else
:
# Directly add name + pass a charset to which name must belong
# Directly add name + pass a charset to which name must belong
# "Person " + str(fid)
faceNames
[
currentFaceID
]
=
randomnames
.
rand_name
(
charset
)
faceNames
[
currentFaceID
]
=
randomnames
.
rand_name
(
charset
)
calculateAverageSuspicious
(
faceSuspicion
)
...
...
@@ -257,11 +285,11 @@ def detectAndTrackMultipleFaces(start_thread,
for
fid
in
faceTrackers
.
keys
():
# Draw a rectangle around people's face
rectangle_around_face
(
fid
,
faceTrackers
,
rectangle_around_face
(
fid
,
faceTrackers
,
faceSuspicion
,
# If False, colors will not change with suspicion level
draw_person_dangerosity
,
draw_person_dangerosity
,
resultImage
)
# Deal with the two others rectangles
...
...
@@ -272,52 +300,58 @@ def detectAndTrackMultipleFaces(start_thread,
t_w
=
int
(
tracked_position
.
width
())
t_h
=
int
(
tracked_position
.
height
())
cv2
.
rectangle
(
resultImage
,
(
t_x
+
t_w
,
t_y
),
(
t_x
+
t_w
+
30
,
t_y
+
80
),
suspiciousColors
[
faceSuspicion
[
fid
]],
-
1
)
#
cv2.rectangle(resultImage,
#
(t_x+t_w, t_y),
#
(t_x+t_w+30, t_y+80),
#
suspiciousColors[faceSuspicion[fid]],
#
-1)
if
averageSuspicious
>
2.5
:
color
=
RED
elif
averageSuspicious
>
1.5
:
color
=
PURPL
E
else
:
color
=
GREEN
#
if averageSuspicious>2.5:
#
color = RED
#
elif averageSuspicious>1.5:
#
color =
ORANG
E
#
else:
#
color = GREEN
width
=
resultImage
.
shape
[
1
]
cv2
.
rectangle
(
resultImage
,
(
int
(
width
-
50
),
0
),
(
int
(
width
),
120
),
color
,
-
1
)
# If Known -> print Name
# width = resultImage.shape[1]
# cv2.rectangle(resultImage, (int(width-50), 0), (int(width), 120), color, -1)
displayGradientOn
(
resultImage
,
averageSuspicious
)
# TODO optimization ?
# x_offset = 50#width-avSusRectWidth
# y_offset = 100
# resultImage[y_offset:y_offset+cursor.shape[0], x_offset:x_offset+cursor.shape[1]] = cursor
# If Known -> print Name
if
fid
in
faceNames
.
keys
():
cv2
.
putText
(
resultImage
,
cv2
.
putText
(
resultImage
,
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
,
NAME_COLOR
,
1
,
0.5
,
NAME_COLOR
,
1
,
cv2
.
LINE_AA
)
# Add a warning message depending on suspicious level
if
add_warning_message
:
put_warning_message
(
fid
,
faceTrackers
,
faceSuspicion
,
put_warning_message
(
fid
,
faceTrackers
,
faceSuspicion
,
resultImage
)
# Else print Identifying
else
:
text
=
"Identifying"
cv2
.
putText
(
resultImage
,
cv2
.
putText
(
resultImage
,
text
,
(
t_x
+
int
((
t_w
-
len
(
text
)
*
10
)
/
2
+
10
),
int
(
t_y
)),
cv2
.
FONT_HERSHEY_SIMPLEX
,
0.5
,
IDENTIFYING_COLOR
,
1
,
0.5
,
IDENTIFYING_COLOR
,
1
,
cv2
.
LINE_AA
)
#Since we want to show something larger on the screen than the
...
...
@@ -353,19 +387,19 @@ def put_warning_message(fid, faceTrackers, faceSuspicion, resultImage):
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
>
SUSPICIOUS_THRESHOLD
:
text
=
"ATTENTION! PERSONNE DANGEREUSE!"
cv2
.
putText
(
resultImage
,
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
,
0.5
,
RED
,
1
,
cv2
.
LINE_AA
)
...
...
@@ -381,17 +415,17 @@ def rectangle_around_face(fid, faceTrackers, faceSuspicion, draw_person_dangeros
t_w
=
int
(
tracked_position
.
width
())
t_h
=
int
(
tracked_position
.
height
())
# If we want to have a dangerosity color per personn
# If we want to have a dangerosity color per personn
if
draw_person_dangerosity
:
suspicionLevel
=
faceSuspicion
[
fid
]
person_color
=
pick_person_dangerosity_color
(
suspicionLevel
)
person_color
=
suspiciousColors
[
suspicionLevel
]
#
pick_person_dangerosity_color(suspicionLevel)
# Else default to rectangleColor
else
:
person_color
=
rectangleColor
# Draw Rectangle
cv2
.
rectangle
(
resultImage
,
cv2
.
rectangle
(
resultImage
,
(
t_x
,
t_y
),
(
t_x
+
t_w
,
t_y
+
t_h
),
person_color
,
...
...
@@ -412,7 +446,7 @@ def pick_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"
,
# If True, then rectangle color around people' face will turn red if dangerous
...
...
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