Android Application Development-Scenario Text Recognition and android Application Development

Source: Internet
Author: User

Android Application Development-Scenario Text Recognition and android Application Development

Because the graduate project needs to complete a Text Recognition System Based on mobile terminals, although it is still earlier than graduation, but for the sake of interest, this system has been completed recently. The basic architecture is as follows:

Client: the Android app can take pictures of scenes, roughly draw out the text areas of interest, and upload them to the server for identification through socket communication;

Server: the Python server performs socket communication listening. After the connection, it calls the text recognition engine (exe executable program) to return the recognition result;


The following is an example of system running:



1. Client

There are two activities: MainActivity main interface, for example, left 1. After you select shooting, call the system's internal photo service, for example, left 2. After taking the photo, enter KernelActivity, for example, left 3, after the text area of interest is roughly drawn, upload it to the server and obtain the recognition result, for example, 4 on the left.


When taking photos on the client and establishing network communication, you must declare the permission in the AndroidManifestxml file.

<! -- Authorize the CAMERA --> <uses-permission android: name = "android. permission. CAMERA"/> <! -- Create and delete objects on the SD card --> <uses-permission android: name = "android. permission. MOUNT_UNMOUNT_FILESYSTEMS"/> <! -- Write data permission to the SD card --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> <! -- Authorize INTERNET access --> <uses-permission android: name = "android. permission. INTERNET"/>



During Android programming on the client, the client connects to the server and uses socket for communication. There are several precautions during the programming process. Otherwise, errors may easily occur:

1.1 you cannot directly establish a network connection in the main thread

To prevent thread congestion, android 4.0 and later versions do not allow network connection to be established directly in the main thread (socket communication requires network connection ). Therefore, when socket communication is required, a new thread should be opened for network connection. The example is as follows:

New Thread () {// The main Thread cannot directly connect to the network. You need to create a subthread @ Overridepublic void run () {// TODO}. start ();


1.2 The close () method is not suitable for sending and receiving data streams from the client socket

The client needs to accept and send data at the same time, so there will be two objects: out and in

// Start socket communication Socket socket = new Socket ("210.77.27.123", 9058); // set the IP address based on your server // write output (upload) data DataOutputStream out = new DataOutputStream (socket. getOutputStream (); // write input (obtain) Data BufferedReader in = new BufferedReader (new InputStreamReader (socket. getInputStream (), "gb2312"); // sets the character encoding format to read Chinese characters correctly.


If you want to use out. close () to close an object after sending data to the server. In this case, the "socket closed" exception will be thrown when the in object is used to receive data "! The reason is that the close () method of the in or out object will cause the socket to close. Therefore, if you need to disable the function, you can use the shutdownInput () and shutdownOutput () methods, or wait until the last socket. close () is not processed.


1.3 The Sub-thread cannot update the UI

When obtaining the server-side recognition result, you need to use this content to update the client UI (TextView in the lower left corner). If you use text directly in the socket communication subthread mentioned above. if the setText () method tries to update the UI, the conclusion is not feasible. Therefore, to update the UI, you need to use the Handler object to update the UI in its handleMessage () method.

The sub-thread stores the server result into the message and sends the message to the predefined handler object.

String response = in. readLine (); Message msg = handler. obtainMessage (); // The UI cannot be updated in the Child thread. handlermsg must be used. obj = response; handler. sendMessage (msg );

Then handler will call its handleMessage () method to update the UI. handleMessage () is defined as follows:

Handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {String message = (String) msg. obj; if (message! = Null) text. setText (message); else text. setText ("sorry, failed to recognize"); super. handleMessage (msg); // This sentence must exist! Otherwise, the UI cannot be updated }};



2. Server Side

The server's main component is pythonscript to listen to socket. Once a connection is established, the developed scene text recognition engine (strengine.exe) is used and the result is returned to the client. The python server script is as follows:

######################################                                   ##       STC Recognition Server      ##                                   ######################################import socketimport oss = socket.socket()host = socket.gethostname()port = 9058s.bind((host, port))s.listen(5)while True:    c, addr = s.accept()    print 'Got connection from' , addr    #get uploaded params    params = c.recv(19)    lst = params.split(' ')    if len(lst) < 4:        c.close()        continue    print 'x=%d, y=%d, w=%d, h=%d' % (int(lst[0]),int(lst[1]),int(lst[2]),int(lst[3]))    #get uploaded image size info    size = int(c.recv(4))        #save uploaded image    f = open('.\\upload\\tmp.jpg', 'wb')    for i in range(size):        f.write(c.recv(1024))    f.close()    print 'Image received, size %dKB' % size    #execute STREngine on server and send back the result    result = os.popen('STREngine ./upload/tmp.jpg ' + params).read()    print 'Recognition result:', result    c.send(result + '\n')    print 'Close connection with', addr, '\n'    c.close()


The internal code of the text recognition engine cannot be shared, but executable files will be shared with interested friends.


There is something very important on the server side. If you don't pay attention to it, it may bring you endless troubles. It took me two nights to discover this small problem. Here, I will share it to avoid wasting other people's time:

When the server returns the recognition result to the client, the line break '\ n' must end with the string! If this parameter is not added, the client in. readline () method will be blocked because the end of the row cannot be found, and the client UI cannot be updated using the returned results. This problem is annoying, because if you do not add '\ n' to the server, it is okay to use the android virtual machine on the local computer. The UI is updated normally on the virtual machine, however, once switched to the mobile phone, the system did not respond (without updating the UI ).



Summary:

In fact, after the client manually draws the text of interest and uploads it, the location of this region needs to be carefully converted in the ImageView and the actual image. I will not elaborate on it here, if you need it, check the Code directly. Or we strongly recommend that you analyze and deduce the relationship by yourself, which will be of great help to deepen the understanding of ImageView and Bitmap.

In addition, because it is not a product, it does not care about efficiency. Now we upload the entire image after taking a photo on our mobile phone. Although the image is compressed, there are still several hundred KB in size in a picture, which is a waste of traffic. The solution is also simple. You only need to extract the selected text area and upload it separately (but the surrounding area needs to be expanded to a certain extent). The size should be reduced by dozens of times.

The client and server Source Code (including recognition engine executable programs) have been shared with CSDN. If you have any questions, please feel free to contact us.










Android development requires text recognition on images. I have been searching for many materials online for a long time, but I still don't have a system to understand.

Similar to License Plate Recognition. In fact, you don't need to focus on how to recognize text from images. Don't worry about Platform issues, so you will have a comprehensive understanding.

How to Create a Chinese template font in android? Mainly used for text recognition on images, which may contain up to one hundred characters. Thank you!

You have a Master's thesis. I don't know. Biji is doing something similar, but it's much simpler. It's just feature recognition, and you don't need a template library.
I think you are very difficult.
Gray, filtering, and enhancement image processing should be a top priority.
Then, image cutting requires algorithms. You must also check the specific features of your image to determine the region of each problem.
Then, do pattern classes need features? You still have to look for the frequently used feature literature for design text recognition.
Then we need to design our own algorithm for extracting features from text images, which is not easy.
If the previous work is done, 100 words are classified by feature, and saved by feature value.

This is probably the case. Android and java are a small problem. The key is how to design algorithms.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.