以下為出錯code:
import numpy as np
import cv2
import os
import pandas as pd
from six.moves import cPickle
# training data direction
train_dir = 'C:/Program Files/BACNN/trainingdataset'
X_train = []
y_age = []
y_gender = []
df = pd.read_csv('C:/Program Files/BACNN/trainingdataset/boneagetrainingdataset.csv')
a = df.values
m = a.shape[0]
path = train_dir
k = 0
print('Loading data set...')
for i in os.listdir(path):
y_age.append(df.boneage[df.id == int(i[:-4])].tolist()[0])
a = df.male[df.id == int(i[:-4])].tolist()[0]
if a:
y_gender.append(1)
else:
y_gender.append(0)
img_path = path + i
img = cv2.imread(path)
print(img_path)
img_G = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_RE = cv2.resize(img_G, (300, 300))
x = np.asarray(img_RE, dtype=np.uint8)
X_train.append(x)
print('100% completed loading data')
# Save data
train_pkl = open('data.pkl', 'wb')
cPickle.dump(X_train, train_pkl, protocol=cPickle.HIGHEST_PROTOCOL)
train_pkl.close()
train_age_pkl = open('data_age.pkl', 'wb')
cPickle.dump(y_age, train_age_pkl, protocol=cPickle.HIGHEST_PROTOCOL)
train_age_pkl.close()
train_gender_pkl = open('data_gender.pkl', 'wb')
cPickle.dump(y_gender, train_gender_pkl, protocol=cPickle.HIGHEST_PROTOCOL)
train_gender_pkl.close()
不太了解為什麼會抓不到檔案位置,想尋求大家有什麼想法。
報錯內容:
[ WARN:0@2.439] global loadsave.cpp:248 cv::findDecoder imread_('C:/Program Files/BACNN/trainingdataset'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "C:\Users\ChaoHuaLee\PycharmProjects\pythonProject\BAA_data__utlis.py", line 31, in
img_G = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.8.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
可以確認一下
img_path 印出來的路徑有符合嗎?
不過你的 imread 又是 path,是打錯變數嗎?
img_path = path + i
img = cv2.imread(path)
print(img_path)
是打錯以下才對,image_path我確認一下
img_path = path + i
img = cv2.imread(img_path)
print(img_path)
整體輸入路徑有調整一下(調整內容如下),調整完後image_path print出來之後是可以讀到裡面的圖片,但總體跑起來還是同樣錯誤。
train_dir = 'C:/Program Files/BACNN/training'
X_train = []
y_age = []
y_gender = []
df = pd.read_csv('C:/Program Files/BACNN/boneagetrainingdataset.csv')
a = df.values
m = a.shape[0]
基本上你那個錯誤是路徑問題
路徑上有中文、空白都有可能造成問題