44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
import os
|
|
import cv2
|
|
from sklearn.neural_network import MLPClassifier
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.metrics import classification_report
|
|
|
|
def preprocess(img):
|
|
scale_percent = 10
|
|
width = int(img.shape[1] * scale_percent / 100)
|
|
height = int(img.shape[0] * scale_percent / 100)
|
|
dim = (width, height)
|
|
resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
|
|
resized = resized.flatten()
|
|
return resized
|
|
|
|
def readData(data_links):
|
|
x, y = [], []
|
|
for link in data_links:
|
|
img = cv2.imread(link, cv2.IMREAD_COLOR)
|
|
img = preprocess(img)
|
|
label = link.split("/")[1].split('_')[1]
|
|
x.append(img)
|
|
y.append(label)
|
|
|
|
return x, y
|
|
|
|
data_links = os.listdir("data/")
|
|
data_links = ["data/" + x for x in data_links]
|
|
|
|
x, y = readData(data_links)
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)
|
|
|
|
clf = MLPClassifier(solver='adam', alpha=1e-5, hidden_layer_sizes=(1000, 700), random_state=1,
|
|
activation='relu', batch_size='auto', shuffle=True, verbose=True, learning_rate='adaptive', n_iter_no_change=10)
|
|
clf.fit(X_train, y_train)
|
|
|
|
print("Score:")
|
|
print(clf.score(X_test, y_test))
|
|
|
|
print("Summary:")
|
|
Y_pred = clf.predict(X_test)
|
|
print(classification_report(y_test, Y_pred))
|