Harsh994's picture
Upload test.py
221338a verified
raw
history blame
No virus
1.56 kB
import sys
import numpy as np
from PIL import Image
from numpy import asarray
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image_dataset_from_directory
input = sys.argv[1]
path = r"D:\MinorP\venv\Validation"
train = image_dataset_from_directory(path, batch_size=32,
image_size=(256,256),shuffle=True)
#pathx=r"C:\Projects\Junk\model.keras"
class_labels = train.class_names
pth=r"D:\MinorP\venv\model.h5"
#model = keras.models.load_model("model.h5")
model = keras.models.load_model(pth, custom_objects=None, compile=True, safe_mode=True)
#print(model.summary())
#imgs = Image.open('basil.jpg')
def calling(img_path):
imgs = Image.open(img_path)
predicted_class, confidence = Prediction(model, asarray(imgs))
return predicted_class
#print('hello')
def Prediction(model, img):
img_array = tf.keras.preprocessing.image.img_to_array((img))
img_array = tf.expand_dims(img_array, 0) # create a batch
predictions = model.predict(img_array)
predicted_class = class_labels[np.argmax(predictions[0])]
confidence = round(100 * (np.max(predictions[0])), 2)
return predicted_class, confidence
#return predicted_class
#return predictions
ph=r"D:\MinorP\venv\ashwagandha.jpg"
op = calling(ph)
print(op)
sys.stdout.flush()
#predicted_class , confidence = Prediction(model,asarray(imgs))
#pred = Prediction(model,asarray(imgs))
#print(predicted_class)
#print('hello2')