Q
Q
q21199992021-06-16 21:14:00
Python
q2119999, 2021-06-16 21:14:00

An error occurs when outputting an image via CSV. What is wrong in my code?

I am learning how to create an NS, I am trying to create a SNS according to the lecture. But I have a problem, when I try to display an image from a CSV table through opencv, an error appears. I'm a newbie, so don't be too harsh. Can anyone explain what is the error? If necessary, I can throw off the laptop with the full code

Below is part of the code

img mask
0 data\first\33-1.jpg data\mas\33-2.jpg
1 data\first\33-3.jpg data\mas\33-4. jpg
2 data\first\33-5.jpg data\mas\33-6.jpg
3 data\first\33-7.jpg data\mas\33-8.jpg
4 data\first\33-9.jpg data \mas\33-10.jpg
5 data\first\33-11.jpg data\mas\33-12.jpg
6 data\first\33-13.jpg data\mas\33-14.jpg
img, mask = train_df.iloc[6
]
img = cv2.imread('data/first/{}'.format(img_name))
mask = cv2.imread('data/mas/{}'.format(img_name)) ​​fig
, axes = plt.subplots (nrows=1, ncols=2, figsize=(25, 25)) cv2.imshow(mask[..., 0]) cv2.imshow(img) ​plt.show () fig, axes = plt.subplots( nrows=1, ncols=2, figsize=(25, 25)) axes[0].imshow(img) axes[1].imshow(mask) plt.show() ----------- -------------------------------------------------- -------------- TypeError Traceback (most recent call last) in 1 fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(25, 25)) 2 axes [0].imshow(img)
----> 3 axes[1].imshow(mask)
4 plt.show()

~\anaconda3\envs\deeplearning\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1445 def inner(ax, *args, data=None, **kwargs):
1446 if data is None:
-> 1447 return func(ax, *map(sanitize_sequence, args), **kwargs)
1448
1449 bound = new_sig.bind(ax, *args, **kwargs)

~\anaconda3\envs\deeplearning\lib\site-packages\matplotlib\axes\_axes.py in imshow(self, X, cmap, norm, aspect, interpolation , alpha, vmin, vmax, origin, extent, filternorm, filterrad, resample, url, **kwargs)
5521 resample=resample, **kwargs)
5522
-> 5523 im.set_data(X)
5524 im.set_alpha(alpha)
5525 if im.get_clip_path() is None:

~\anaconda3\envs\deeplearning\lib\site-packages\matplotlib\image.py in set_data(self, A)
700 if (self._A. dtype != np.uint8 and
701 not np.can_cast(self._A.dtype, float, "same_kind")):
--> 702 raise TypeError("Image data of dtype {} cannot be converted to "
703 "float" .format(self._A.dtype))
704

TypeError: Image data of dtype object cannot be converted to float

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-16
@Vindicar

img = cv2.imread('data/first/{}'.format(img_name))
mask = cv2.imread('data/mas/{}'.format(img_name))
First, you should specify cv2 as the second parameter. IMREAD_COLOR.
Secondly, check what is in the end in img and mask.
On error, imread() silently returns None rather than throwing an exception.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question