Spaces:
Sleeping
Sleeping
Create Predictive.py
Browse files- Predictive.py +58 -0
Predictive.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
import glob
|
| 4 |
+
from tensorflow.keras.models import Sequential, load_model
|
| 5 |
+
from sklearn.preprocessing import LabelBinarizer
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from tensorflow.keras.utils import to_categorical # Correct import for TensorFlow 2.4.0
|
| 8 |
+
|
| 9 |
+
global size
|
| 10 |
+
size = 100
|
| 11 |
+
model = Sequential()
|
| 12 |
+
model = load_model('D:/Downloads/potholes/sample.h5')
|
| 13 |
+
|
| 14 |
+
# Load Testing data: non-pothole
|
| 15 |
+
nonPotholeTestImages = glob.glob("D:/Downloads/potholes/plain/*.jpg")
|
| 16 |
+
test2 = [cv2.imread(img, 0) for img in nonPotholeTestImages]
|
| 17 |
+
for i in range(0, len(test2)):
|
| 18 |
+
test2[i] = cv2.resize(test2[i], (size, size))
|
| 19 |
+
temp4 = np.asarray(test2)
|
| 20 |
+
|
| 21 |
+
# Load Testing data: potholes
|
| 22 |
+
potholeTestImages = glob.glob("D:/Downloads/potholes/pot/*.jpg")
|
| 23 |
+
test1 = [cv2.imread(img, 0) for img in potholeTestImages]
|
| 24 |
+
for i in range(0, len(test1)):
|
| 25 |
+
test1[i] = cv2.resize(test1[i], (size, size))
|
| 26 |
+
temp3 = np.asarray(test1)
|
| 27 |
+
|
| 28 |
+
X_test = []
|
| 29 |
+
X_test.extend(temp3)
|
| 30 |
+
X_test.extend(temp4)
|
| 31 |
+
X_test = np.asarray(X_test)
|
| 32 |
+
|
| 33 |
+
X_test = X_test.reshape(X_test.shape[0], size, size, 1)
|
| 34 |
+
|
| 35 |
+
# Prepare labels for testing data
|
| 36 |
+
y_test1 = np.ones([temp3.shape[0]], dtype=int)
|
| 37 |
+
y_test2 = np.zeros([temp4.shape[0]], dtype=int)
|
| 38 |
+
|
| 39 |
+
y_test = []
|
| 40 |
+
y_test.extend(y_test1)
|
| 41 |
+
y_test.extend(y_test2)
|
| 42 |
+
y_test = np.asarray(y_test)
|
| 43 |
+
|
| 44 |
+
y_test = to_categorical(y_test) # Use to_categorical for label encoding
|
| 45 |
+
|
| 46 |
+
# Predict the classes
|
| 47 |
+
tests = model.predict(X_test)
|
| 48 |
+
tests = np.argmax(tests, axis=1) # Convert probabilities to class labels (0 or 1)
|
| 49 |
+
|
| 50 |
+
for i in range(len(X_test)):
|
| 51 |
+
print(">>> Predicted=%s" % (tests[i]))
|
| 52 |
+
|
| 53 |
+
# Optionally, evaluate the model
|
| 54 |
+
# metrics = model.evaluate(X_test, y_test)
|
| 55 |
+
# for metric_i in range(len(model.metrics_names)):
|
| 56 |
+
# metric_name = model.metrics_names[metric_i]
|
| 57 |
+
# metric_value = metrics[metric_i]
|
| 58 |
+
# print('{}: {}'.format(metric_name, metric_value))
|