1

请帮我解决这个错误。我需要将图像一张一张地放入一个循环中以转换为灰色并计算 HOG 特征。但是之前我需要一张一张地拍摄图像。

# define path to images:

pos_im_path = r"/content/positive_images" # This is the path of our positive input dataset
# define the same for negatives
neg_im_path= r"negative_images"

# read the image files:
pos_im_listing = os.listdir(pos_im_path) # it will read all the files in the positive image path (so all the required images)
neg_im_listing = os.listdir(neg_im_path)
num_pos_samples = size(pos_im_listing) # simply states the total no. of images
num_neg_samples = size(neg_im_listing)
print(num_pos_samples) # prints the number value of the no.of samples in positive dataset
print(num_neg_samples)
data= []
labels = []

# compute HOG features and label them:
#/content/positive_images/22.png
for file in pos_im_listing: #this loop enables reading the files in the pos_im_listing variable one by one
    img = Image.open(pos_im_path + '\\' + file) # open the file
    #img = img.resize((64,128))
    gray = img.convert('L') # convert the image into single channel i.e. RGB to grayscale
    # calculate HOG for positive features
    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm='L2', feature_vector=True)# fd= feature descriptor
    data.append(fd)
    labels.append(1)
    
# Same for the negative images
for file in neg_im_listing:
    img= Image.open(neg_im_path + '/' + file)
    #img = img.resize((64,128))
    gray= img.convert('L')
    # Now we calculate the HOG for negative features
    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, block_norm='L2', feature_vector=True) 
    data.append(fd)
    labels.append(0)
# encode the labels, converting them from strings to integers
le = LabelEncoder()
labels = le.fit_transform(labels)```
4

1 回答 1

0

.ipynb_checkpoints 由 jupyter/colab notebook 在您的目录中创建。错误可能发生在代码中从目录中提取图像的以下两个位置。

for file in pos_im_listing: #this loop enables reading the files in the pos_im_listing variable one by one
    img = Image.open(pos_im_path + '\\' + file) # open the file

for file in neg_im_listing:
    img= Image.open(neg_im_path + '/' + file)

添加这一行“如果'.ipynb'不在文件中:”如下所示:

for file in pos_im_listing:
    if '.ipynb' not in file:
        img = Image.open(pos_im_path + '\\' + file) # open the file
    
for file in neg_im_listing:
    if '.ipynb' not in file:
        img= Image.open(neg_im_path + '/' + file)

这应该安全地忽略笔记本检查点并导入图像。或者,如果您的所有图像都有特定的扩展名,您可以像“如果文件中的'.jpeg'”这样指定,如下所示:

    for file in pos_im_listing:
        if '.jpeg' in file:
            img = Image.open(pos_im_path + '\\' + file) 
        
    for file in neg_im_listing:
        if '.jpeg' in file:
            img= Image.open(neg_im_path + '/' + file)

您还可以专门为图像创建一个单独的目录。

于 2021-09-03T13:34:36.450 回答