text_plain_output_1.png
from imutils.contours import sort_contours

from matplotlib import cm

from sklearn.model_selection import train_test_split

from tensorflow import keras

from tensorflow.keras.optimizers import RMSprop

import cv2

import imutils

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import tensorflow as tf

import tensorflow as tf

import tensorflow as tf

mnist = keras.datasets.mnist
(train_images_mnist, train_labels_mnist), (test_images_mnist, test_labels_mnist) = mnist.load_data()
train_images_mnist = np.reshape(train_images_mnist, (train_images_mnist.shape[0], 28, 28, 1))
test_images_mnist = np.reshape(test_images_mnist, (test_images_mnist.shape[0], 28, 28, 1))

az_path = '/kaggle/input/az-handwritten-alphabets-in-csv-format/A_Z Handwritten Data.csv'
AZ_data = pd.read_csv(az_path, header=None)
AZ_labels = AZ_data.values[:, 0]
AZ_images = AZ_data.values[:, 1:]
AZ_images = np.reshape(AZ_images, (AZ_images.shape[0], 28, 28, 1))

from sklearn.model_selection import train_test_split
test_size = float(len(test_labels_mnist)) / len(train_labels_mnist)
train_images_AZ, test_images_AZ, train_labels_AZ, test_labels_AZ = train_test_split(AZ_images, AZ_labels, test_size=test_size)
train_labels_mnist = train_labels_mnist + max(AZ_labels) + 1
test_labels_mnist = test_labels_mnist + max(AZ_labels) + 1
train_images = np.concatenate((train_images_AZ, train_images_mnist), axis=0)
train_labels = np.concatenate((train_labels_AZ, train_labels_mnist))
test_images = np.concatenate((test_images_AZ, test_images_mnist), axis=0)
test_labels = np.concatenate((test_labels_AZ, test_labels_mnist))

import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(512, activation='relu'), tf.keras.layers.Dense(len(np.unique(train_labels)), activation='softmax')])
model.compile(optimizer=RMSprop(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.summary()

image_path = '/kaggle/input/tester2/pja 4.jpg'
image = cv2.imread(image_path)

# perform edge detection, find contours in the edge map, and sort the
# resulting contours from left-to-right
edged = cv2.Canny(blurred, 30, 250) #low_threshold, high_threshold
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sort_contours(cnts, method="left-to-right")[0]

figure = plt.figure(figsize=(7,7))
plt.axis('off');
plt.imshow(edged,cmap=cm.binary_r);

chars = []
for c in cnts:
    x, y, w, h = cv2.boundingRect(c)
    roi = cropped[y:y + h, x:x + w]
    thresh = cv2.threshold(roi, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    tH, tW = thresh.shape
    if tW > tH:
        thresh = imutils.resize(thresh, width=28)
    else:
        thresh = imutils.resize(thresh, height=28)
    tH, tW = thresh.shape
    dX = int(max(0, 28 - tW) / 2.0)
    dY = int(max(0, 28 - tH) / 2.0)
    padded = cv2.copyMakeBorder(thresh, top=dY, bottom=dY, left=dX, right=dX, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0))
    padded = cv2.resize(padded, (28, 28))
    padded = padded.astype('float32') / 255.0
    padded = np.expand_dims(padded, axis=-1)
    chars.append((padded, (x, y, w, h)))

n_cols = 10
n_rows = int(np.floor(len(chars) / n_cols) + 1)
fig = plt.figure(figsize=(1.5 * n_cols, 1.5 * n_rows))
for i, char in enumerate(chars):
    ax = plt.subplot(n_rows, n_cols, i + 1)
    ax.imshow(char[0][:, :, 0], cmap=cm.binary, aspect='auto')
plt.tight_layout()