[英]Problem when gathering image data with cv2
I'm trying to get the image data from some cat and dog images with cv2 for a machine learning project in python and append them all to a training_data
list.我正在尝试使用 cv2 从一些猫和狗图像中获取图像数据,用于 python 和 append 中的机器学习项目,将它们全部添加到training_data
列表中。 But it is just printing None
when printing the list at the end.但它只是在最后打印列表时打印None
。
At first I thought that maybe I needed to convert the data to a numpy array but nothing seems to work and I'm really not understanding what's wrong.起初我想也许我需要将数据转换为 numpy 数组,但似乎没有任何效果,我真的不明白出了什么问题。
CATEGORIES = ["Dog", "Cat"] # 0=dog, 1=cat
IMG_SIZE = 50
training_data = []
def create_training_data():
i = 0
for category in CATEGORIES:
path = os.path.join(DATADIR, category) # path to cats or dogs dir
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
i += 1
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])
except Exception as e:
print("general exception", e, os.path.join(path, img))
if i % 1000 == 0:
print(i, '...')
print('\n\nfinished...\n\n')
create_training_data()
training_data = random.shuffle(training_data)
print(training_data)
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
I think it has to do with the resizing because it is throwing a lot of these exceptions:我认为这与调整大小有关,因为它引发了很多这样的异常:
general exception OpenCV(4.1.1) C:...\opencv\modules\imgproc\src\resize.cpp:3720: error: (-215:Assertion failed).ssize:empty() in function 'cv::resize' D..../cats_and_dogs/PetImages\Cat\9565.jpg一般异常 OpenCV(4.1.1) C:...\opencv\modules\imgproc\src\resize.cpp:3720: error: (-215:Assertion failed).ssize:empty() in function:resizecv:resize ' D..../cats_and_dogs/PetImages\Cat\9565.jpg
It also prints this error:它还打印此错误:
Traceback (most recent call last):回溯(最近一次通话最后):
File "D:/Python/tensorflow/tutorial/cats_and_dogs.py", line 44, in for features, label in training_data: TypeError: 'NoneType' object is not iterable文件“D:/Python/tensorflow/tutorial/cats_and_dogs.py”,第 44 行,在功能中,label 在 training_data:TypeError:'NoneType' object 不可迭代
Corrupt JPEG data: 399 extraneous bytes before marker 0xd9损坏的 JPEG 数据:标记 0xd9 之前的 399 个无关字节
Corrupt JPEG data: 226 extraneous bytes before marker 0xd9损坏的 JPEG 数据:标记 0xd9 之前的 226 个无关字节
Corrupt JPEG data: 162 extraneous bytes before marker 0xd9损坏的 JPEG 数据:标记 0xd9 之前的 162 个无关字节
Warning: unknown JFIF revision number 0.00警告:未知的 JFIF 修订号 0.00
... ...
After researching your errors I found this thread that may be relevant to the problem you are having.在研究了您的错误后,我发现这个线程可能与您遇到的问题有关。
In short, the thread notes a problem with the Oxford-IIIT Pet Dataset.简而言之,该线程指出了 Oxford-IIIT 宠物数据集的问题。 In that dataset some of the images end in a '.jpg' extension but are actually '.png's.在该数据集中,一些图像以“.jpg”扩展名结尾,但实际上是“.png”。 Verify where you got your images from.验证您从哪里获得图像。
It appears you are having a similar/the same problem.看来您遇到了类似/相同的问题。 The mismatch between the file extension and the actual image data may lead to openCV not being able to read in the data correctly.文件扩展名与实际图像数据不匹配可能导致 openCV 无法正确读取数据。
Edit: 2019 Oct. 14编辑:2019 年 10 月 14 日
Additionally, random.shuffle()
operates in place and therefore returns None
.此外, random.shuffle()
就地运行,因此返回None
。 Change training_data = random.shuffle(training_data)
to random.shuffle(training_data)
将training_data = random.shuffle(training_data)
更改为random.shuffle(training_data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.