AI_PROJEKT_2021/neural_network_vers2.py

32 lines
936 B
Python
Raw Permalink Normal View History

2021-06-22 19:02:44 +02:00
import tensorflow as tf
import numpy as np
from tensorflow import keras
import os
import random
from PIL import Image
img_height = 180
img_width = 180
class_names=['glass','metal','paper','plastic']
model = tf.keras.models.load_model('./saved_model_vers2')
2021-06-22 19:02:44 +02:00
def predict():
path="./dane_testowe"
2021-06-22 19:02:44 +02:00
files=os.listdir(path)
d=random.choice(files)
im = Image.open("./dane_testowe/" + d)
2021-06-22 19:02:44 +02:00
im.show()
img = keras.preprocessing.image.load_img(
"./dane_testowe/" + d, target_size=(img_height, img_width)
2021-06-22 19:02:44 +02:00
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)