This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
/
MainActivity.java
211 lines (182 loc) · 6.44 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package ibm.tf.hangul;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String LABEL_FILE = "2350-common-hangul.txt";
private static final String MODEL_FILE = "optimized_hangul_tensorflow.pb";
private HangulClassifier classifier;
private PaintView paintView;
private Button alt1, alt2, alt3, alt4;
private LinearLayout altLayout;
private EditText resultText;
private TextView translationText;
private String[] currentTopLabels;
/**
* This is called when the application is first initialized/started. Basic setup logic is
* performed here.
* @param savedInstanceState Bundle
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
paintView = (PaintView) findViewById(R.id.paintView);
TextView drawHereText = (TextView) findViewById(R.id.drawHere);
paintView.setDrawText(drawHereText);
Button clearButton = (Button) findViewById(R.id.buttonClear);
clearButton.setOnClickListener(this);
Button classifyButton = (Button) findViewById(R.id.buttonClassify);
classifyButton.setOnClickListener(this);
Button backspaceButton = (Button) findViewById(R.id.buttonBackspace);
backspaceButton.setOnClickListener(this);
Button spaceButton = (Button) findViewById(R.id.buttonSpace);
spaceButton.setOnClickListener(this);
Button submitButton = (Button) findViewById(R.id.buttonSubmit);
submitButton.setOnClickListener(this);
altLayout = (LinearLayout) findViewById(R.id.altLayout);
altLayout.setVisibility(View.INVISIBLE);
alt1 = (Button) findViewById(R.id.alt1);
alt1.setOnClickListener(this);
alt2 = (Button) findViewById(R.id.alt2);
alt2.setOnClickListener(this);
alt3 = (Button) findViewById(R.id.alt3);
alt3.setOnClickListener(this);
alt4 = (Button) findViewById(R.id.alt4);
alt4.setOnClickListener(this);
translationText = (TextView) findViewById(R.id.translationText);
resultText = (EditText) findViewById(R.id.editText);
loadModel();
}
/**
* This method is called when the user clicks a button in the view.
* @param view
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonClear:
clear();
break;
case R.id.buttonClassify:
classify();
paintView.reset();
paintView.invalidate();
break;
case R.id.buttonBackspace:
backspace();
altLayout.setVisibility(View.INVISIBLE);
paintView.reset();
paintView.invalidate();
break;
case R.id.buttonSpace:
space();
break;
case R.id.buttonSubmit:
altLayout.setVisibility(View.INVISIBLE);
translate();
break;
case R.id.alt1:
case R.id.alt2:
case R.id.alt3:
case R.id.alt4:
useAltLabel(Integer.parseInt(view.getTag().toString()));
break;
}
}
/**
* Delete the last character in the text input field.
*/
private void backspace() {
int len = resultText.getText().length();
if (len > 0) {
resultText.getText().delete(len - 1, len);
}
}
/**
* Add a space to the text input.
*/
private void space() {
resultText.append(" ");
}
/**
* Clear the text and drawing to return to the beginning state.
*/
private void clear() {
paintView.reset();
paintView.invalidate();
resultText.setText("");
translationText.setText("");
altLayout.setVisibility(View.INVISIBLE);
}
/**
* Perform the classification, updating UI elements with the results.
*/
private void classify() {
float pixels[] = paintView.getPixelData();
currentTopLabels = classifier.classify(pixels);
resultText.append(currentTopLabels[0]);
altLayout.setVisibility(View.VISIBLE);
alt1.setText(currentTopLabels[1]);
alt2.setText(currentTopLabels[2]);
alt3.setText(currentTopLabels[3]);
alt4.setText(currentTopLabels[4]);
}
/**
* Perform the translation using the current Korean text in the text input field.
*/
private void translate() {
String text = resultText.getText().toString();
if (text.isEmpty()) {
return;
}
HashMap<String, String> postData = new HashMap<>();
postData.put("text", text);
postData.put("source", "ko");
postData.put("target", "en");
String apikey = getResources().getString(R.string.apikey);
String url = getResources().getString(R.string.url);
HangulTranslator translator = new HangulTranslator(postData, translationText, apikey, url);
translator.execute();
}
/**
* This function will switch out the last classified character with the alternative given the
* index in the top labels array.
*/
private void useAltLabel(int index) {
backspace();
resultText.append(currentTopLabels[index]);
}
@Override
protected void onResume() {
paintView.onResume();
super.onResume();
}
@Override
protected void onPause() {
paintView.onPause();
super.onPause();
}
/**
* Load pre-trained model in memory.
*/
private void loadModel() {
new Thread(new Runnable() {
@Override
public void run() {
try {
classifier = HangulClassifier.create(getAssets(),
MODEL_FILE, LABEL_FILE, PaintView.FEED_DIMENSION,
"input", "keep_prob", "output");
} catch (final Exception e) {
throw new RuntimeException("Error loading pre-trained model.", e);
}
}
}).start();
}
}