Ammar Shabir
Ammar Shabir

Reputation: 75

getting image from url and want to predict image from own classification model

i trained a model that will classfied image everything is working fine when i put send image directly from frontend but now i want to get image from url and predict that. consider i have a url like : www.example.com/image.jpg my code is:

 url = 'https://icon2.cleanpng.com/20171220/gze/twitter-logo-png-5a3a1851372e76.0876249315137567532269680.jpg'
 response = requests.get(url)
 img = Image.open(BytesIO(response.content))
 imgplot = plt.imshow(img)
 img_test = tf.expand_dims(img, axis=0)
 classes = np.argmax(parrotModel.predict(img_test), axis=-1)
 print(str(classes[0]))

error :

ValueError: Attempt to convert a value (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=626x417 at 0x7FD401D29898>) with an unsupported type (<class 'PIL.JpegImagePlugin.JpegImageFile'>) to a Tensor.```

Upvotes: 0

Views: 718

Answers (1)

Timbus Calin
Timbus Calin

Reputation: 15033

The problem here is that you need to convert the image to a numpy array.

img_test = tf.expand_dims(np.array(img), axis=0)

Also, have a look here: ValueError: Attempt to convert a value (<PIL.PngImagePlugin.PngImageFile image mode=RGB size=519x600 at 0x7F95AD916518>) with an unsupported type , a similar problem was discussed.

Upvotes: 1

Related Questions