Advertisement
Krakkus

Prediction

Feb 20th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | Source Code | 0 0
  1. import os
  2. import cv2
  3. import random
  4. import tensorflow as tf
  5. import numpy as np
  6.  
  7. images = []
  8. for path, subdirs, files in os.walk("C:\\Users\\Krakkus\\Desktop\\flower_images"):
  9.     for name in files:
  10.         fn = os.path.join(path, name)
  11.         images.append(fn)
  12.  
  13. fn = random.choice(images)
  14. print(fn)
  15.  
  16. org_img = cv2.imread(fn)
  17. tf_image = cv2.resize(org_img, (224, 224))
  18. tf_image = cv2.cvtColor(tf_image, cv2.COLOR_BGR2RGB)
  19. tf_image = np.expand_dims(tf_image, axis=0)
  20.  
  21. model = tf.keras.models.load_model('my_model_224.keras')
  22. prediction = model.predict([tf_image])
  23. print(prediction[0])
  24. predicted_class = np.argmax(prediction[0])
  25. print(predicted_class)
  26.  
  27. cv2.imshow('Image classification', org_img)
  28. cv2.waitKey(0)
  29.  
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement