Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

python - Why images from dataset can't be resized

guys! I have "cats and dogs" dataset and I want to resize all the images. Then I found that some images can be resized using cv2.resize() but some can't be resized.

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2 as cv

DATADIR = "D:/kagglecatsanddogs/PetImages"
CATEGORIES = ["DOG", "CAT"]

def create_training_data():
    for category in CATEGORIES:
        path = os.path.join(DATADIR, category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            img_array = cv.imread(os.path.join(path, img), cv.IMREAD_GRAYSCALE)
            new_array = cv.resize(img_array, (IMG_SIZE, IMG_SIZE))
            training_data.append([new_array, class_num])


create_training_data()

But I got a error:

Traceback (most recent call last):
  File "E:/cv_arm/handwriting/test.py", line 44, in <module>
    create_training_data()
  File "E:/cv_arm/handwriting/test.py", line 39, in create_training_data
    new_array = cv.resize(img_array, (IMG_SIZE, IMG_SIZE))
cv2.error: OpenCV(4.5.1) C:UsersappveyorAppDataLocalTemp1pip-req-build-oduouqigopencvmodulesimgprocsrc
esize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize

Then I add some thing in my code:

def create_training_data():
    for category in CATEGORIES:
        path = os.path.join(DATADIR, category)
        class_num = CATEGORIES.index(category)
        count = 1
        for img in os.listdir(path):
            img_array = cv.imread(os.path.join(path, img), cv.IMREAD_GRAYSCALE)
            try:
                new_array = cv.resize(img_array, (IMG_SIZE, IMG_SIZE))
            except:
                print(count)
            training_data.append([new_array, class_num])
            count += 1

(count is aimed to tell me how many times I have done in the for loop)

I have tried to print the count when errors exist and I got this :(just a part of them)

450
833
888
1571
1864
1895
2057
2062
2845
3464
4040
4377
4587
4876
5044
5377

So, Why some images can't be resized ? Please help me ^-^

question from:https://stackoverflow.com/questions/65887550/why-images-from-dataset-cant-be-resized

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...