Neural Network - #4
This commit is contained in:
parent
f4ffb21a27
commit
7cd48f5cba
@ -11,23 +11,52 @@ model = keras.models.load_model('trained_model.h5')
|
|||||||
class_names = ['Empty', 'Food','People']
|
class_names = ['Empty', 'Food','People']
|
||||||
|
|
||||||
# Path to the folder containing test images
|
# Path to the folder containing test images
|
||||||
test_images_folder = 'Testing/
|
test_images_folder = 'Testing/'
|
||||||
|
|
||||||
# Iterate over the test images
|
# Iterate over the test images
|
||||||
for filename in os.listdir(test_images_folder):
|
i = 0
|
||||||
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
|
errorcount = 0
|
||||||
# Load and preprocess the test image
|
for folder_name in os.listdir(test_images_folder):
|
||||||
image_path = os.path.join(test_images_folder, filename)
|
folder_path = os.path.join(test_images_folder, folder_name)
|
||||||
test_image = image.load_img(image_path, target_size=(224, 224))
|
if os.path.isdir(folder_path):
|
||||||
test_image = image.img_to_array(test_image)
|
print('Testing images in folder:', folder_name)
|
||||||
test_image = np.expand_dims(test_image, axis=0)
|
|
||||||
test_image = test_image / 255.0 # Normalize the image
|
# True class based on folder name
|
||||||
|
if folder_name == 'Empty':
|
||||||
# Make predictions
|
true_class = 'Empty'
|
||||||
predictions = model.predict(test_image)
|
elif folder_name == 'Food':
|
||||||
predicted_class_index = np.argmax(predictions[0])
|
true_class = 'Food'
|
||||||
predicted_class = class_names[predicted_class_index]
|
elif folder_name == 'People':
|
||||||
|
true_class = 'People'
|
||||||
print('Image:', filename)
|
true_class = folder_name
|
||||||
print('Predicted class:', predicted_class)
|
|
||||||
print()
|
# Iterate over the files in the subfolder
|
||||||
|
for filename in os.listdir(folder_path):
|
||||||
|
if filename.endswith('.jpg') or filename.endswith('.jpeg'):
|
||||||
|
i+=1
|
||||||
|
# Load and preprocess the test image
|
||||||
|
image_path = os.path.join(folder_path, filename)
|
||||||
|
test_image = image.load_img(image_path, target_size=(100, 100))
|
||||||
|
test_image = image.img_to_array(test_image)
|
||||||
|
test_image = np.expand_dims(test_image, axis=0)
|
||||||
|
test_image = test_image / 255.0 # Normalize the image
|
||||||
|
|
||||||
|
# Reshape the image array to (1, height, width, channels)
|
||||||
|
test_image = np.reshape(test_image, (1,100, 100, 3))
|
||||||
|
|
||||||
|
# Make predictions
|
||||||
|
predictions = model.predict(test_image)
|
||||||
|
predicted_class_index = np.argmax(predictions[0])
|
||||||
|
predicted_class = class_names[predicted_class_index]
|
||||||
|
|
||||||
|
direct = 'Results/'
|
||||||
|
filename = str(i) + predicted_class + '.jpeg'
|
||||||
|
test_image = np.reshape(test_image, (100, 100, 3))
|
||||||
|
tf.keras.preprocessing.image.save_img(direct+filename, test_image)
|
||||||
|
if predicted_class != true_class:
|
||||||
|
errorcount += 1
|
||||||
|
print('Image:', filename)
|
||||||
|
print('True class:', true_class)
|
||||||
|
print('Predicted class:', predicted_class)
|
||||||
|
print()
|
||||||
|
print('Error count: ', errorcount)
|
@ -47,7 +47,7 @@ for i in range(60):
|
|||||||
|
|
||||||
direct = 'Results/'
|
direct = 'Results/'
|
||||||
filename = predicted_class + str(i) + '.jpeg'
|
filename = predicted_class + str(i) + '.jpeg'
|
||||||
tf.keras.preprocessing.image.save_img(direct+filename, val_images[i])
|
tf.keras.preprocessing.image.save_img(direct+filename, test_image)
|
||||||
if predicted_class != true_class:
|
if predicted_class != true_class:
|
||||||
errorcount += 1
|
errorcount += 1
|
||||||
print('Image', i+1)
|
print('Image', i+1)
|
||||||
|
Loading…
Reference in New Issue
Block a user