Está en la página 1de 3

import numpy as np

import matplotlib.pyplot as plt


from sklearn import cluster
from sklearn import datasets
from sklearn.datasets import make_blobs
from pandas import DataFrame

from skimage import io


imagen = io.imread('impre_320_240.JPG')
print(imagen.shape)

largo,ancho,canales = imagen.shape;

imagen_columna = imagen.reshape(-1,3);
print(imagen_columna.shape)
print(imagen_columna)

plt.imshow(imagen)

(240, 320, 3)
(76800, 3)
[[156 191 245]
[154 189 243]
[154 189 243]
...
[148 181 216]
[147 180 215]
[145 178 213]]
<matplotlib.image.AxesImage at 0x7fac3d69c748>

#etiquetas
cuaderno, impresora, pared, pizarra, mueble_azul, mueble_gris = range(6)
0, 1, 2, 3, 4, 5,
print(cuaderno, impresora, pared, pizarra, mueble_azul, mueble_gris)

#np.full((3, 5), 7)
#numpy.array(n * [value])

cu = io.imread('cuaderno.png').reshape(-1,3); eti_cu = np.array(cu.shape[0]*[cua


( ) ( ) (
im = io.imread('impresora.png').reshape(-1,3); eti_im = np.array(im.shape[0]*[im
im2= io.imread('impresora2.png').reshape(-1,3); eti_im2= np.array(im2.shape[0]*[im
pa = io.imread('pared.png').reshape(-1,3); eti_pa = np.array(pa.shape[0]*[pa
pi = io.imread('pizarra.png').reshape(-1,3); eti_pi = np.array(pi.shape[0]*[pi
ma = io.imread('mueble_azul.png').reshape(-1,3); eti_ma = np.array(ma.shape[0]*[mue
mg = io.imread('mueble_gris.png').reshape(-1,3); eti_mg = np.array(mg.shape[0]*[mue

entrenos = np.concatenate((cu,im,im2,pa,pi,ma,mg))
etiquetas = np.concatenate((eti_cu,eti_im,eti_im2,eti_pa,eti_pi,eti_ma,eti_mg))
print(entrenos.shape, etiquetas.shape)

0 1 2 3 4 5
(135, 3) (135,)
(126, 3) (126,)
(70, 3) (70,)
(160, 3) (160,)
(294, 3) (294,)
(209, 3) (209,)
(216, 3) (216,)
(1210, 3) (1210,)

pred_kmeans = cluster.KMeans(n_clusters=6, random_state=170).fit_predict(imagen_co


pred_kmeans = pred_kmeans.reshape(240,320)
plt.imshow(pred_kmeans)

<matplotlib.image.AxesImage at 0x7fac3b987588>

from sklearn.neural_network import MLPClassifier


# all parameters not specified are set to their defaults
perceptron = MLPClassifier ( solver='sgd', hidden_layer_sizes=(25), \
activation='logistic',max_iter=500, \
early_stopping=True, verbose=False, \
random_state=0 )

perceptron.fit (entrenos, etiquetas)

pred_mpl = perceptron.predict(imagen_columna)

print(pred_mpl)
pred_mpl = pred_mpl.reshape(240,320)
plt.imshow(pred_mpl)
[2 2 2 ... 2 2 2]
<matplotlib.image.AxesImage at 0x7fa53809d7b8>

También podría gustarte