Predicting New Images

Contents

Predicting New Images#

Requirements#

Here we gather the required libraries, classes and function for this notebook.

from PIL import Image
import os
import sys
import pickle

PyImageML is a Python package that has been developed under this project, which has several utils for plotting images and extracting features from them, features that later could be used along with Machine Learning algorithms to solve typical ML tasks.

sys.path.insert(0, r"C:\Users\fscielzo\Documents\Packages\PyImageML_Package_Private")
from PyImageML.preprocessing import ImageFeaturesExtraction

PyMachineLearning is another custom Python package that contains efficient utils to be used in real Machine Learning workflows.

sys.path.insert(0, r'C:\Users\fscielzo\Documents\Packages\PyMachineLearning_Package_Private')
from PyMachineLearning.preprocessing import scaler, pca

In this section new images will be classified using the best found pipeline as it was saved before.

# Load the model from file
with open(r'C:\Users\fscielzo\Documents\DataScience-GitHub\Image Analysis\Image-Classification\Fire-Detection\Results\best_pipeline.pkl', 'rb') as file:
    loaded_pipeline = pickle.load(file)

# Building a list with the new images paths
folder_new_images = r'C:\Users\fscielzo\Documents\DataScience-GitHub\Image Analysis\Image-Classification\Fire-Detection\New-Data'
new_images_names = os.listdir(folder_new_images)
new_images_paths = [os.path.join(folder_new_images, file_name) for file_name in new_images_names]

# Get all files in the folder
X_new = new_images_paths

# Predicting the new images with the loaded pipeline
Y_new_hat = loaded_pipeline.predict(X_new)
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 260ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 132ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 126ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 143ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 174ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 130ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 134ms/step
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 147ms/step

Plotting the images along with the model predictions.

for i, (img, img_name) in enumerate(zip(X_new, new_images_names)):
    print(f'--------------------------\nPredicting {img_name}\n')
    if Y_new_hat[i] == 1:
        print('Result: Fire detected\n--------------------------')
    else:
        print('Result: Fire not detected\n--------------------------')
    display(Image.open(img).resize((500, 350)))
--------------------------
Predicting img1.png

Result: Fire detected
--------------------------
_images/4005f024f58612ec82f0f61cdcd2354695341a2d2fd792e37121a2e0bd415c5b.png
--------------------------
Predicting img2.jpg

Result: Fire detected
--------------------------
_images/bf5f7c81ab18954e2c1e38af5c21147056613facb4a9633e70ac99a134e26fc4.png
--------------------------
Predicting img3.jpg

Result: Fire detected
--------------------------
_images/bef54590ed52cdcb26b3a2230b8d0a2b0db84b73dd191d4c8cc42eb46f40d621.png
--------------------------
Predicting img4.jpg

Result: Fire not detected
--------------------------
_images/d5f465c52a757c4ee580d8148ac41bdf0766b58fe41d583eb9bf26cefb24d4d2.png
--------------------------
Predicting img5.jpg

Result: Fire not detected
--------------------------
_images/4a85f015524c3315f872ce8567f35ad13039bce63e5150515b8651f070c4f749.png
--------------------------
Predicting img6.jpg

Result: Fire not detected
--------------------------
_images/6a6483b19d0272eabf84ce181a679a199368834614f99e3fbf0f27bb48e2d33f.png
--------------------------
Predicting img7.jpg

Result: Fire not detected
--------------------------
_images/a842fbb139ff3a77eed930d8247a304e1f1e1374f74e2d8dce6cd627902ceed9.png
--------------------------
Predicting img8.jpg

Result: Fire detected
--------------------------
_images/c5d586c8776e5108d18eca7e64575c0dd4ec926e790d68f28d24ba3f043ee28f.png

All the new images have been classified correctly.