NOTE: This Use Case is not purposed for resource constrained devices.
Fire Detection Using Surveillance Camera on Roads¶
Credit: AITS Cainvas Community
Photo by Aslan Almukhambetov on Dribbble
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.
In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras import layers, optimizers, models, preprocessing, losses, callbacks
import os
import random
from PIL import Image
import tensorflow as tf
import tensorflow.keras
In [2]:
!wget "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/fire.zip"
!unzip -qo fire.zip
!rm fire.zip
The dataset zip file has 3 folders, train, validation and test. Each of these has 2 sub folders - Fire and Non-fire.
In [3]:
# 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')
Lets look into the spread of images across the categories and dataset splits.
In [4]:
# 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)))
It is a balanced dataset.
In [5]:
# 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)
Visualization¶
In [6]:
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")