projekt_widzenie/pred_test.py

51 lines
1.3 KiB
Python
Raw Normal View History

2023-01-22 17:15:16 +01:00
# # %% [markdown]
# # # Prediction on the test data
# # %%
import os
import pandas as pd
import numpy as np
import tensorflow as tf
model = tf.keras.models.load_model('model_pred/sign_char_detection_model')
2023-01-22 17:15:16 +01:00
# Get the list of all files and directories
path = "test_data"
dir_list = os.listdir(path)
print(dir_list)
# %%
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
tf.keras.utils.load_img
class_names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'del', 'nothing', 'space']
img_height=256
img_width=256
actual=[]
pred=[]
for i in dir_list:
for j in os.listdir(path+'/'+i):
file_path = path+'/'+i + '/' + j
actual.append(i)
2023-01-22 20:12:33 +01:00
test_image = tf.keras.utils.load_img(file_path, target_size = (200, 200))
2023-01-22 17:15:16 +01:00
test_image = tf.keras.utils.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = model.predict(test_image)
pred.append(class_names[np.argmax(result)])
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import accuracy_score
print("Test accuracy=",accuracy_score(pred,actual))
print("Classification report:\n",classification_report(pred,actual))
# %%