fork download
  1. import tensorflow as tf
  2. from tensorflow.keras.applications import ResNet50
  3. from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
  4. from tensorflow.keras.preprocessing import image
  5. import numpy as np
  6.  
  7. def process_image(image_path):
  8. # Load the pre-trained model
  9. model = ResNet50(weights='imagenet')
  10.  
  11. # Load and preprocess the image
  12. img = image.load_img(image_path, target_size=(224, 224))
  13. img_array = image.img_to_array(img)
  14. img_array = np.expand_dims(img_array, axis=0)
  15. img_array = preprocess_input(img_array)
  16.  
  17. # Predict
  18. predictions = model.predict(img_array)
  19. description = decode_predictions(predictions, top=1)[0][0][1]
  20.  
  21. return description
  22.  
Success #stdin #stdout 0.92s 192208KB
stdin
Standard input is empty
stdout
Standard output is empty