Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build and deploy alphabet recognition system in python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to build and deploy a letter recognition system in python". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Convolution neural network

Let's start by understanding what a convolution neural network is. Convolution neural network (CNN) is a kind of neural network which is widely used in image recognition and classification.

Cnn is a regularized version of multilayer perceptrons. A multilayer perceptron usually refers to a fully connected network, in which each neuron in one layer is connected to all neurons in the next layer.

CNN consists of the following layers:

Convolution layer: a "core" the size of 3X3 or 5X5 is passed to the image and the dot product of the original pixel value and the weight defined in the kernel is calculated. The matrix then uses an activation function "ReLu", which converts each negative value in the matrix to zero.

Pooling layer: the size of the "pooling matrix" is 2X2 or 4X4. By pooling, the size of the matrix is reduced to highlight only the important features of the image.

There are two types of pool operations:

The maximum pool is a pool type, and the maximum value that exists in the pooling matrix is put into the final matrix.

The average pool (Average Pooling) is a pool type in which the average of all values in the pooling matrix is calculated and placed in the final matrix.

Note: there can be multiple combinations of convolution and pool tiers in the CNN architecture to improve its performance.

Full connection layer: the final matrix is flattened into an one-dimensional vector. The vector is then input into the neural network. Finally, the output layer is a probability list of different tags (such as the letters a, b, c) attached to the image. The label with the highest probability is the output of the classifier.

CNN implementation

Let's start by importing the libraries in Jupyter Notebook, as follows:

Import numpy as npimport matplotlib.pyplot as pltfrom keras.preprocessing.image import ImageDataGeneratorfrom keras.preprocessing import imageimport kerasfrom keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activationimport osimport pickle

Then, let's import two datasets containing images from a to z to train and test our model.

Train_datagen = ImageDataGenerator (rescale = 1.amp255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True) test_datagen = ImageDataGenerator (rescale = 1.Accord255) train_generator = train_datagen.flow_from_directory (directory = 'Training', target_size = (32) Batch_size = 32, class_mode = 'categorical') test_generator = test_datagen.flow_from_directory (directory =' Testing', target_size = (32), batch_size = 32, class_mode = 'categorical')

ImageDataGenerator generates a batch of tensor image data and converts RGB coefficients in the range of 0-255 to target values between 0 and 1 by scaling using rescale.

Shear_range is used to randomly apply shear transformations.

Zoom_range is used to randomly zoom inside the picture.

Horizontal_flip is used to randomly flip half of the image horizontally.

Then we use * * .flow_from_directory** to import images one by one from the directory and apply ImageDataGenerator to them.

Then we convert the image from the original size to the target size and declare the batch size, which refers to the number of training examples used in one iteration.

Then we set class_mode to category, which means we have multiple classes (a to z) to predict.

Next, let's build our CNN architecture.

