0% found this document useful (0 votes)
2 views3 pages

Code

This document contains a Python script for a mask detection system using a Raspberry Pi. It utilizes OpenCV for face detection and controls an LED and buzzer based on whether a mask is detected. The script also updates an LCD display with the detection status and allows for exiting the program with the 'q' key.

Uploaded by

brajeswar26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Code

This document contains a Python script for a mask detection system using a Raspberry Pi. It utilizes OpenCV for face detection and controls an LED and buzzer based on whether a mask is detected. The script also updates an LCD display with the detection status and allows for exiting the program with the 'q' key.

Uploaded by

brajeswar26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import cv2

import [Link] as GPIO

from RPLCD import CharLCD

from RPLCD.i2c import CharLCD as LCD_I2C

from time import sleep

# GPIO setup

LED_PIN = 18

BUZZER_PIN = 12

[Link]([Link])

[Link](LED_PIN, [Link])

[Link](BUZZER_PIN, [Link])

# LCD setup

lcd = CharLCD(pin_rs=17, pin_e=27, pins_data=[22, 23, 24, 25],


numbering_mode=[Link])

# Initialize camera

camera = [Link](0)

# Load pre-trained face detection model (replace with your mask


detection model)

face_cascade = [Link]([Link] +
'haarcascade_frontalface_default.xml')

def update_lcd(message):

[Link]()

lcd.write_message(message)

def detect_mask(frame):
gray = [Link](frame, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1,


minNeighbors=5, minSize=(30, 30))

return faces

try:

while True:

ret, frame = [Link]()

if not ret:

print("Failed to grab frame")

break

faces = detect_mask(frame)

mask_detected = len(faces) > 0

if mask_detected:

[Link](LED_PIN, [Link]) # Turn on LED

[Link](BUZZER_PIN, [Link]) # Turn on Buzzer

update_lcd('Mask Detected')

else:

[Link](LED_PIN, [Link]) # Turn off LED

[Link](BUZZER_PIN, [Link]) # Turn off Buzzer

update_lcd('No Mask Detected')

[Link]('Mask Detection', frame)

# Exit on 'q' key

if [Link](1) & 0xFF == ord('q'):

break
finally:

[Link]()

[Link]()

[Link]()

You might also like