Commit f321244b authored by cb's avatar cb

Added an option to put a warning message below the face if suspicious

parent e1c72495
...@@ -19,7 +19,7 @@ import randomnames ...@@ -19,7 +19,7 @@ import randomnames
#Make sure that you copy this file from the opencv project to the root of this #Make sure that you copy this file from the opencv project to the root of this
#project folder #project folder
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
print( faceCascade.empty() )
#The deisred output width and height #The deisred output width and height
OUTPUT_SIZE_WIDTH = 775 OUTPUT_SIZE_WIDTH = 775
OUTPUT_SIZE_HEIGHT = 600 OUTPUT_SIZE_HEIGHT = 600
...@@ -45,7 +45,11 @@ def calculateAverageSuspicious(faceSuspicion): ...@@ -45,7 +45,11 @@ def calculateAverageSuspicious(faceSuspicion):
averageSuspicious = sum(faceSuspicion)/currentFaces averageSuspicious = sum(faceSuspicion)/currentFaces
print(f"Average suspicion: {averageSuspicious}") 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 #Open the first webcame device
capture = cv2.VideoCapture(0) capture = cv2.VideoCapture(0)
...@@ -217,6 +221,7 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity): ...@@ -217,6 +221,7 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
# 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 # Advantage is that we can pop an "Identifying..." Message
# Disadvantage is that is more "lourd"
if start_thread : 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.
...@@ -245,6 +250,7 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity): ...@@ -245,6 +250,7 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
rectangle_around_face(fid, rectangle_around_face(fid,
faceTrackers, faceTrackers,
faceSuspicion, faceSuspicion,
# If False, colors will not change with suspicion level
draw_person_dangerosity, draw_person_dangerosity,
resultImage) resultImage)
...@@ -271,30 +277,40 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity): ...@@ -271,30 +277,40 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
width = resultImage.shape[1] width = resultImage.shape[1]
cv2.rectangle(resultImage, (int(width-50), 0), (int(width), 120), color, -1) cv2.rectangle(resultImage, (int(width-50), 0), (int(width), 120), color, -1)
# If Known -> print Name
if fid in faceNames.keys(): if fid in faceNames.keys():
# Name's alignment
cv2.putText(resultImage, cv2.putText(resultImage,
faceNames[fid] , faceNames[fid] ,
(t_x+ int((t_w - len(faceNames[fid])*10)/2 +2), int(t_y)), # Name's alignment
cv2.FONT_HERSHEY_SIMPLEX, (t_x+ int((t_w - len(faceNames[fid])*10)/2 + 10), int(t_y) - 10),
0.5, cv2.FONT_HERSHEY_SIMPLEX,
(255, 255, 255), 0.5,
1, (255, 255, 255),
cv2.LINE_AA) 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: else:
# Detecting
text = "Identifying" text = "Identifying"
cv2.putText(resultImage, cv2.putText(resultImage,
text , 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, cv2.FONT_HERSHEY_SIMPLEX,
0.5, 0.5,
(255, 255, 255), (255, 255, 255),
1, 1,
cv2.LINE_AA) cv2.LINE_AA)
#Since we want to show something larger on the screen than the #Since we want to show something larger on the screen than the
#original 320x240, we resize the image again #original 320x240, we resize the image again
# #
...@@ -308,9 +324,6 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity): ...@@ -308,9 +324,6 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
cv2.imshow("base-image", baseImage) cv2.imshow("base-image", baseImage)
cv2.imshow("result-image", resultImage) #largeResult) cv2.imshow("result-image", resultImage) #largeResult)
#To ensure we can also deal with the user pressing Ctrl-C in the console #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 #we have to check for the KeyboardInterrupt exception and break out of
#the main loop #the main loop
...@@ -321,6 +334,32 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity): ...@@ -321,6 +334,32 @@ def detectAndTrackMultipleFaces(start_thread, charset, draw_person_dangerosity):
cv2.destroyAllWindows() cv2.destroyAllWindows()
exit(0) 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): def rectangle_around_face(fid, faceTrackers, faceSuspicion, draw_person_dangerosity, resultImage):
''' '''
Draw a rectangle around's people face Draw a rectangle around's people face
...@@ -336,21 +375,25 @@ def rectangle_around_face(fid, faceTrackers, faceSuspicion, draw_person_dangeros ...@@ -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 we want to have a dangerosity color per personn
if draw_person_dangerosity : if draw_person_dangerosity :
suspicionLevel = faceSuspicion[fid] suspicionLevel = faceSuspicion[fid]
person_color = draw_person_dangerosity_color(suspicionLevel) person_color = pick_person_dangerosity_color(suspicionLevel)
# Else default to rectangleColor # Else default to rectangleColor
else: else:
person_color = rectangleColor person_color = rectangleColor
cv2.rectangle(resultImage, (t_x, t_y), # Draw Rectangle
(t_x + t_w , t_y + t_h), cv2.rectangle(resultImage,
person_color,2) (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 Compute person level suspicion -> simplified to 2 levels
Can reintroduce the faceSuspicious dictionnary lated, was more clean Can reintroduce the faceSuspicious dictionnary lated, was more clean
''' '''
if suspicionLevel>2: if suspicionLevel> threshold:
color = RED color = RED
else: else:
color = GREEN color = GREEN
...@@ -360,11 +403,11 @@ def draw_person_dangerosity_color(suspicionLevel): ...@@ -360,11 +403,11 @@ def draw_person_dangerosity_color(suspicionLevel):
if __name__ == '__main__': if __name__ == '__main__':
detectAndTrackMultipleFaces( detectAndTrackMultipleFaces(
# False: Names are directly printed on screen, no new thread # 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. # 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 # 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
) )
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