-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict_fire.py
54 lines (45 loc) · 1.54 KB
/
predict_fire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# USAGE
# python predict_fire.py
# import the necessary packages
from tensorflow.keras.models import load_model
import config
from imutils import paths
import numpy as np
import imutils
import random
import cv2
import os
# load the trained model from disk
print("[INFO] loading model...")
model = load_model(config.MODEL_PATH)
# grab the paths to the fire and non-fire images, respectively
print("[INFO] predicting...")
firePaths = list(paths.list_images(config.FIRE_PATH))
nonFirePaths = list(paths.list_images(config.NON_FIRE_PATH))
# combine the two image path lists, randomly shuffle them, and sample
# them
imagePaths = firePaths + nonFirePaths
random.shuffle(imagePaths)
imagePaths = imagePaths[:config.SAMPLE_SIZE]
# loop over the sampled image paths
for (i, imagePath) in enumerate(imagePaths):
# load the image and clone it
image = cv2.imread(imagePath)
output = image.copy()
# resize the input image to be a fixed 128x128 pixels, ignoring
# aspect ratio
image = cv2.resize(image, (128, 128))
image = image.astype("float32") / 255.0
# make predictions on the image
preds = model.predict(np.expand_dims(image, axis=0))[0]
j = np.argmax(preds)
label = config.CLASSES[j]
# draw the activity on the output frame
text = label if label == "Non-Fire" else "WARNING! Fire!"
output = imutils.resize(output, width=500)
cv2.putText(output, text, (35, 50), cv2.FONT_HERSHEY_SIMPLEX,
1.25, (0, 255, 0), 5)
# write the output image to disk
filename = "{}.png".format(i)
p = os.path.sep.join([config.OUTPUT_IMAGE_PATH, filename])
cv2.imwrite(p, output)