OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

TypeError: Can't convert object to 'str' for 'filename'

  • Thread starter Thread starter melolilili
  • Start date Start date
M

melolilili

Guest
I want to create imgs variable to load all the images in "./input/hubmap-organ-segmentation/train_images/" folder. My code raised TypeError: Can't convert object to 'str' for 'filename' error.

Code:
import os
import glob
import pandas as pd
import cv2

BASE_PATH = "./input/hubmap-organ-segmentation/"
df = pd.read_csv(os.path.join(BASE_PATH, "train.csv"))
    
all_image_files = glob.glob(os.path.join(BASE_PATH, "train_images", "**", "*.tiff"), recursive=True)
train_img_map = {int(x[:-5].rsplit("/", 1)[-1]):x for x in all_image_files}
df.insert(3, "img_path", image_ids.map(train_img_map))

imgs = [cv2.imread(img_path)[..., ::-1] for img_path in df.img_path.values]

Traceback:

Code:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [132], in <cell line: 38>()
     37 ftu_crop_map = {}
     38 for _organ in ORGANS:
     39     #sub_df = df[df.organ==_organ].sample(N_EX)
---> 40     imgs = [cv2.imread(img_path)[..., ::-1] for img_path in df.img_path.values]

Input In [132], in <listcomp>(.0)
     37 ftu_crop_map = {}
     38 for _organ in ORGANS:
     39     #sub_df = df[df.organ==_organ].sample(N_EX)
---> 40     imgs = [cv2.imread(img_path)[..., ::-1] for img_path in df.img_path.values]

TypeError: Can't convert object to 'str' for 'filename'

Example of img_path:

Code:
df.img_path[0]
'./input/hubmap-organ-segmentation/train_images/10044.tiff'
<p>I want to create <code>imgs</code> variable to load all the images in "./input/hubmap-organ-segmentation/train_images/" folder. My code raised <code>TypeError: Can't convert object to 'str' for 'filename'</code> error.</p>
<pre class="lang-py prettyprint-override"><code>import os
import glob
import pandas as pd
import cv2

BASE_PATH = "./input/hubmap-organ-segmentation/"
df = pd.read_csv(os.path.join(BASE_PATH, "train.csv"))

all_image_files = glob.glob(os.path.join(BASE_PATH, "train_images", "**", "*.tiff"), recursive=True)
train_img_map = {int(x[:-5].rsplit("/", 1)[-1]):x for x in all_image_files}
df.insert(3, "img_path", image_ids.map(train_img_map))

imgs = [cv2.imread(img_path)[..., ::-1] for img_path in df.img_path.values]
</code></pre>
<p>Traceback:</p>
<pre class="lang-none prettyprint-override"><code>---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [132], in <cell line: 38>()
37 ftu_crop_map = {}
38 for _organ in ORGANS:
39 #sub_df = df[df.organ==_organ].sample(N_EX)
---> 40 imgs = [cv2.imread(img_path)[..., ::-1] for img_path in df.img_path.values]

Input In [132], in <listcomp>(.0)
37 ftu_crop_map = {}
38 for _organ in ORGANS:
39 #sub_df = df[df.organ==_organ].sample(N_EX)
---> 40 imgs = [cv2.imread(img_path)[..., ::-1] for img_path in df.img_path.values]

TypeError: Can't convert object to 'str' for 'filename'
</code></pre>
<p>Example of <code>img_path</code>:</p>
<pre class="lang-py prettyprint-override"><code>df.img_path[0]
'./input/hubmap-organ-segmentation/train_images/10044.tiff'
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top