iT邦幫忙

0

OpenCV報錯 global loadsave.cpp:248

  • 分享至 

  • xImage

以下為出錯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'

froce iT邦大師 1 級 ‧ 2023-10-11 12:47:21 檢舉
train_dir = 'C:/Program Files/BACNN/trainingdataset'

改成 train_dir = r'C:\Program Files\BACNN\trainingdataset'
試試看。
更換過後還是同結果QQ
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
hokou
iT邦好手 1 級 ‧ 2023-10-11 12:37:47
最佳解答

可以確認一下
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]
hokou iT邦好手 1 級 ‧ 2023-10-11 13:13:27 檢舉

基本上你那個錯誤是路徑問題
路徑上有中文、空白都有可能造成問題

https://blog.csdn.net/qq_51985653/article/details/113486737

了解,那我再一個一個除錯看看
謝謝~

結果問題是在資料夾屬性,trainging資料夾唯讀修改後便可除錯~

hokou iT邦好手 1 級 ‧ 2023-10-12 14:53:22 檢舉

竟然,我猜是放在這個'C:/Program Files/資料夾的緣故

我要發表回答

立即登入回答