AL-2020/coder/image.py

82 lines
1.9 KiB
Python
Raw Normal View History

2020-05-20 07:32:13 +02:00
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.neural_network import MLPClassifier
2020-05-25 00:24:34 +02:00
import pandas as pd
2020-05-20 07:32:13 +02:00
import cv2
2020-05-25 00:24:34 +02:00
#28x28
train_data = np.genfromtxt('dataset/train.csv', delimiter=',', skip_header=1 ,max_rows=20000, encoding='utf-8')
test_data = np.genfromtxt('dataset/test.csv', delimiter=',' , skip_header=1, max_rows=20000, encoding='utf-8')
2020-05-20 07:32:13 +02:00
# training
# recznie napisane cyfry
2020-05-20 07:32:13 +02:00
2020-05-25 00:24:34 +02:00
digits = datasets.load_digits()
y = digits.target
x = digits.images.reshape((len(digits.images), -1))
2020-05-25 00:24:34 +02:00
2020-05-20 12:57:31 +02:00
#ogarnac zbior, zwiekszyc warstwy
2020-05-20 07:32:13 +02:00
2020-05-25 00:24:34 +02:00
x_train = train_data[0:20000, 1:]
y_train = train_data[0:20000, 0]
x_test = test_data[0:20000]
y_test = test_data[0:20000, 0]
2020-05-20 07:32:13 +02:00
2020-05-25 00:24:34 +02:00
# x_train = x[:900]
# y_train = y[:900]
# x_test = x[900:]
# y_test = y[900:]
2020-05-20 07:32:13 +02:00
2020-05-25 00:24:34 +02:00
print(x_test[0].shape, y_test[9].shape)
mlp = MLPClassifier(hidden_layer_sizes=(100, 100, 100, 100), activation='logistic', alpha=1e-4,
solver='sgd', tol=0.000000000001, random_state=1,
learning_rate_init=.1, verbose=True, max_iter=1000)
2020-05-25 00:24:34 +02:00
mlp.fit(x_train, y_train)
print(123456789)
predictions = mlp.predict(x_test)
2020-05-25 00:24:34 +02:00
print(123456789)
print("Accuracy: ", accuracy_score(y_test, predictions))
# image
2020-05-25 00:24:34 +02:00
img = cv2.cvtColor(cv2.imread('test5.jpg'), cv2.COLOR_BGR2GRAY)
2020-05-20 11:45:55 +02:00
img = cv2.blur(img, (9, 9)) # poprawia jakosc
2020-05-25 00:24:34 +02:00
img = cv2.resize(img, (28, 28), interpolation=cv2.INTER_AREA)
img = img.reshape((len(img), -1))
print(type(img))
print(img.shape)
print(img)
plt.imshow(img ,cmap='binary')
plt.show()
data = []
rows, cols = img.shape
for i in range(rows):
for j in range(cols):
k = img[i, j]
2020-05-20 11:45:55 +02:00
if k > 225:
k = 0 # brak czarnego
else:
k = 1
data.append(k)
data = np.asarray(data, dtype=np.float32)
print(data)
predictions = mlp.predict([data])
print("Liczba to:", predictions[0])