text: # -*- coding: utf-8 -*-
  """Untitled0.ipynb
  
  Automatically generated by Colaboratory.
  
  Original file is located at
      https://colab.research.google.com/drive/1CiSN3oNJeAnF0z0rrybxoyXR5wtO0xK_
  """
  
  import PIL
  from PIL import Image, ImageOps
  import ssl
  import requests
  import numpy as np
  from sklearn.cluster import DBSCAN, KMeans
  import matplotlib.pyplot as plt
  from collections import Counter
  import tensorflow as tf
  from keras.datasets import mnist
  from keras.models import Sequential
  from keras.layers import Dense
  from keras.layers import Dropout
  from keras.layers import Flatten
  from keras.layers.convolutional import Conv2D
  from keras.layers.convolutional import MaxPooling2D
  from keras.optimizers import Adam
  from keras.utils import np_utils
  from PIL import Image
  import numpy as np
  import os
  
  #1,INPUT: Read the image
  image = Image.open("car plate (1).png")
  
  #2. convert to gray scale
  gray_image = ImageOps.grayscale(image)
  
  #3. Separeta the umage unto digits (Clustering)
  #3.1 Convert the pixels into a list of points
  #print(gray_image)
  pixels = np.asanyarray(gray_image)
  points = []
  maxX = len(pixels)
  maxY = len(pixels[0])
  for i in range(maxX):
      for j in range(maxY):
          if pixels[i][j] < 100:
              points.append((i,j))
  
  #3.2 finding cluster of pixels to set numbers
  
  clustering = DBSCAN(eps=2, min_samples=5)
  clusters = clustering.fit_predict(points)
  
  points_2 = np.array(points)
  plt.scatter(points_2[:, 1], points_2[:, 0], c=clusters, cmap="plasma")
  plt.show()
  
  n_cluster = len(Counter(clusters).keys())
  
  imgs = [[] for _ in range(n_cluster)]# Create 1 array per cluster
  for i in range(len(clusters)):
      imgs[clusters[i]].append(points[i])
  
  MIN_X, MIN_Y, MAX_X, MAX_Y = 0,1,2,3
  bounds = [[1000,1000,0,0] for _ in range(n_cluster)]
  #Find boundaries for every image
  i=0
  for img in imgs:
      for x,y in img:
          if x < bounds[i][MIN_X]: bounds[i][MIN_X] = x
          if x > bounds[i][MAX_X]: bounds[i][MAX_X] = x
          if y < bounds[i][MIN_Y]: bounds[i][MIN_Y] = y
          if y > bounds[i][MAX_Y]: bounds[i][MAX_Y] = y
      i += 1
  
  for i in range(n_cluster):
      b = bounds[i]
      size = max(b[MAX_X] - b[MIN_X], b[MAX_Y]-b[MIN_Y]) + 6
      new_img = [[0 for _ in range(size)] for _ in range(size)]
      for i in range(len(imgs)):
          x,y $
  
  
  
  # 1.- Create the Model
  model = tf.keras.Sequential()
  
  # 2.- Define the architecture
  input_layer = tf.keras.layers.Flatten(input_shape=(28, 28))
  model.add(input_layer)
  
  hidden_layer = tf.keras.layers.Dense(200, activation="sigmoid")
  model.add(hidden_layer)
  
  output_layer = tf.keras.layers.Dense(10, activation="sigmoid")
  model.add(output_layer)
  
  # 3.- Train the network
  model.compile(optimizer=tf.keras.optimizers.SGD(0.01),
                loss='sparse_categorical_crossentropy', metrics=['accuracy'])
  
  mnist = tf.keras.datasets.mnist
  
  (x_train, y_train), (x_test, y_test) = mnist.load_data()
  
  #So I have to set the images created as the x_test for the model and this code do it:
  #https://blog.tanka.la/2018/10/28/build-the-mnist-model-with-your-own-handwritten-digits-using-tensorflow-keras-and-python/
  
  x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1).astype('float32')
  x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1).astype('float32')
  
  # To load images to features and labels
  def load_images_to_data(image_label, image_directory, features_data, label_data):
      list_of_files = os.listdir(image_directory)
      for file in list_of_files:
          image_file_name = os.path.join(image_directory, file)
          if ".png" in image_file_name:
              img = Image.open(image_file_name).convert("L")
              img = np.resize(img, (28,28,1))
              im2arr = np.array(img)
              im2arr = im2arr.reshape(1,28,28,1)
              features_data = np.append(features_data, im2arr, axis=0)
              label_data = np.append(label_data, [image_label], axis=0)
      return features_data, label_data
  
  x_test, y_test = load_images_to_data('1', '/content/', x_test, y_test)
  
  x_test/=255
  
  # With that I can use my images as test!
  
  history = model.fit(x_train, y_train, batch_size=1000, epochs=50)
  
  # 4. Saving the model
  model.save('saved_model/my_model')
  
  import matplotlib.pyplot as plt
  
  # 5. Display the results
  plt.figure(figsize=(10, 6))
  plt.subplot(2, 2, 1)
  plt.plot(range(len(history.history['accuracy'])), history.history['accuracy'])
  plt.ylabel('accuracy')
  plt.xlabel('epochs')
  plt.subplot(2, 2, 2)
  plt.plot(range(len(history.history['loss'])), history.history['loss'])
  plt.ylabel('loss')
  plt.xlabel('epochs')
  plt.show()
  
  #Loading the model
  new_model = tf.keras.models.load_model('saved_model/my_model/')
  
  img = Image.open('image_12.png').convert("L")
  im2arr = np.array(img)
  im2arr = im2arr.reshape(1,28,28,1)
  y_pred = new_model.predict_classes(im2arr)
  print(y_pred)
  
  """# Sección nueva"""