簡體   English   中英

在Python中使用astype('float32')時出錯

[英]Error when using astype('float32') in Python

我是python的新手,正在使用以下代碼:

for index, person in enumerate(people):
  print(index)
  dir_path = 'train/' + person
for img_path in os.listdir(dir_path):
  name, ext = os.path.splitext(img_path)
  if ext.lower() not in valid_images:
    continue

img_data = cv2.imread(dir_path + '/' + img_path)
# convert image to gray
img_data=cv2.cvtColor(img_data, cv2.COLOR_BGR2GRAY)
img_data_list.append(img_data)
labels.append(index)

img_data = np.array(img_data_list)
img_data = img_data.astype('float32')

但是在運行時出現錯誤:

img_data = img_data.astype('float32') ValueError: setting an array element with a sequence.

誰能幫我解決這個問題?

遍歷您的列表:

在代碼末尾,您似乎正在嘗試更改存儲在列表中的圖像的dtype 用OpenCV讀取的圖像自然是numpy數組。

以下示例應有幫助:

# create a test image list
img = np.ones((60,60), dtype=np.uint8)
img_list = [img] * 4

# use a list comp to run through the images and change dtype
changed_img_list = [img.astype(np.float32) for i in img_list]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM