Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
data.append(features)
labels.append(texture)
# train the classifier
print "[INFO] training model..."
model = LinearSVC(C=10.0, random_state=42)
model.fit(data, labels)
print "[INFO] classifying..."
# loop over the test images
for imagePath in glob.glob(args["test"] + "/*.png"):
# load the image, convert it to grayscale, and extract Haralick
# texture from the test image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
features = mahotas.features.haralick(gray).mean(axis=0)
# classify the test image
pred = model.predict(features.reshape(1, -1))[0]
cv2.putText(image, pred, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0,
(0, 255, 0), 3)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
# initialize the data matrix and the list of labels
print "[INFO] extracting features..."
data = []
labels = []
# loop over the dataset of images
for imagePath in glob.glob(args["training"] + "/*.png"):
# load the image, convert it to grayscale, and extract the texture
# name from the filename
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
texture = imagePath[imagePath.rfind("/") + 1:].split("_")[0]
# extract Haralick texture features in 4 directions, then take the
# mean of each direction
features = mahotas.features.haralick(image).mean(axis=0)
# update the data and labels
data.append(features)
labels.append(texture)
# train the classifier
print "[INFO] training model..."
model = LinearSVC(C=10.0, random_state=42)
model.fit(data, labels)
print "[INFO] classifying..."
# loop over the test images
for imagePath in glob.glob(args["test"] + "/*.png"):
# load the image, convert it to grayscale, and extract Haralick
# texture from the test image
image = cv2.imread(imagePath)
def texture(im):
'''Compute features for an image
Parameters
----------
im : ndarray
Returns
-------
fs : ndarray
1-D array of features
'''
im = im.astype(np.uint8)
return mh.features.haralick(im).ravel()
def features_for(im):
from features import chist
im = mh.imread(im)
img = mh.colors.rgb2grey(im).astype(np.uint8)
return np.concatenate([mh.features.haralick(img).ravel(),
chist(im)])