# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.datasets import load_digits
import pandas as pd
import matplotlib.pyplot as plt

data=load_digits()

(x_train,x_test,y_train,y_test)=train_test_split(data.data/255,data.target.astype("int0"),test_size=0.15)
m=x_train.shape[0]
m1=x_test.shape[0]
x_train=x_train.reshape(m,8,8,1)
x_test=x_test.reshape(m1,8,8,1)
x_train.shape

plt.gray()
plt.imshow(data.images[0]) 


import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout,Conv2D,Activation,MaxPooling2D,Flatten
import keras.backend as k

# +
model=Sequential()
model.add(Conv2D(32, (3, 3) ,padding="same",activation='relu', input_shape=(8,8,1)))
model.add(Conv2D(32, (3, 3) ,padding="same",activation='relu'))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3),padding="same", activation='relu'))
model.add(Conv2D(64, (3, 3), padding="same",activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

# +
opt=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)

model.compile(loss='sparse_categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])
# -

model.fit(x_train,y_train, epochs=40, batch_size=64, validation_data=(x_test,y_test),shuffle=True)

l=model.predict(x_test)
y_pred=np.argmax(l,axis=1)
y_pred

y_test

target_names=["class 0","class 1","class 2","class 3","class 4","class 5","class 6","class 7","class 8","class 9"]
print(classification_report(y_test, y_pred, target_names=target_names))

import cv2
import matplotlib.pyplot as plt

img=cv2.imread(r"download1.jpg")
plt.gray()
plt.imshow(img,cmap = plt.get_cmap(name = 'gray'))
img=img.resize(1,8,8,1)
arr=np.array(img)
arr

l=model.predict(arr)
y_pred=np.argmax(l,axis=1)
y_pred



from PIL import Image
import matplotlib.pyplot as plt

img = Image.open(r"download8.jpg")
img=img.resize((8,8))
plt.imshow(img)

pix_val = list(img.getdata())
len(pix_val)

pix_val=np.array(pix_val)
pix_val=pix_val[:,0].reshape(1,8,8,1)
pix_val=pix_val/255
pix_val

l=model.predict(pix_val)
y_pred=np.argmax(l,axis=1)
y_pred



StarcoderdataJupyterScriptsDedupFiltered