How to develop an Android translation program

Time:2020-8-9

Introduction

There must be a lot of friends who like to travel abroad. It’s better to go abroad for a tour. Before traveling, everyone will do a variety of strategies on food, clothing, accommodation, transportation and play routes, and then set out with full expectation

Travel in imagination

   before departure, the imaginary tourist destination may have beautiful buildings:
How to develop an Android translation program

  delicious food:
How to develop an Android translation program
Beautiful little sister:
How to develop an Android translation program
Leisurely life
How to develop an Android translation program

Tourism in reality

But in reality, if you go to a place where you don’t know your language, you may encounter the following problems:

A fascinating map:
How to develop an Android translation program

Dream menu:
How to develop an Android translation program
                 
How to develop an Android translation program
How to develop an Android translation program
       shopping malls
How to develop an Android translation program
How to develop an Android translation program
  it’s too hard
How to develop an Android translation program

Photo translator helps you

With Huawei HMS ml kit text recognition and translation services, these are not problems. Today, I would like to introduce how to use the SDK provided by Huawei HMS ml kit to develop a photo translation service. In short, it only takes two steps to complete the development of photo translation app

Text recognition

   take a photo to get the picture, and then send the acquired image frame to Huawei HMS ml kit text recognition service for text recognition.
   Huawei text recognition service provides offline SDK (end side) and cloud side at the same time. The end side can be detected in real time free of charge, and the cloud side recognition type and accuracy are higher. In this actual combat, we use the capabilities provided by the cloud side.

Text recognition features Specification (HMS 4.0)
End side Support China, Japan and South Korea
Cloud side multilingual There are 19 languages including Chinese, English, French, Western and Thai
Tilt recognition It can still be recognized at 30 degrees tilt
Curved text support It can be recognized successfully even if 45 degree bending is supported
Text tracking End side support tracking

The above specifications are for reference only, which shall be subject to the official website of Huawei developer alliance

translate

   send the identified text to Huawei HMS mlkit translation service for text translation, and then you can get the result after the translation.
   translation is a service provided by the cloud side.

The characteristics of text translation Specification (HMS 4.0)
Multilingual Seven languages: Chinese, English, French, Spanish, Turkish, Arabic and Thai
time delay 300ms / 100 words
Bleu value >30
Dynamic term configuration support

The above specifications are for reference only, which shall be subject to the official website of Huawei developer alliance


Photo translation app development

There is too much nonsense above. Let’s get to the point

1 development preparation

Due to the use of cloud side services, it is necessary to register developer accounts with Huawei’s developer alliance and open these services in the cloud. I will not go into details here. You can directly follow the official appgallery connect configuration and service opening operation steps

For registered developers, please refer to:

1.1 add Huawei Maven warehouse to project level gradle

   open Android studio project level build.gradle Documents.
How to develop an Android translation program
   incrementally add the following Maven address:

buildscript {
    repositories {        
        maven {url 'http://developer.huawei.com/repo/'}
    }    }allprojects {
    repositories {       
        maven { url 'http://developer.huawei.com/repo/'}
    }
   }

1.2 at the application level build.gradle Add SDK dependency

How to develop an Android translation program
   integrated SDK. (as the cloud side capability is used, only the SDK basic package can be introduced.)

dependencies{  
    implementation 'com.huawei.hms:ml-computer-vision:1.0.2.300'
    implementation 'com.huawei.hms:ml-computer-translate:1.0.2.300'
    }

1.3 in AndroidManifest.xml File to apply for camera and storage rights

To enable the application to automatically update the latest machine learning model to the user’s device after the user installs your application from the Huawei application market, add the following statement to the AndroidManifest.xml In the document:

<manifest    
   <application  
       <meta-data                     
           android:name="com.huawei.hms.ml.DEPENDENCY"          
           android:value= "imgseg "/>                    
   </application>
</manifest> 

1.4 in AndroidManifest.xml File to apply for camera and storage rights

<uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-feature android:name="android.hardware.camera" /><uses-feature android:name="android.hardware.camera.autofocus" />

2 key steps of code development

2.1 dynamic permission application

private static final int CAMERA_PERMISSION_CODE = 1; @Override 
public void onCreate(Bundle savedInstanceState) { 
    // Checking camera permission   
   if (!allPermissionsGranted()) {
       getRuntimePermissions();
   }} 

