Untitled

 avatar
unknown
plain_text
10 months ago
2.5 kB
3
Indexable
import cv2
import numpy as np
import smtplib
import pygame
import threading

pygame.init()
pygame.mixer.init()
alarm_sound = pygame.mixer.Sound('alarm-sound.mp3')

Alarm_Status = False
Email_Status = False
Fire_Reported = 0

def play_alarm_sound_function():
    pygame.mixer.Sound.play(alarm_sound)
    while pygame.mixer.get_busy():  # Așteaptă până când muzica se termină
        pygame.time.Clock().tick(10)

def send_mail_function():
    recipientEmail = "Enter_Recipient_Email"
    recipientEmail = recipientEmail.lower()

    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        server.login("Enter_Your_Email", 'Enter_Your_Email_Password')
        server.sendmail("Enter_Your_Email", recipientEmail, "Warning: A Fire Accident has been reported at ABC Company")
        print("Email sent to {}".format(recipientEmail))
        server.close()
    except Exception as e:
        print(e)

def detect_fire(frame):
    blur = cv2.GaussianBlur(frame, (21, 21), 0)
    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
    lower_red = np.array([0, 150, 150])
    upper_red = np.array([10, 255, 255])
    mask_red = cv2.inRange(hsv, lower_red, upper_red)
    
    lower_orange = np.array([10, 100, 100])
    upper_orange = np.array([25, 255, 255])
    mask_orange = cv2.inRange(hsv, lower_orange, upper_orange)
    
    lower_yellow = np.array([25, 100, 100])
    upper_yellow = np.array([35, 255, 255])
    mask_yellow = cv2.inRange(hsv, lower_yellow, upper_yellow)
    
    full_mask = mask_red + mask_orange + mask_yellow
    output = cv2.bitwise_and(frame, frame, mask=full_mask)
    
    return output, full_mask

# Initialize video capture
video = cv2.VideoCapture("ma4.mp4")

while True:
    (grabbed, frame) = video.read()
    if not grabbed:
        break

    frame = cv2.resize(frame, (960, 540))

    output, mask = detect_fire(frame)
    no_red = cv2.countNonZero(mask)

    cv2.imshow("Fire Detection", output)

    if int(no_red) > 15000:
        Fire_Reported += 1

    if Fire_Reported >= 1:
        if not Alarm_Status:
            threading.Thread(target=play_alarm_sound_function).start()
            Alarm_Status = True
        
        if not Email_Status:
            threading.Thread(target=send_mail_function).start()
            Email_Status = True

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
video.release()
Editor is loading...
Leave a Comment