I need help with adding a notification /alert system in my program

I cloned this repo for facemask detection using yolov8. I want to add an alert/notification system whenever a person without a mask is detected. this is my first python project that i am trying to figure out so im not sure how to use a conditional statement because i dont know what is the variable used for “no mask”. please guide me.

def DetectMask(self, frame):
        if not self.b_DetectMask:
            results = self.model.predict(source=frame, show=False, save=False, conf=0.5)
            detect_image = results[0].plot()
        else:
            detect_image = inference(self.engine, frame, self.b_DetectMask)
        if self.comboBox.currentIndex() != 2:
            detect_image = cv2.resize(detect_image, (int(detect_image.shape[1]*self.ViewRadio), int(detect_image.shape[0]*self.ViewRadio)),
                                      interpolation=cv2.INTER_CUBIC)
        detect_frame = cv2.cvtColor(detect_image, cv2.COLOR_BGR2RGB) # yolov8 result image is BGR(for opencv display)
        detect_frame = QImage(detect_frame, detect_image.shape[1], detect_image.shape[0], QImage.Format_RGB888)
        return detect_frame

What actually are you trying to figure out?

That is: when you try using the code, what happens, and how is that different from what you want to happen? Try to get as much detail on this as you can, first.

The model is detecting (1) face with mask (2) face with no mask (3) face with mask wore incorrectly from the laptop’s camera in live feed. I want a notification whenever a face without a mask is detected and the program should keep running to detect next frame. Now i dont really know where to put a audio file to be played when the face with no mask is detected

Two ways to go about this. First just load up an audio player and play it. Here is a good tutorial on that: Playing and Recording Sound in Python – Real Python

Second would be to use something like PyGame, it will help you run the full application with an event loop, ui, and all that. Tutorial on that here: Python Programming Tutorials

Hello,

from:

the variable/attribute used for detection appears to be b_DetectMask from the code that you have provided. Though it does not have to be written verbatim as you have provided it. Do use it as a guide, however.

Using if/else conditional statements, as per your stated detection method, you can use something along the lines of (just an example to get you started):

NO_MASK = 0
YES_MASK = 1
MASK_INCORRECTLY = 2

b_DetectMask = 6  # Try different values for testing purposes

if b_DetectMask == NO_MASK:
    print('No mask was detected!')  # for testing purposes only
    # Add code here of what you want to happen when this
    # condition is met
    # Include a notification whenever a face without a mask is detected

elif b_DetectMask == YES_MASK:
    print('Mask detected correctly!')  # for testing purposes only
    # Add code here of what you want to happen when this
    # condition is met


elif b_DetectMask == MASK_INCORRECTLY:
    print('Mask worn incorrectly!')  # for testing purposes only
    # Add code here of what you want to happen when this
    # condition is met

else:
    print('Error in detection.  Try again')  # for testing purposes only
    # Add code here of what you want to happen when this
    # condition is met

For the example provided, I stripped the self to focus only on the process of the conditional statements for testing purposes so that you can get the feel for them. Change the value of the b_DetectMask variable to test the script and observe what happens for different values.

if/else conditional statements are a topic that generally precedes the topics of functions, methods, and classes, to name a few, but of which you will have to understand for developing this code (from the test code snippet that you have provided). Note that the def DetectMask function is referred to as a method when part of a class - just pointing this out since you stated that you are uneasy about conditional statements and classes are a more advanced topic. Hope you understand the subtleties and how methods are referenced via class instances since as you stated that you cloned this repo for facemask detection using yolov8. It is nice to reference other code but even better if you understand it.

Good luck.

1 Like

Hi. thank you for the explanation. i just studied it a bit more and realized that this code in another file was creating the bounding boxes and actually giving the output. now i can print a string if a mask is detected or not but if i try to add a notification sound it just messes with the loop and breaks the program. the cpu usage goes to 100% and the window gets stuck on first frame loaded. is there anyway to optimize it?

        if mask_detect_mode:
            cls = OBJECT_MASK_CLASSES[cls_id]
            #print('cls: ',cls)
            color = OBJECT_MASK_COLORS[cls]
            pygame.init()
            alert_sound = pygame.mixer.Sound(r"D:\Project 3\notification.wav")
            while True:
                if cls == "No Mask":
                    alert_sound.play()
                    time.sleep(0.1)
                    # Display text on the left top of the opened camera window in red color
                    #cv2.putText(draw, "No Mask Detected !!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), thickness=2)
                elif cls == "Bad Mask":
                    # Display text on the left top of the opened camera window in orange color
                    cv2.putText(draw, "Bad Mask Detected !!", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 165, 255), thickness=2)

Hello,

I see that you have added additional references into your code including pygame. Unfortunately, I am not familiar with this module. To debug your program, you can start by adding print statements along the trajectory of your code to pin point the source of the error.

Does it have to be a sound response? If you are ok with a visual, you can try a pop-up response as in the following snippet.

import tkinter as tk

def mask_detection(mask_status):

    root = tk.Tk()
    root.title("MASK DETECTION")
    root.minsize(265, 50)

    match mask_status:

        case 0:
            
            text = tk.Label(root, text = 'FAIL!')
            root.configure(background=  'red')

        case 1:
            text = tk.Label(root, text = 'PASS')
            root.configure(background=  'green')
            
        case 2:
             text = tk.Label(root, text = 'ADJUST MASK')
             root.configure(background=  'orange')

        case _:
            
             text = tk.Label(root, text = 'ERROR IN DETECTION. TRY AGAIN')

    text.config(font = ("Times", 22))
    text.pack(padx = 40, pady = 40)
    root.after(2000, root.destroy)

NO_MASK = 0
YES_MASK = 1
MASK_INCORRECTLY = 2

b_DetectMask = 1              # Try different values for testing purposes
mask_detection(b_DetectMask)

Good Evening. I have also tried popups using this method and windows notifications using win10toast. All of them crash the program at the first frame and it stops responding until i have to force shut down.

The best bet is the text on the window itself as it works and doesnt crash the program.

This method does not crash. Run it using IDLE. Just copy and paste it on a new file and run it.

i will try it when i get home. Thank you

I got it to work by changing up the syntax a bit.