├── README.md ├── model.h5 ├── number recognize using keras.py ├── prediction.mp4 └── project.py /README.md: -------------------------------------------------------------------------------- 1 | # Digit-recognition 2 | 3 | In this project I have made a deep learning model to predict the handwritten number using keras. 4 | In the number recongnize using keras file, I have used MNIST Handwritten images Datasets to train the model, after training the model 5 | we can save the model trained so that we dont need to train the model every time we run the program. 6 | Then in project.py file using opencv i have built a GUI through which you can draw an number and save it by pressing 's' or 'S' 7 | the function then reads the saved image and then returns to the main program. The image is then converted to required size for the model to predict the number. The image is then fed to the model and it returns the predicted number. The number is then written on 8 | the Predicted number. 9 | You can see prediction.mp4 how it works. 10 | -------------------------------------------------------------------------------- /model.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jagadishb1409/Digit-recognition/4ff704a3432384b3927ee904f066ae7bc12afcc9/model.h5 -------------------------------------------------------------------------------- /number recognize using keras.py: -------------------------------------------------------------------------------- 1 | # importing keras 2 | from tensorflow import keras 3 | 4 | # dividing MNIST datasets to training and test datasets 5 | (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() 6 | x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) 7 | x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) 8 | # converting dataset to float32 type 9 | x_train = x_train.astype('float32') 10 | x_test = x_test.astype('float32') 11 | x_train /= 255 12 | x_test /= 255 13 | 14 | # preparing the model 15 | model = keras.models.Sequential([ 16 | keras.layers.Conv2D(64, (2, 2), padding='same', 17 | activation='elu', input_shape=[28, 28, 1]), 18 | keras.layers.MaxPooling2D(2), 19 | keras.layers.Conv2D(128, (3, 3), padding="same", activation="elu"), 20 | keras.layers.Conv2D(256, (4, 4), padding="same", activation="elu"), 21 | keras.layers.Dropout(0.5), 22 | keras.layers.MaxPooling2D(2), 23 | keras.layers.Flatten(), 24 | keras.layers.Dense(128, activation="elu"), 25 | keras.layers.Dropout(0.25), 26 | keras.layers.Dense(10, activation="softmax") 27 | ]) 28 | 29 | # compiling the model 30 | model.compile(loss="sparse_categorical_crossentropy", metrics=[ 31 | "accuracy"], optimizer=keras.optimizers.SGD(lr=0.08)) 32 | print(model.summary()) 33 | # train the model 34 | model.fit(x_train, y_train, epochs=5) 35 | # Evaluate the model 36 | model.evaluate(x_test, y_test) 37 | # saving the model 38 | model.save('model.h5') 39 | print('Model Saved') 40 | -------------------------------------------------------------------------------- /prediction.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jagadishb1409/Digit-recognition/4ff704a3432384b3927ee904f066ae7bc12afcc9/prediction.mp4 -------------------------------------------------------------------------------- /project.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | from tensorflow import keras 4 | 5 | windowName = 'Draw an number' 6 | img = np.zeros((512, 512, 3), np.uint8) 7 | cv2.namedWindow(windowName) 8 | # true if mouse is pressed 9 | drawing = False 10 | # if True, draw rectangle. Press 'm' to toggle to curve 11 | mode = True 12 | (ix, iy) = (-1, -1) 13 | # mouse callback function 14 | 15 | # brush function 16 | 17 | 18 | def draw_shape(event, x, y, flags, param): 19 | global ix, iy, drawing, mode 20 | if event == cv2.EVENT_LBUTTONDOWN: 21 | drawing = True 22 | (ix, iy) = x, y 23 | elif event == cv2.EVENT_MOUSEMOVE: 24 | if drawing: 25 | if mode: 26 | cv2.circle(img, (x, y), 15, (0, 0, 255), -1) 27 | elif event == cv2.EVENT_LBUTTONUP: 28 | drawing = False 29 | if mode: 30 | cv2.circle(img, (x, y), 15, (0, 0, 255), -1) 31 | 32 | 33 | cv2.setMouseCallback(windowName, draw_shape) 34 | 35 | 36 | def main(): 37 | global mode 38 | 39 | while(True): 40 | cv2.imshow(windowName, img) 41 | 42 | k = cv2.waitKey(1) 43 | if k == ord('m') or k == ord('M'): 44 | mode = not mode 45 | # 's' is used to save the image you have drawn in Window 46 | elif k == ord('s') or k == ord('S'): 47 | cv2.imwrite('num.png', img) 48 | elif k == 27: 49 | break 50 | 51 | cv2.destroyAllWindows() 52 | img2 = cv2.imread('num.png') 53 | return img2 54 | 55 | 56 | if __name__ == "__main__": 57 | img = main() 58 | img2 = img 59 | # converting Color of the image to gray 60 | img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 61 | # resize the image 62 | img = cv2.resize(img, (28, 28), interpolation=cv2.INTER_AREA) 63 | # convert img to float32 type 64 | img = img.astype('float32') 65 | img /= 255 66 | img = np.reshape(img, (1, 28, 28, 1)) 67 | 68 | # loading the previously saved model 69 | model1 = keras.models.load_model('model.h5') 70 | # predicting the number in the image 71 | num = model1.predict_classes(img)[0] 72 | # prints the number predicted 73 | print('number = ', num) 74 | f = np.zeros((500, 500, 3), dtype='uint8') 75 | font = cv2.FONT_HERSHEY_SIMPLEX 76 | org = (20, 20) 77 | fontScale = 1 78 | color = (255, 255, 0) 79 | thickness = 2 80 | print(num) 81 | # shows the predicted number in the image 82 | img2 = cv2.putText(img2, f'Predicted number = {num}', org, font, 83 | fontScale, color, thickness, cv2.LINE_AA) 84 | 85 | cv2.imshow('Predicted', img2) 86 | cv2.waitKey(0) 87 | cv2.destroyAllWindows() 88 | --------------------------------------------------------------------------------