Reputation: 25
I trained my model in three classes and now I want to input one image at a time to see whether it belongs to classes 1,2, or 3.
data = []
img_size = 224
for i in categories:
path = os.path.join(TRAIN_DIR1, i)
class_num = categories.index(i)
for file in os.listdir(path):
filepath = os.path.join(path, file)
img = cv2.imread(filepath, 0)
img = cv2.resize(img, (img_size, img_size))
data.append([img, class_num])
random.shuffle(data)
X, y = [], []
for feature, label in data:
X.append(feature)
y.append(label)
X = np.array(X).reshape(-1, img_size, img_size, 1)
X = X / 255.0
y = np.array(y)
X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=20, stratify=y)
X_train = X_train.reshape(X_train.shape[0], img_size*img_size*1)
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
I need help writing my prediction code to input one testing image at a time, please.
Upvotes: 0
Views: 284
Reputation: 424
import cv2
img_directory = input(str("Input directory: ")) # 'C:/dataset/img.png'
img= cv2.imread(img_directory)
img=cv2.resize(img, (180,180))
img = tf.expand_dims(img, 0)
prediction = model.predict(img)
score = tf.nn.softmax(prediction[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
You can use a while loop to continuously enter the img directory with break
when you input quit
.
Upvotes: 2