AI_PROJEKT_2021/neural_network_vers2.py
2021-06-22 19:02:44 +02:00

32 lines
1.0 KiB
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('C:/Users/Natalia/Desktop/lsm04/smieciara/saved_model_vers2')
def predict():
path="C:/Users/Natalia/Documents/dane_testowe"
files=os.listdir(path)
d=random.choice(files)
im = Image.open("C:/Users/Natalia/Documents/dane_testowe/" + d)
im.show()
img = keras.preprocessing.image.load_img(
"C:/Users/Natalia/Documents/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))
)