Fire Detection Using Surveillance Camera on Roads¶
Credit: AITS Cainvas Community
Accidents on the road can sometimes lead to a fire that can get worse over time. Fires along the road due to other reasons are also be hazardous to the traffic on the road and nearby places. These fires need to be detected and controlled with utmost urgency in order to maintain the safety of those in the vicinity.
import numpy as np import pandas as pd import matplotlib.pyplot as plt from keras import layers, optimizers, models, preprocessing, losses, callbacks import os import random from PIL import Image import tensorflow as tf import keras
!wget "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/fire.zip" !unzip -qo fire.zip !rm fire.zip
--2021-01-08 07:38:59-- https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/fire.zip Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 220.127.116.11 Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|18.104.22.168|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 102552780 (98M) [application/zip] Saving to: ‘fire.zip’ fire.zip 100%[===================>] 97.80M 97.7MB/s in 1.0s 2021-01-08 07:39:00 (97.7 MB/s) - ‘fire.zip’ saved [102552780/102552780]
The dataset zip file has 3 folders, train, validation and test. Each of these has 2 sub folders - Fire and Non-fire.
# Loading the dataset path = 'fire/' input_shape = (256, 256, 3) # default input shape while loading the images batch = 64 # The train and test datasets print("Train dataset") train_ds = preprocessing.image_dataset_from_directory(path+'Train', batch_size=batch, label_mode='binary') print("Test dataset") test_ds = preprocessing.image_dataset_from_directory(path+'Test', batch_size=batch, label_mode='binary') print("Validation dataset") val_ds = preprocessing.image_dataset_from_directory(path+'Vali', batch_size=batch, label_mode='binary')
Train dataset Found 6003 files belonging to 2 classes. Test dataset Found 2000 files belonging to 2 classes. Validation dataset Found 2000 files belonging to 2 classes.
Lets look into the spread of images across the categories and dataset splits.
# How many samples in each class for t in ['Train', 'Test', 'Vali']: print('\n', t.upper()) for x in os.listdir(path + t): print(x, ' - ', len(os.listdir(path + t + '/' + x)))
TRAIN Non-Fire - 3000 Fire - 3003 TEST Non-Fire - 1000 Fire - 1000 VALI Non-Fire - 1000 Fire - 1000
It is a balanced dataset.
# Looking into the class labels class_names = train_ds.class_names print("Train class names: ", train_ds.class_names) print("Test class names: ", test_ds.class_names) print("Validation class names: ", val_ds.class_names)
Train class names: ['Fire', 'Non-Fire'] Test class names: ['Fire', 'Non-Fire'] Validation class names: ['Fire', 'Non-Fire']
num_samples = 4 # the number of samples to be displayed in each class for x in class_names: plt.figure(figsize=(20, 20)) filenames = os.listdir(path + 'Train/' + x) for i in range(num_samples): ax = plt.subplot(1, num_samples, i + 1) img = Image.open(path +'Train/' + x + '/' + filenames[i]) plt.imshow(img) plt.title(x) plt.axis("off")