Model = Sequential () model.add (Conv2D (32, (3, 3), input_shape = (32, (3, 3), input_shape = (32, (3, 3), activation = 'relu')) model.add (MaxPooling2D (pool_size = (2, 2) model.add (Conv2D (32, (3, 3), activation =' relu') model.add (MaxPooling2D (pool_size = (2, 2)) model.add (Flatten ()) model.add (Dense (units = 128, activation = 'relu') model.add (Dense (units = 26)) Activation = 'softmax')) model.compile (optimizer =' adam', loss = 'categorical_crossentropy', metrics = [' accuracy']) model.summary ()

Let's start by creating a Sequential model that allows us to define the CNN schema layer by layer using the .add function.

We first add a convolution layer with 32 3X3-sized filters (cores) to the input image and activate the function through "relu".

Then we use a pool of the size 2X2 to perform the MaxPooling operation.

Then repeat these layers again to improve the performance of the model.

Finally, we flatten the resulting matrix and pass through a fully connected layer of 128 nodes. It is then connected to an output layer of 26 nodes, each representing an alphabet. We use softmax activation to transform the score into a normalized probability distribution and select the node with the highest probability as the output.

Once our CNN schema is defined, we compile the model using the adam optimizer.

Finally, we train the model.

Model.fit_generator (train_generator, steps_per_epoch = 16, epochs = 3, validation_data = test_generator, validation_steps = 16)

The accuracy of the model after training is 93.42%.

Now let's test our model. But before we do this, we need to define a function that gives us the alphabet associated with the result.

Def get_result (result): if result [0] [0] = = 1: return ('a') elif result [0] [1] = 1: return ('b') elif result [0] [2] = 1: return ('c') elif result [0] [3] = 1: return ('d') elif result [0] [4] = 1: return (' E') elif result [0] [5] = = 1: return ('f') elif result [0] [6] = 1: return ('g') elif result [0] [7] = 1: return ('h') elif result [0] [8] = 1: return ('i') elif result [0] [9] = 1: return ('j') Elif result [0] [10] = = 1: return ('K') elif result [0] [11] = = 1: return ('l') elif result [0] [12] = 1: return ('m') elif result [0] [13] = 1: return ('n') elif result [0] [14] = 1: return ('o') elif result [0] [15] = 1: return ('p') elif result [0] [16] = 1: return ('q') elif result [0] [17] = 1: return ('r') elif result [0] [18] = 1: return ('s') elif result [0] [19] = 1: return ('t') elif result [0] [20] = = 1 : return ('u') elif result [0] [21] = = 1: return ('v') elif result [0] [22] = 1: return ('w') elif result [0] [23] = 1: return ('x') elif result [0] [24] = 1: return ('y') elif result [0] [25] = 1: return ('z')

Finally, let's test our model:

Filename = r'Testing\ e\ 25.png'test_image = image.load_img (filename, target_size = (32)) plt.imshow (test_image) test_image = image.img_to_array (test_image) test_image = np.expand_dims (test_image, axis = 0) result = model.predict (test_image) result = get_result (result) print ('Predicted Alphabet is: {}' .format (result))

The model correctly predicts the letters of the input image, and the result is "e".

Anvil integration

Anvil is a platform that allows us to build full-stack web applications using python. It makes it easier for us to transform machine learning models from Jupyter Notebook to web applications.

Let's first create an account on anvil. When you are done, create a new blank application with material design.

Check out this link for a step-by-step tutorial on how to use anvil: https://anvil.works/learn

The toolbox on the right contains all the components that can be dragged to the site.

Required components:

2 tags (title and subtitle)

Image (display input image)

FileLoader (upload input image)

Highlight button (for predicting results)

Label (view results)

Drag and drop these components and arrange them according to your requirements.

To add a title and subtitle, select the label and in the attributes section on the right, then go to the option named text, as shown below (highlighted in red), and then type the title / subtitle.

After completing the user interface, go to the code section shown above (highlighted in green) and create a new function, as shown below

Def primary_color_1_click (self, * * event_args): file = self.file_loader_1.file self.image_1.source = file result = anvil.server.call ('model_run',file) self.label_3.text = result pass

This function is executed when we press the "PREDICT" button. It takes the input image uploaded from the file loader and passes it to the "model_run" function of Jupyter Notebook. This function will return prediction letters.

Now all we have to do is connect our anvil website to Jupyter Notebook.

This requires the following two steps:

Import Anvil uplink (uplink) keys: click the Settings button, then click uplinks, click enable uplink keys and copy keys.

Paste the following in Jupyter Notebook:

Import anvil.serverimport anvil.mediaanvil.server.connect ("paste your anvil uplink key here")

Create a "model_run" function to predict the images uploaded to the site.

Anvil.server.callabledef model_run (path): with anvil.media.TempFile (path) as filename: test_image = image.load_img (filename, target_size = (32)) test_image = image.img_to_array (test_image) test_image = np.expand_dims (test_image) Axis = 0) result = model.predict (test_image) result = get_result (result) return ('Predicted Alphabet is: {}' .format (result))

Now you can go back to anvil, click the run button, and the letter recognition system is complete.

This is the end of "how to build and deploy a letter recognition system in python". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report