Advertisement
Guest User

Untitled

a guest
Sep 19th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import requests
  2. from io import BytesIO
  3. from matplotlib import pyplot as plt
  4.  
  5. image_url = "https://datasets-server.huggingface.co/assets/mnist/--/mnist/train/12/image/image.jpg"
  6.  
  7. # Download the image using requests
  8. response = requests.get(image_url)
  9.  
  10. if response.status_code == 200:
  11.      # Read the image from the response content
  12.     image_bytes = BytesIO(response.content)
  13.     input_image = plt.imread(image_bytes, format='jpg')
  14.  
  15.     # Display the downloaded image
  16.     plt.imshow(input_image, cmap='gray')
  17.     plt.show()
  18.  
  19.     # Preprocess the image (assuming the model expects the same preprocessing as the MNIST dataset)
  20.     input_image = np.array(input_image, dtype='float32')
  21.     input_image /= 255.0  # Normalize pixel values to the range [0, 1]
  22.  
  23.     # Reshape the image to match the model's input shape
  24.     input_image = np.reshape(input_image, (1, 28, 28))
  25.  
  26.     # Get the prediction using the probability_model
  27.     predictions = probability_model.predict(input_image)
  28.  
  29.     # The predictions will be a probability distribution over classes.
  30.     # You can extract the class with the highest probability using argmax.
  31.     predicted_class = np.argmax(predictions[0])
  32.  
  33.     print("Predicted Class:", predicted_class)
  34. else:
  35.     print("Failed to download the image. Status code:", response.status_code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement