Search code examples
pythonsvmscikit-imagetrain-test-split

NameError: name 'skimage' is not defined


im trying to figure out how to use SVM for image classification using images from my own dataset, to which im using the notebook from his link: https://github.com/whimian/SVM-Image-Classification. The problem is that, for whatever other project i use skimage it works alright, but for this one i get the error described above in the title in the following line:

img = skimage.io.imread(file)

I already used the commands pip uninstall scikit-image and install and still didn't work.

Moreover, the following errors occurs in the down lines, im not sure if they are related to this problem:

image_dataset.data, image_dataset.target, test_size=0.3,random_state=109

NameError: name 'image_dataset' is not defined


clf.fit(X_train, y_train)

NameError: name 'X_train' is not defined

And for visualization, here's the code snipped to which the error belongs to:

image_dir = Path(container_path)
folders = [directory for directory in image_dir.iterdir() if directory.is_dir()]
categories = [fo.name for fo in folders]

descr = "A image classification dataset"
images = []
flat_data = []
target = []
for i, direc in enumerate(folders):
    for file in direc.iterdir():
        img = skimage.io.imread(file)
        img_resized = resize(img, dimension, anti_aliasing=True, mode='reflect')
        flat_data.append(img_resized.flatten()) 
        images.append(img_resized)
        target.append(i)
flat_data = np.array(flat_data)
target = np.array(target)
images = np.array(images)

return Bunch(data=flat_data,
             target=target,
             target_names=categories,
             images=images,
             DESCR=descr)

As for the imports:

from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
from sklearn import svm, metrics, datasets
from sklearn.utils import Bunch
from sklearn.model_selection import GridSearchCV, train_test_split

from skimage.io import imread
from skimage.transform import resize

Solution

  •  img = skimage.io.imread(file)
    

    change this line to

     img = imread(file)