├── Figure_1.png ├── handwrite.rar ├── Images ├── image.webp ├── patchs.png ├── sema.png ├── DeepWriter.png ├── classifier.png ├── image (1).webp ├── visualize.png ├── architecture.png └── Screenshot 2025-02-21 122712.png ├── README.md ├── simpletestimg ├── visualize.png └── hsf_7_00017.png ├── handwriting_letter_recognition.h5 ├── iam_dataset └── data_subset │ ├── img-1.webp │ ├── img-17.png │ ├── img-19.png │ ├── img-2.webp │ ├── img-20.png │ ├── img-3.webp │ ├── img-38.png │ ├── img-4.webp │ ├── img-42.png │ ├── img-43.png │ ├── img-5.webp │ ├── img-7.webp │ ├── img-8.webp │ ├── img-9.webp │ ├── img-10.webp │ ├── img-11.webp │ ├── img-12.webp │ ├── img-13.webp │ ├── img-14.webp │ ├── img-15.webp │ ├── img-16.webp │ ├── img-18.webp │ ├── img-21.webp │ ├── img-22.webp │ ├── img-23.webp │ ├── img-24.webp │ ├── img-25.webp │ ├── img-26.webp │ ├── img-27.webp │ ├── img-28.webp │ ├── img-29.webp │ ├── img-30.webp │ ├── img-31.webp │ ├── img-32.webp │ ├── img-33.webp │ ├── img-34.webp │ ├── img-35.webp │ ├── img-36.webp │ ├── img-37.webp │ ├── img-39.webp │ ├── img-40.webp │ └── img-41.webp ├── fullsentence.py └── train.py /Figure_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Figure_1.png -------------------------------------------------------------------------------- /handwrite.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/handwrite.rar -------------------------------------------------------------------------------- /Images/image.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/image.webp -------------------------------------------------------------------------------- /Images/patchs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/patchs.png -------------------------------------------------------------------------------- /Images/sema.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/sema.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##This is handwriting sentence recognized python code. 2 | I doesn't work properly. 3 | -------------------------------------------------------------------------------- /Images/DeepWriter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/DeepWriter.png -------------------------------------------------------------------------------- /Images/classifier.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/classifier.png -------------------------------------------------------------------------------- /Images/image (1).webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/image (1).webp -------------------------------------------------------------------------------- /Images/visualize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/visualize.png -------------------------------------------------------------------------------- /Images/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/architecture.png -------------------------------------------------------------------------------- /simpletestimg/visualize.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/simpletestimg/visualize.png -------------------------------------------------------------------------------- /simpletestimg/hsf_7_00017.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/simpletestimg/hsf_7_00017.png -------------------------------------------------------------------------------- /handwriting_letter_recognition.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/handwriting_letter_recognition.h5 -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-1.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-1.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-17.png -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-19.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-19.png -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-2.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-2.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-20.png -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-3.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-3.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-38.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-38.png -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-4.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-4.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-42.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-42.png -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-43.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-43.png -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-5.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-5.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-7.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-7.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-8.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-8.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-9.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-9.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-10.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-10.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-11.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-11.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-12.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-12.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-13.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-13.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-14.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-14.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-15.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-15.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-16.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-16.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-18.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-18.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-21.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-21.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-22.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-22.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-23.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-23.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-24.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-24.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-25.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-25.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-26.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-26.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-27.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-27.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-28.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-28.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-29.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-29.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-30.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-30.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-31.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-31.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-32.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-32.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-33.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-33.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-34.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-34.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-35.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-35.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-36.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-36.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-37.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-37.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-39.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-39.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-40.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-40.webp -------------------------------------------------------------------------------- /iam_dataset/data_subset/img-41.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/iam_dataset/data_subset/img-41.webp -------------------------------------------------------------------------------- /Images/Screenshot 2025-02-21 122712.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terabyte-sourcer/handAI/HEAD/Images/Screenshot 2025-02-21 122712.png -------------------------------------------------------------------------------- /fullsentence.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | import os 4 | import tensorflow as tf 5 | load_model = tf.keras.models.load_model 6 | from sklearn.preprocessing import LabelEncoder 7 | from PIL import Image 8 | import matplotlib.pyplot as plt 9 | 10 | # 🔹 Load Trained Model 11 | MODEL_PATH = "handwriting_letter_recognition.h5" 12 | model = load_model(MODEL_PATH) 13 | 14 | # 🔹 Define Label Encoder (A-Z) 15 | letters = [chr(i) for i in range(65, 91)] + [chr(i) for i in range(97, 123)] # A-Z and a-z 16 | encoder = LabelEncoder() 17 | encoder.fit(letters) 18 | 19 | # 🔹 Character Segmentation Using OpenCV 20 | def segment_characters(image_path): 21 | img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) 22 | _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) 23 | 24 | # Apply morphological operations to remove noise 25 | kernel = np.ones((3,3), np.uint8) 26 | thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) 27 | 28 | # Find contours 29 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 30 | 31 | char_images = [] 32 | bounding_boxes = [] 33 | 34 | for contour in contours: 35 | x, y, w, h = cv2.boundingRect(contour) 36 | if w > 5 and h > 10: # Ignore noise 37 | char = img[y:y+h, x:x+w] 38 | char = cv2.resize(char, (28, 28)) # Resize for CNN input 39 | char_images.append(char) 40 | bounding_boxes.append((x, y, w, h)) 41 | 42 | # Sort characters from left to right 43 | sorted_chars = [char for _, char in sorted(zip(bounding_boxes, char_images), key=lambda b: b[0])] 44 | return sorted_chars 45 | 46 | 47 | # 🔹 Predict Letters Using the CNN Model 48 | def recognize_sentence(image_path): 49 | characters = segment_characters(image_path) 50 | 51 | sentence = "" 52 | for char in characters: 53 | char = np.array(char).reshape(1, 28, 28, 1).astype("float32") / 255.0 54 | 55 | prediction = model.predict(char) 56 | predicted_label = encoder.inverse_transform([np.argmax(prediction)]) 57 | sentence += predicted_label[0] 58 | 59 | print(f"Recognized Sentence: {sentence}") 60 | return sentence 61 | 62 | # 🔹 Test with a Sample Image 63 | image_path = "./iam_dataset/data_subset/img-19.png" 64 | recognized_text = recognize_sentence(image_path) 65 | 66 | # 🔹 Display the Image 67 | img = Image.open(image_path) 68 | plt.imshow(img, cmap="gray") 69 | plt.title(f"Recognized Text: {recognized_text}") 70 | plt.show() 71 | -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorflow_datasets as tfds 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import string 6 | 7 | to_categorical = tf.keras.utils.to_categorical 8 | Adam = tf.keras.optimizers.Adam 9 | Conv2D = tf.keras.layers.Conv2D 10 | MaxPooling2D = tf.keras.layers.MaxPooling2D 11 | Flatten = tf.keras.layers.Flatten 12 | Dense = tf.keras.layers.Dense 13 | Dropout = tf.keras.layers.Dropout 14 | Sequential = tf.keras.models.Sequential 15 | 16 | # 📥 Load EMNIST Dataset (Letters only) 17 | dataset, info = tfds.load("emnist/balanced", split="train", as_supervised=True, with_info=True) 18 | 19 | # Convert dataset to NumPy arrays 20 | X_data, y_data = [], [] 21 | 22 | for image, label in dataset: 23 | img = tf.image.resize(image, (28, 28)) # Resize to 28x28 24 | img = tf.cast(img, tf.float32) / 255.0 # Normalize 25 | X_data.append(img.numpy()) 26 | 27 | # Here, assign labels for both upper and lower cases (e.g., 0-25 for 'A-Z', 26-51 for 'a-z') 28 | label_index = label.numpy() - 1 # Adjust label index to 0-25 for lowercase 29 | y_data.append(label_index) # Assuming the dataset is lowercase only 30 | 31 | letters = list(string.ascii_uppercase + string.ascii_lowercase) # ['A', 'B', ... 'Z', 'a', 'b', ... 'z'] 32 | 33 | num_classes = len(letters) 34 | X_data = np.array(X_data).reshape(-1, 28, 28, 1) 35 | y_data = to_categorical(np.array(y_data), num_classes=num_classes) 36 | 37 | from sklearn.model_selection import train_test_split 38 | X_train, X_test, y_train, y_test = train_test_split(X_data, y_data, test_size=0.2, random_state=42) 39 | 40 | print(f"Train images: {X_train.shape}, Train labels: {y_train.shape}") 41 | print(f"Test images: {X_test.shape}, Test labels: {y_test.shape}") 42 | 43 | model = Sequential([ 44 | Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 1)), 45 | MaxPooling2D((2, 2)), 46 | Conv2D(64, (3, 3), activation="relu"), 47 | MaxPooling2D((2, 2)), 48 | Conv2D(128, (3, 3), activation="relu"), 49 | MaxPooling2D((2, 2)), 50 | Flatten(), 51 | Dense(256, activation="relu"), 52 | Dropout(0.5), 53 | Dense(num_classes, activation="softmax") # Updated for 52 classes 54 | ]) 55 | 56 | 57 | model.compile(optimizer=Adam(learning_rate=0.001), loss="categorical_crossentropy", metrics=["accuracy"]) 58 | 59 | history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test)) 60 | 61 | plt.figure(figsize=(12, 5)) 62 | plt.subplot(1, 2, 1) 63 | plt.plot(history.history["accuracy"], label="Train Accuracy") 64 | plt.plot(history.history["val_accuracy"], label="Validation Accuracy") 65 | plt.legend() 66 | plt.title("Training vs Validation Accuracy") 67 | 68 | plt.subplot(1, 2, 2) 69 | plt.plot(history.history["loss"], label="Train Loss") 70 | plt.plot(history.history["val_loss"], label="Validation Loss") 71 | plt.legend() 72 | plt.title("Training vs Validation Loss") 73 | 74 | plt.show() 75 | 76 | model.save("handwriting_letter_recognition.h5") 77 | 78 | test_loss, test_acc = model.evaluate(X_test, y_test) 79 | print(f"Test Accuracy: {test_acc * 100:.2f}%") 80 | --------------------------------------------------------------------------------