Nhyi
Nhyi

Reputation: 383

CV2 Returning NoneType for Image?

I am currently trying to compute the arrays of different images. I have the code below which uses cv2 to read and then hog.compute to calculate it. However the issue that I am getting is that I am getting a NoneType being outputted. I know that the absolute file path which is why I used os.path.abspath(file). However, I know that the file is being read as I have printed the file name and it is the file that is in the directory?

The files are located within a folder called image_dataset and this has 3 subfolders called bikes, cars and people. I'm pretty sure the first file is being read as well but have no clue why I am getting a NoneType returned when I try hog.compute(im)? Any clue as to why?

import os
import numpy as np
import cv2
import glob
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

    
def obtain_dataset(folder_name):
    # assuming 128x128 size images and HoGDescriptor length of 34020
    hog_feature_len=34020
    hog = cv2.HOGDescriptor()
    
    image_dict = {'bikes':1, 'cars':2, 'people':3}
    
    y = []
    X = []
        
    #code for obtaining hog feature for one image file name

    for subdir, dirs, files in os.walk(folder_name):
        
        for file in files:
            
            if file.lower().endswith(('.png')):
            
                location = (os.path.abspath(file))
                im = cv2.imread(location)
                h = hog.compute(im)

    # use this to read all images in the three directories and obtain the set of features X and train labels Y
    # you can assume there are three different classes in the image dataset
    return (X,y) 

train_folder_name='image_dataset'
(X_train, Y_train) = obtain_dataset(train_folder_name)

Upvotes: 1

Views: 410

Answers (1)

Red
Red

Reputation: 27567

In your case, as you have a subdirectory, the os.path.abspath() method does not return the complete path of the file. Instead, use os.path.join() to join the file names with the path of the directory of the files:

location = os.path.join(subdir, file)

Upvotes: 1

Related Questions