2.2 create cloud side text analyzer. You can create a text parser through the text detection configurator “mlremotetextsetting”

MLRemoteTextSetting setting = (new MLRemoteTextSetting.Factory()).
         setTextDensityScene(MLRemoteTextSetting.OCR_LOOSE_SCENE).create();this.textAnalyzer = MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer(setting);

2.3 adoption android.graphics.Bitmap Create an “mlframe” object for the parser to detect images

MLFrame mlFrame = new MLFrame.Creator().setBitmap(this.originBitmap).create();

2.4 call “asyncanalyseframe” method for text detection

Task<MLText> task = this.textAnalyzer.asyncAnalyseFrame(mlFrame);
            task.addOnSuccessListener(new OnSuccessListener<MLText>() {
                @Override                public void onSuccess(MLText mlText) {
                    // Transacting logic for segment success.
                    if (mlText != null) {
                        RemoteTranslateActivity.this.remoteDetectSuccess(mlText);
                    } else {
                        RemoteTranslateActivity.this.displayFailure();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override                public void onFailure(Exception e) {
                    // Transacting logic for segment failure.
                    RemoteTranslateActivity.this.displayFailure();
                    return;
                }
            });

2.5 create a text translator. You can create a translator through the text translator custom parameter class “mlremotetranslatesetting”

MLRemoteTranslateSetting.Factory factory = new MLRemoteTranslateSetting
                .Factory()
                // Set the target language code. The ISO 639-1 standard is used.
                .setTargetLangCode(this.dstLanguage);
        if (!this.srcLanguage.equals("AUTO")) {
            // Set the source language code. The ISO 639-1 standard is used.
            factory.setSourceLangCode(this.srcLanguage);
        }
        this.translator = MLTranslatorFactory.getInstance().getRemoteTranslator(factory.create());

2.6 call the “asyncanalyseframe” method to translate the content obtained by text recognition

final Task<String> task = translator.asyncTranslate(this.sourceText);
        task.addOnSuccessListener(new OnSuccessListener<String>() {
            @Override            public void onSuccess(String text) {
                if (text != null) {
                    RemoteTranslateActivity.this.remoteDisplaySuccess(text);
                } else {
                    RemoteTranslateActivity.this.displayFailure();
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override            public void onFailure(Exception e) {
                RemoteTranslateActivity.this.displayFailure();
            }
        });

2.7 release resources after translation

if (this.textAnalyzer != null) {
            try {
                this.textAnalyzer.close();
            } catch (IOException e) {
                SmartLog.e(RemoteTranslateActivity.TAG, "Stop analyzer failed: " + e.getMessage());
            }
        }
        if (this.translator != null) {
            this.translator.stop();
        }

3 source code

Old rules, simple demo source code has been uploaded to GitHub, GitHub source code address please stamp: https://github.com/HMS-MLKit/HUAWEI-HMS-MLKit-Sample (project directory: Photo translate), you can do the optimization based on the scene for reference.

4 demo effect

How to develop an Android translation program


Conclusion

    the app development demonstrated this time uses the two cloud side capabilities of Huawei HMS ml kit, namely, the ability of character recognition and translation. Huawei’s character recognition and translation can also help developers do many other interesting and powerful functions, such as:
[universal text recognition]
1. Character recognition of bus license plate
2. Text recognition in document reading scene
[card type text recognition]
1. Through character recognition, the card number of bank card can be identified, which is used for bank card binding and other scenarios
2. Of course, in addition to identifying bank cards, you can also identify all kinds of card numbers in your life, such as membership cards and discount cards
3. In addition, it can also realize the identification of ID card, Hong Kong and Macao pass, etc
[translation]
1. Translation of road signs
2. Document translation
3. Web page translation, such as identifying the language type of the website comment area and translating it into the language of the corresponding country;
4. Introduction and translation of overseas shopping
5. Translation of restaurant order menu

For more detailed development guide, please refer to the official website of Huawei developer alliance
Huawei developer alliance machine learning service development guide


Previous link: Android | teaches you how to develop a DIY app for ID photo

Source: https://developer.huawei.com/consumer/cn/forum/topicview?tid=0201209905778120045&fid=18

Original author: AI_ talking