Sagnik Ghosh
Published © GPL3+

Smart fire detection using opencv and python

Consistently individuals bring about an enormous loss of property a life because of fire and blasts. Fire being one of the savage component

IntermediateShowcase (no instructions)5 hours8,506
Smart fire detection using opencv and python

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Low Voltage Transformer, Class II
Low Voltage Transformer, Class II
×1
Solenoid Valve, 2 Way
Solenoid Valve, 2 Way
×1
Relay (generic)
×1
Single Turn Potentiometer- 10k ohms
Single Turn Potentiometer- 10k ohms
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
Capacitor 10 µF
Capacitor 10 µF
×1
Buzzer
Buzzer
×1

Software apps and online services

Arduino IDE
Arduino IDE
Jupyter Notebook
Jupyter Notebook
OpenCV
OpenCV

Story

Read more

Schematics

Block Diagram

Code

Arduino code

Arduino
void setup() {
 Serial.begin(9600);
 pinMode(A0,INPUT);
 pinMode(A1,INPUT);
 pinMode(A2,INPUT);
 pinMode(8,OUTPUT);
 pinMode(7,OUTPUT);
 
}

void loop() {
  if(Serial.available()>0){
    char ser = Serial.read();
    Serial.println(ser);
    if(ser == 'A'){
      digitalWrite(7,HIGH);
      delay(5000);
   digitalWrite(8,HIGH);
   delay(1000);
    Serial.println("*");
      Serial.println("FIRE DETECTED");
      Serial.println("#");
    }
  }
    int gas=analogRead(A0);
    Serial.println("*");
    Serial.print("GAS = ");
    Serial.println(gas);
    delay(500);
    Serial.println("#");
    
    if(gas >250){
      digitalWrite(7,1);
      delay(2000);
      digitalWrite(7,0);
      
      digitalWrite(8,1);
      delay(2000);
      digitalWrite(8,0);
      Serial.println("*");
    Serial.print("GAS DETECTED ");
     
    Serial.println("*");
    delay(500);
    }
     Serial.println("*");
    int v1 = analogRead(A1);
    Serial.println("Current value ");
    delay(500);
    //Serial.println(v1);
int    v3=(v1/20.805056);
    Serial.print(v3);
        delay(500);
    Serial.println("#");
    
    Serial.println("*");
    int v2 = analogRead(A2);
    Serial.print("Voltage value");
    delay(500);
    //Serial.println(v2);
    int v4=(v2/20.805056);
    
    Serial.print(v4);
    if(v4>13||v3>5)
    {
      Serial.print("CIRUIT TRIPPED");
         }
         else{
           digitalWrite(8,LOW);
      digitalWrite(7,LOW);
          }
    
    delay(500);
    Serial.println("#");
    

}

Opencv code

Python
import cv2
import numpy as np
import serial
 
video_file = r"C:\Users\hp\Downloads\water 20 second video.mp4"
video = cv2.VideoCapture(video_file)
ser = serial.Serial('COM5')
 
while True:
    (grabbed, frame) = video.read()
    if not grabbed:
        break
 
    blur = cv2.GaussianBlur(frame, (21, 21), 0)
    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
 
    lower = [18, 50, 50]
    upper = [35, 255, 255]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")
    mask = cv2.inRange(hsv, lower, upper)
    
 
 
    output = cv2.bitwise_and(frame, hsv, mask=mask)
    no_red = cv2.countNonZero(mask)
    cv2.imshow("output", output)
    #print("output:", frame)
    if int(no_red) > 20000:
        print ('Fire detected')
        ser.write(b'A')
    #print(int(no_red))
   #print("output:".format(mask))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
video.release()

Credits

Sagnik Ghosh

Sagnik Ghosh

4 projects • 38 followers
DIY enthusiast- Crazy Engineer,Mathematics is Love,Projects on Embedded S/W, Electronics, RC Vehicles.

Comments