【问题标题】:Google Colab [Errno 21] Is a directory: '/content/positive_images/.ipynb_checkpoints'Google Colab [Errno 21] 是一个目录:'/content/positive_images/.ipynb_checkpoints'
【发布时间】:2021-11-01 20:05:06
【问题描述】:

请帮我解决这个错误。我需要将图像一张一张地放入一个循环中以转换为灰色并计算 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)```

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python machine-learning google-colaboratory svm histogram-of-oriented-gradients


【解决方案1】:

.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)

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

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2018-06-20
    • 2022-12-07
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2019-07-08
    相关资源
    最近更新 更多