# loop over the images in each sub-folder
for x in range(1,images_per_class+1):
# get the image file name
file = dir + "/" + str(x) + ".jpg"
# read the image and resize it to a fixed-size
image = cv2.imread(file)
image = cv2.resize(output, tuple(point1), tuple(point2),(image,20,30)
Error Message:
File “”, line 8
image = cv2.resize(output, tuple(point1), tuple(point2),(image,20,30)
^
SyntaxError: unexpected EOF while parsing
You do not have anything called “output”. You are supposed to pass in the image you have just read. You decided to call this “image”. So, you need the first argument of cv2.resize to be the file/image you want resized.
#-----------------------------------
# GLOBAL FEATURE EXTRACTION
#-----------------------------------
# organize imports
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import MinMaxScaler
import numpy as np
import mahotas
import cv2
import os
import h5py
# fixed-sizes for image
fixed_size = tuple((256, 256))
# path to training data
train_path = "dataset/train/"
# no.of.trees for Random Forests
num_trees = 100
# bins for histogram
bins = 8
# train_test_split size
test_size = 0.10
# seed for reproducing same results
seed = 9
# feature-descriptor-1: Hu Moments
def fd_hu_moments(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
feature = cv2.HuMoments(cv2.moments(image)).flatten()
return feature
# feature-descriptor-2: Haralick Texture
def fd_haralick(image):
# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# compute the haralick texture feature vector
haralick = mahotas.features.haralick(gray).mean(axis=0)
# return the result
return haralick
# feature-descriptor-3: Color Histogram
def fd_histogram(image, mask=None):
# convert the image to HSV color-space
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# compute the color histogram
hist = cv2.calcHist([image], [0, 1, 2], None, [bins, bins, bins], [0, 256, 0, 256, 0, 256])
# normalize the histogram
cv2.normalize(hist, hist)
# return the histogram
return hist.flatten()
# get the training labels
train_labels = os.listdir(train_path)
# sort the training labels
train_labels.sort()
print(train_labels)
# empty lists to hold feature vectors and labels
global_features = []
labels = []
i, j = 0, 0
k = 0
# num of images per class
images_per_class = 80
# loop over the training data sub-folders
for training_name in train_labels:
# join the training data path and each species training folder
dir = os.path.join(train_path, training_name)
# get the current training label
current_label = training_name
k = 1
# loop over the images in each sub-folder
name='image_'
for x in range(1,images_per_class+1):
# get the image file name
# file = dir + "/" + str(x) + ".jpg"
file = dir + "/" + name + str(x) + ".jpg"
print(file)
# read the image and resize it to a fixed-size
image = cv2.imread(file)
image = cv2.resize(image, fixed_size)
####################################
# Global Feature extraction
####################################
fv_hu_moments = fd_hu_moments(image)
fv_haralick = fd_haralick(image)
fv_histogram = fd_histogram(image)
###################################
# Concatenate global features
###################################
global_feature = np.hstack([fv_histogram, fv_haralick, fv_hu_moments])
# update the list of labels and feature vectors
labels.append(current_label)
global_features.append(global_feature)
i += 1
k += 1
print("[STATUS] processed folder: {}".format(current_label))
j += 1
print("[STATUS] completed Global Feature Extraction...")
# get the overall feature vector size
print("[STATUS] feature vector size {}".format(np.array(global_features).shape))
# get the overall training label size
print("[STATUS] training Labels {}".format(np.array(labels).shape))
# encode the target labels
targetNames = np.unique(labels)
le = LabelEncoder()
target = le.fit_transform(labels)
print("[STATUS] training labels encoded...")
# normalize the feature vector in the range (0-1)
scaler = MinMaxScaler(feature_range=(0, 1))
rescaled_features = scaler.fit_transform(global_features)
print("[STATUS] feature vector normalized...")
print("[STATUS] target labels: {}".format(target))
print("[STATUS] target labels shape: {}".format(target.shape))
# save the feature vector using HDF5
h5f_data = h5py.File('output/data.h5', 'w')
h5f_data.create_dataset('dataset_1', data=np.array(rescaled_features))
h5f_label = h5py.File('output/labels.h5', 'w')
h5f_label.create_dataset('dataset_1', data=np.array(target))
h5f_data.close()
h5f_label.close()
print("[STATUS] end of training..")
error Traceback (most recent call last)
in
1 # read the image and resize it to a fixed-size
2 image = cv2.imread(file)
----> 3 image = cv2.resize(image, fixed_size)
error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src\resize.cpp:3720: error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’
This is the modification we were working on:::
NameError Traceback (most recent call last)
in
1 # loop over the images in each sub-folder
----> 2 for x in range(1,images_per_class+1):
3 # get the image file name
4 file = dir + “/” + str(x) + “.jpg”
5
The first error is suggesting your image is not read. Either, this is a broken file, and will never work, or you have linked the wrong file type to be read. To fix this, you could add a try...except block so that images that cannot be read will not break the scripts execution.
For the second error: I see no reason why this would happen, unless you are running your code in sections, and not as one Python script.
fngwira, you are posting many errors for different code.
In any programming language, you need to define a variable in order to pass it as an argument into a function, or use it in any way. Therefore, if you have not explicitly said point1 = 1, you cannot use point1 as an argument in a function (that is just an example)
You are getting simple errors, and of different code/scripts. I cannot help, if you are working on different versions of the same file.
I would suggest taking a quick crash-course online for coding in Python. Yes, you can just copy and paste code, but then your file structure has to be exactly the same as the person who wrote it.
Spend some time researching it, and it will benefit you greatly in the long-run.
@Sky020, as you can see the error i just posted is coming after making the recommendation you gave me, i will research further it is not a matter of copy + paste. Thanks for your advise.