AI_PROJEKT_2021/neural_network_vers2.py

32 lines
936 B
Python

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')
def predict():
path="./dane_testowe"
files=os.listdir(path)
d=random.choice(files)
im = Image.open("./dane_testowe/" + d)
im.show()
img = keras.preprocessing.image.load_img(
"./dane_testowe/" + d, target_size=(img_height, img_width)
)
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))
)