diff --git a/UI/AI/README.md b/UI/AI/README.md new file mode 100644 index 0000000000000000000000000000000000000000..c636664f6765ea0215b723162498ce0aac214b40 --- /dev/null +++ b/UI/AI/README.md @@ -0,0 +1,14 @@ +# Animation + +- Animation is one of the basic features of UI components. A well-designed animation not only polishes the look and feel of your application, but also brings fluent and natural interactive experience. + + **FrameAnimationElement** creates a frame-by-frame animation by displaying a series of still images in sequence, so that human eyes will perceive the images as moving ones. + + The value of **AnimatorValue** changes from 0 to 1, indicating changes of animation properties \(such as duration, change curve, and repetition times\) instead of the **Component** itself. These properties affect the component attributes, thus implementing the animation effect of the component. The following describes how to use **AnimatorValue** to implement an animation. + + **AnimatorProperty** allows you to set one or more properties for a **Component** to implement the animation effect. + +- Licensing + +For details, see the LICENSE. + diff --git a/UI/AI/README_zh.md b/UI/AI/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..58b5d0a58f913dc33b72ff0b8fcb513b026c5a89 --- /dev/null +++ b/UI/AI/README_zh.md @@ -0,0 +1,15 @@ +# 动画 + +- 动画是组件的基础特性之一,精心设计的动画使 UI 变化更直观,有助于改进应用程序的外观并改善用户体验。 + + 1、帧动画是利用视觉暂留现象,将一系列静止的图片按序播放,给用户产生动画的效果。 + + 2、AnimatorValue 数值从0到1变化,本身与 Component 无关。开发者可以设置0到1变化过程的属性,例如:时长、变化曲线、重复次数等,并通过值的变化改变组件的属性,实现组件的动画效果。 + + 3、AnimatorProperty 可以为 Component 设置某个属性或多个属性而实现动画效果。 + +- Licensing + + 请查阅 LICENSE。 + + diff --git a/UI/AI/build.gradle b/UI/AI/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..cd9448d8b8e119768a9caee9a2530cab40c11449 --- /dev/null +++ b/UI/AI/build.gradle @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } +} +buildscript { + repositories { + maven { + url 'https://mirrors.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.4.2' + } +} +allprojects { + repositories { + maven { + url 'https://mirrors.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + jcenter() + } +} \ No newline at end of file diff --git a/UI/AI/entry/build.gradle b/UI/AI/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..656369fc78e70ed49e04ddc78669669135d51545 --- /dev/null +++ b/UI/AI/entry/build.gradle @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +apply plugin: 'com.huawei.ohos.hap' +ohos { + compileSdkVersion 5 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) +} diff --git a/UI/AI/entry/src/main/config.json b/UI/AI/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..7e45d9ae3d6d01203e301c0032453d249dd8b1a7 --- /dev/null +++ b/UI/AI/entry/src/main/config.json @@ -0,0 +1,72 @@ +{ + "app": { + "bundleName": "ohos.samples.ai", + "version": { + "code": 1000000, + "name": "1.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.ai", + "name": ".MainAbility", + "reqCapabilities": [ + "video_support" + ], + "deviceType": [ + "default" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree":false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "portrait", + "visible": true, + "formsEnabled": false, + "name": "ohos.samples.ai.MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:app_name", + "type": "page", + "launchType": "standard" + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.MICROPHONE" + }, + { + "name": "ohos.permission.READ_CALENDAR" + }, + { + "name": "ohos.permission.INTERNET" + }, + { + "name": "ohos.permission.ACCESS_NETWORK_STATE" + }, + { + "name": "ohos.permission.READ_USER_STORAGE" + }, + { + "name": "ohos.permission.WRITE_USER_STORAGE" + }, + { + "name": "ohos.permission.MANAGE_USER_STORAGE" + } + ] + } +} \ No newline at end of file diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/MainAbility.java b/UI/AI/entry/src/main/java/ohos/samples/ai/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..ab3eb8de4179165be656af1bb79f89b7d313b0c5 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/MainAbility.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai; + +import ohos.samples.ai.slice.AiMainSlice; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; + +/** + * MainAbility + */ +public class MainAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(AiMainSlice.class.getName()); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/bean/AsrBean.java b/UI/AI/entry/src/main/java/ohos/samples/ai/bean/AsrBean.java new file mode 100644 index 0000000000000000000000000000000000000000..42d3cb16dc41900cc1efed3c89bf230398e5edee --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/bean/AsrBean.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.bean; + +import java.util.List; + +/** + * AsrBean + */ + +public class AsrBean { + private List result; + + private String resultType; + + public void setResult(List result) { + this.result = result; + } + + public List getResult() { + return result; + } + + public void setResultType(String resultType) { + this.resultType = resultType; + } + + public String getResultType() { + return resultType; + } + + /** + * Speech recognition result + */ + public class Result { + private double confidence; + + private String ori_word; + + private String pinyin; + + private String word; + + public void setConfidence(double confidence) { + this.confidence = confidence; + } + + public double getConfidence() { + return confidence; + } + + public void setOriWord(String ori_word) { + this.ori_word = ori_word; + } + + public String getOriWord() { + return ori_word; + } + + public void setPinyin(String pinyin) { + this.pinyin = pinyin; + } + + public String getPinyin() { + return pinyin; + } + + public void setWord(String word) { + this.word = word; + } + + public String getWord() { + return word; + } + } +} + + diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/AiMainSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/AiMainSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..e21bdc7aa759415efb5868cd67553ca5fe59b04e --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/AiMainSlice.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.ComponentContainer; + +/** + * AiMainSlice + */ +public class AiMainSlice extends AbilitySlice { + private AbilitySlice[] sliceArray = { + new TextRecognitionSlice(), new ImageSuperResolutionSlice(), new DocumentCorrectionSlice(), + new QRCodeGenerationSlice(), new TextImageSuperResolutionSlice(), new WordSegmentationSlice(), + new PartOfSpeechTaggingSlice(), new AssistantIntentionRecognitionSlice(), new IMIntentRecognitionSlice(), + new KeywordExtractionSlice(), new EntityIdentificationSlice(), new SpeechRecognitionSlice(), + new TextToSpeechSlice() + }; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_main_slice); + } + + @Override + protected void onActive() { + super.onActive(); + ComponentContainer container = (ComponentContainer) findComponentById(ResourceTable.Id_rootLayout); + for (int i = 0; i < container.getChildCount(); i++) { + int index = i; + container.getComponentAt(i).setClickedListener(component -> present(sliceArray[index], new Intent())); + } + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/AssistantIntentionRecognitionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/AssistantIntentionRecognitionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..b04870a8c459f8a1cf2f8d5da571121b2a31bfba --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/AssistantIntentionRecognitionSlice.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.nlu.NluClient; +import ohos.ai.nlu.NluRequestType; +import ohos.ai.nlu.ResponseResult; +import ohos.utils.zson.ZSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Assistant Intention Recognition Slice + */ +public class AssistantIntentionRecognitionSlice extends BaseSlice { + private TextField inputText; + + private TextField outText; + + private boolean initEngineResult; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_word_segmentation_slice, null, false); + rootLayout.addComponent(currComponent); + + initEngine(); + initComponents(); + } + + private void initEngine() { + NluClient.getInstance().init(this, result -> initEngineResult = true, true); + } + + private void initComponents() { + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + inputText.setText("Turn off bluetooth."); + } + + private void start(Component component) { + Map map = new HashMap<>(); + map.put("text", inputText.getText()); + map.put("type", 0); + String requestJson = ZSONObject.toZSONString(map); + if (initEngineResult) { + ResponseResult responseResult = NluClient.getInstance() + .getAssistantIntention(requestJson, NluRequestType.REQUEST_TYPE_LOCAL); + if (responseResult != null) { + String result = responseResult.getResponseResult(); + outText.setText("Result:" + System.lineSeparator() + result); + } + } + } + + @Override + public void setTitle() { + aiTitle.setText("Assistant Intention Recognition"); + } + + @Override + protected void onStop() { + super.onStop(); + NluClient.getInstance().destroy(this); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/BaseSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/BaseSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..799bf0ec39e2ab1eab1ed02cce721bb116875777 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/BaseSlice.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.aafwk.ability.AbilitySlice; + +import ohos.samples.ai.ResourceTable; + +import ohos.aafwk.content.Intent; +import ohos.agp.components.Component; +import ohos.agp.components.DirectionalLayout; +import ohos.agp.components.Text; +import ohos.agp.window.dialog.ToastDialog; + +/** + * Base Slice + */ +public abstract class BaseSlice extends AbilitySlice { + /** + * common baseSlice root layout + */ + public DirectionalLayout rootLayout; + + /** + * current slice layout + */ + public Component currComponent; + + /** + * slice title + */ + public Text aiTitle; + + /** + * slice desc + */ + public Text aiDesc; + + @Override + protected void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_base_layout); + + initCommons(); + initLayout(); + setTitle(); + } + + private void initCommons() { + rootLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_root_layout); + aiTitle = (Text) findComponentById(ResourceTable.Id_title); + aiDesc = (Text) findComponentById(ResourceTable.Id_desc); + Component back = findComponentById(ResourceTable.Id_back); + back.setClickedListener(component -> terminate()); + } + + /** + * common toast method + * + * @param msg toast content + */ + public void showTips(String msg) { + new ToastDialog(this).setContentText(msg).show(); + } + + /** + * init slice layout + */ + public abstract void initLayout(); + + /** + * set slice title and desc + */ + public abstract void setTitle(); + + @Override + protected void onStop() { + super.onStop(); + if (currComponent != null) { + rootLayout.removeComponent(currComponent); + } + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/DocumentCorrectionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/DocumentCorrectionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..4fee10aca366ca6e9d417416b1c244ed7c69f873 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/DocumentCorrectionSlice.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.utils.LogUtil; +import ohos.samples.ai.utils.WidgetHelper; + +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.ai.cv.common.ConnectionCallback; +import ohos.ai.cv.common.ImageResult; +import ohos.ai.cv.common.VisionConfiguration; +import ohos.ai.cv.common.VisionImage; +import ohos.ai.cv.common.VisionManager; +import ohos.ai.cv.docrefine.DocCoordinates; +import ohos.ai.cv.docrefine.DocRefineConfiguration; +import ohos.ai.cv.docrefine.IDocRefine; +import ohos.media.image.PixelMap; + +/** + * Document Correction Slice + */ +public class DocumentCorrectionSlice extends BaseSlice { + private static final String IMAGE_PATH = "entry/resources/rawfile/doc_check.png"; + + private Image originalImage; + + private Image outImage; + + private IDocRefine docRefine; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_document_correction_slice, null, false); + rootLayout.addComponent(currComponent); + initComponents(); + initEngine(); + } + + private void initComponents() { + originalImage = (Image) findComponentById(ResourceTable.Id_image); + outImage = (Image) findComponentById(ResourceTable.Id_out_image); + Component startButton = findComponentById(ResourceTable.Id_start); + startButton.setClickedListener(this::doSuperResolution); + originalImage.setPixelMap(WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH)); + } + + private void initEngine() { + int result = VisionManager.init(this, connectionCallback); + LogUtil.info(getLocalClassName(), "VisionManager.init result code : " + result); + } + + private void doSuperResolution(Component component) { + docRefine = VisionManager.getDocRefine(this); + PixelMap pixelMap = WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH); + + VisionImage image = VisionImage.fromPixelMap(pixelMap); + DocRefineConfiguration.Builder builder = new DocRefineConfiguration.Builder(); + builder.setProcessMode(VisionConfiguration.MODE_IN); + DocRefineConfiguration configuration = builder.build(); + docRefine.setVisionConfiguration(configuration); + ImageResult imageResult = new ImageResult(); + + DocCoordinates docCoordinates = new DocCoordinates(); + docRefine.docDetect(image, docCoordinates, null); + int result = docRefine.docRefine(image, docCoordinates, imageResult, null); + showTips("DocDetect : " + (result == 0 ? "Succeeded " : "Failed code = " + result)); + outImage.setPixelMap(imageResult.getPixelMap()); + } + + @Override + public void setTitle() { + aiTitle.setText("Document Correction"); + } + + private ConnectionCallback connectionCallback = new ConnectionCallback() { + @Override + public void onServiceConnect() { + LogUtil.info(getLocalClassName(), "Service connect succeeded"); + } + + @Override + public void onServiceDisconnect() { + LogUtil.info(getLocalClassName(), "Service disconnect succeeded"); + } + }; + + @Override + protected void onStop() { + super.onStop(); + if (docRefine != null) { + docRefine.release(); + } + VisionManager.destroy(); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/EntityIdentificationSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/EntityIdentificationSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..db57b42d5f722fd5514499f1383f8e4e50a19ed3 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/EntityIdentificationSlice.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.nlu.NluClient; +import ohos.ai.nlu.NluRequestType; +import ohos.ai.nlu.ResponseResult; +import ohos.utils.zson.ZSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Entity Identification Slice + */ +public class EntityIdentificationSlice extends BaseSlice { + private TextField inputText; + + private TextField outText; + + private boolean initEngineResult; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_word_segmentation_slice, null, false); + rootLayout.addComponent(currComponent); + initEngine(); + initComponents(); + } + + private void initEngine() { + NluClient.getInstance().init(this, result -> initEngineResult = true, true); + } + + private void initComponents() { + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + inputText.setText("I like the beach."); + } + + private void start(Component component) { + Map map = new HashMap<>(); + map.put("text", inputText.getText()); + String requestJson = ZSONObject.toZSONString(map); + if (initEngineResult) { + ResponseResult responseResult = NluClient.getInstance() + .getEntity(requestJson, NluRequestType.REQUEST_TYPE_LOCAL); + if (responseResult != null) { + String result = responseResult.getResponseResult(); + outText.setText("Result:" + System.lineSeparator() + result); + } + } + } + + @Override + public void setTitle() { + aiTitle.setText("Entity Identification"); + } + + @Override + protected void onStop() { + super.onStop(); + NluClient.getInstance().destroy(this); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/IMIntentRecognitionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/IMIntentRecognitionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..e70b2b78806d41e467998dae6cfebd86ac18257d --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/IMIntentRecognitionSlice.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.nlu.NluClient; +import ohos.ai.nlu.NluRequestType; +import ohos.ai.nlu.ResponseResult; +import ohos.utils.zson.ZSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * IM Intent Recognition Slice + */ +public class IMIntentRecognitionSlice extends BaseSlice { + private TextField inputText; + + private Component startParse; + + private TextField outText; + + private boolean initEngineResult; + + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_word_segmentation_slice, null, false); + rootLayout.addComponent(currComponent); + + initIMEngine(); + initComponents(); + } + + private void initIMEngine() { + NluClient.getInstance().init(this, result -> initEngineResult = true, true); + } + + private void initComponents() { + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + inputText.setText("Your personal credit card bill this month is $198.00, due date December 30."); + } + + private void start(Component component) { + Map map = new HashMap<>(); + map.put("text", inputText.getText()); + String requestJson = ZSONObject.toZSONString(map); + if (initEngineResult) { + ResponseResult responseResult = NluClient.getInstance() + .getChatIntention(requestJson, NluRequestType.REQUEST_TYPE_LOCAL); + if (responseResult != null) { + String result = responseResult.getResponseResult(); + outText.setText("Result:" + System.lineSeparator() + result); + } + } + } + + @Override + public void setTitle() { + aiTitle.setText("IM Intent Recognition"); + } + + @Override + protected void onStop() { + super.onStop(); + NluClient.getInstance().destroy(this); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/ImageSuperResolutionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/ImageSuperResolutionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..da123a42a7679aa07ca73ff9f2d1aeff00ac4717 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/ImageSuperResolutionSlice.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.utils.LogUtil; +import ohos.samples.ai.utils.WidgetHelper; + +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.ai.cv.common.ConnectionCallback; +import ohos.ai.cv.common.ImageResult; +import ohos.ai.cv.common.VisionConfiguration; +import ohos.ai.cv.common.VisionImage; +import ohos.ai.cv.common.VisionManager; +import ohos.ai.cv.sr.IImageSuperResolution; +import ohos.ai.cv.sr.SisrConfiguration; +import ohos.media.image.PixelMap; + +/** + * Image Super Resolution Slice + */ +public class ImageSuperResolutionSlice extends BaseSlice { + private static final String IMAGE_PATH = "entry/resources/rawfile/super_image.png"; + + private Image originalImage; + + private Image outImage; + + private IImageSuperResolution imageSuperResolution; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_image_super_resolution_slice, null, false); + rootLayout.addComponent(currComponent); + initComponents(); + initEngine(); + } + + private void initComponents() { + originalImage = (Image) findComponentById(ResourceTable.Id_image); + outImage = (Image) findComponentById(ResourceTable.Id_out_image); + Component startButton = findComponentById(ResourceTable.Id_start); + startButton.setClickedListener(this::doSuperResolution); + originalImage.setPixelMap(WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH)); + } + + private void initEngine() { + int result = VisionManager.init(this, connectionCallback); + LogUtil.info(getLocalClassName(), "VisionManager.init result code : " + result); + } + + private void doSuperResolution(Component component) { + imageSuperResolution = VisionManager.getSisr(this); + SisrConfiguration.Builder builder = new SisrConfiguration.Builder(); + builder.setProcessMode(VisionConfiguration.MODE_IN); + SisrConfiguration config = builder.build(); + config.setScale(SisrConfiguration.SISR_SCALE_3X); + config.setQuality(SisrConfiguration.SISR_QUALITY_HIGH); + imageSuperResolution.setVisionConfiguration(config); + + PixelMap pixelMap = WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH); + VisionImage image = VisionImage.fromPixelMap(pixelMap); + ImageResult imageResult = new ImageResult(); + int result = imageSuperResolution.doSuperResolution(image, imageResult, null); + showTips("DoSuperResolution : " + (result == 0 ? "succeeded " : "failed code = " + result)); + outImage.setPixelMap(imageResult.getPixelMap()); + } + + @Override + public void setTitle() { + aiTitle.setText("Image Super Resolution"); + } + + private ConnectionCallback connectionCallback = new ConnectionCallback() { + @Override + public void onServiceConnect() { + LogUtil.info(getLocalClassName(), "Service connect succeeded"); + } + + @Override + public void onServiceDisconnect() { + LogUtil.info(getLocalClassName(), "Service disconnect succeeded"); + } + }; + + @Override + protected void onStop() { + super.onStop(); + if (imageSuperResolution != null) { + imageSuperResolution.release(); + } + VisionManager.destroy(); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/KeywordExtractionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/KeywordExtractionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..ac2b1388cec692d839cc700166813fa0b0514a43 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/KeywordExtractionSlice.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.nlu.NluClient; +import ohos.ai.nlu.NluRequestType; +import ohos.ai.nlu.ResponseResult; +import ohos.utils.zson.ZSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Keyword Extraction Slice + */ +public class KeywordExtractionSlice extends BaseSlice { + private TextField inputText; + + private TextField outText; + + private boolean initEngineResult; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_word_segmentation_slice, null, false); + rootLayout.addComponent(currComponent); + + initEngine(); + initComponents(); + } + + private void initEngine() { + NluClient.getInstance().init(this, result -> initEngineResult = true, true); + } + + private void initComponents() { + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + inputText.setText("Let's go to class together today."); + } + + private void start(Component component) { + Map map = new HashMap<>(); + map.put("number", "2"); + map.put("body", inputText.getText()); + String requestJson = ZSONObject.toZSONString(map); + if (initEngineResult) { + ResponseResult responseResult = NluClient.getInstance() + .getKeywords(requestJson, NluRequestType.REQUEST_TYPE_LOCAL); + if (responseResult != null) { + String result = responseResult.getResponseResult(); + outText.setText("Result:" + System.lineSeparator() + result); + } + } + } + + @Override + public void setTitle() { + aiTitle.setText("Keyword Extraction"); + } + + @Override + protected void onStop() { + super.onStop(); + NluClient.getInstance().destroy(this); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/PartOfSpeechTaggingSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/PartOfSpeechTaggingSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..05b1fead1152d18bd5855b5750316e0c93acbf73 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/PartOfSpeechTaggingSlice.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.nlu.NluClient; +import ohos.ai.nlu.NluRequestType; +import ohos.ai.nlu.ResponseResult; +import ohos.utils.zson.ZSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Part Of Speech Tagging Slice + */ +public class PartOfSpeechTaggingSlice extends BaseSlice { + private TextField inputText; + + private TextField outText; + + private boolean initEngineResult; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_word_segmentation_slice, null, false); + rootLayout.addComponent(currComponent); + + initEngine(); + initComponents(); + } + + private void initEngine() { + NluClient.getInstance().init(this, result -> initEngineResult = true, true); + } + + private void initComponents() { + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + inputText.setText("I want to see a movie about speed and passion."); + } + + private void start(Component component) { + Map map = new HashMap<>(); + map.put("text", inputText.getText()); + map.put("type", 0); + String requestJson = ZSONObject.toZSONString(map); + if (initEngineResult) { + ResponseResult responseResult = NluClient.getInstance() + .getWordPos(requestJson, NluRequestType.REQUEST_TYPE_LOCAL); + if (responseResult != null) { + String result = responseResult.getResponseResult(); + outText.setText("Result:" + System.lineSeparator() + result); + } + } + } + + @Override + public void setTitle() { + aiTitle.setText("Part of Speech Tagging"); + } + + @Override + protected void onStop() { + super.onStop(); + NluClient.getInstance().destroy(this); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/QRCodeGenerationSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/QRCodeGenerationSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..7c386b27821fdb6b5de0981587f0e212eabf8593 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/QRCodeGenerationSlice.java @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.utils.LogUtil; + +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.ai.cv.common.ConnectionCallback; +import ohos.ai.cv.common.VisionManager; +import ohos.ai.cv.qrcode.IBarcodeDetector; +import ohos.media.image.ImageSource; +import ohos.media.image.PixelMap; + +/** + * QR Code Generation Slice + */ +public class QRCodeGenerationSlice extends BaseSlice { + private Image outImage; + + private IBarcodeDetector barcodeDetector; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_qr_code_generation_slice, null, false); + rootLayout.addComponent(currComponent); + initEngine(); + initComponents(); + } + + private void initEngine() { + VisionManager.init(this, connectionCallback); + } + + private void initComponents() { + outImage = (Image) findComponentById(ResourceTable.Id_out_image); + Component startButton = findComponentById(ResourceTable.Id_start); + startButton.setClickedListener(this::start); + } + + private void start(Component component) { + barcodeDetector = VisionManager.getBarcodeDetector(this); + int length = 600; + byte[] byteArray = new byte[length * length * 4]; + int result = barcodeDetector.detect("This is a TestCase of IBarcodeDetector", byteArray, length, length); + showTips("Detect : " + (result == 0 ? "Succeeded " : "Failed code = " + result)); + if (result == 0) { + ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions(); + srcOpts.formatHint = "image/png"; + ImageSource imageSource = ImageSource.create(byteArray, srcOpts); + PixelMap pixelMap = imageSource.createPixelmap(null); + outImage.setPixelMap(pixelMap); + } + } + + @Override + public void setTitle() { + aiTitle.setText("QRCode Generation"); + } + + private ConnectionCallback connectionCallback = new ConnectionCallback() { + @Override + public void onServiceConnect() { + LogUtil.info(getLocalClassName(), "Service connect succeeded"); + } + + @Override + public void onServiceDisconnect() { + LogUtil.info(getLocalClassName(), "Service disconnect succeeded"); + } + }; + + @Override + protected void onStop() { + super.onStop(); + if (barcodeDetector != null) { + barcodeDetector.release(); + } + VisionManager.destroy(); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/SpeechRecognitionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/SpeechRecognitionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..50590fdcc3e4f9a27ce6bb1e60e1d6cece6213c7 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/SpeechRecognitionSlice.java @@ -0,0 +1,281 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.bean.AsrBean; +import ohos.samples.ai.utils.LogUtil; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.asr.AsrClient; +import ohos.ai.asr.AsrIntent; +import ohos.ai.asr.AsrListener; +import ohos.ai.asr.util.AsrResultKey; +import ohos.eventhandler.EventHandler; +import ohos.eventhandler.EventRunner; +import ohos.eventhandler.InnerEvent; +import ohos.global.resource.RawFileEntry; +import ohos.global.resource.Resource; +import ohos.utils.PacMap; +import ohos.utils.zson.ZSONException; +import ohos.utils.zson.ZSONObject; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * Speech Recognition Slice + */ + +public class SpeechRecognitionSlice extends BaseSlice { + private static final String TAG = SpeechRecognitionSlice.class.getSimpleName(); + + private static final int EVENT_MSG_INIT = 0x1000001; + + private static final int EVENT_MSG_PARSE_START = 0x1000002; + + private static final int EVENT_MSG_PARSE_END = 0x1000003; + + private static final int EVENT_MSG_ERROR = 0x1000004; + + private static final int VALID_LENGTH = 1280; + + private static final String RAW_AUDIO_WAV = "entry/resources/rawfile/asr_test.wav"; + + private static final String RAW_AUDIO_PCM = "entry/resources/rawfile/asr_date_conversion.pcm"; + + private AsrClient asrClient; + + private TextField outText; + + private String result; + + private AsrIntent asrIntent; + + private String wavCachePath; + + private String pcmCachePath; + + private EventHandler handler = new EventHandler(EventRunner.current()) { + @Override + protected void processEvent(InnerEvent event) { + switch (event.eventId) { + case EVENT_MSG_INIT: + showTips("InitAIEngine Success"); + break; + case EVENT_MSG_PARSE_START: + outText.setText("Doing now, please wait a moment ..."); + break; + case EVENT_MSG_PARSE_END: + parseJson(); + break; + case EVENT_MSG_ERROR: + showTips(result); + break; + } + } + }; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_speech_recognition_slice, null, false); + rootLayout.addComponent(currComponent); + initData(); + initComponents(); + initAIEngine(); + } + + private void initData() { + wavCachePath = new File(getFilesDir(), "asr_test.wav").getPath(); + pcmCachePath = new File(getFilesDir(), "asr_date_conversion.pcm").getPath(); + writeToDisk(RAW_AUDIO_WAV, wavCachePath); + writeToDisk(RAW_AUDIO_PCM, pcmCachePath); + } + + private void initComponents() { + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + } + + private void start(Component component) { + showTips("start parse ..."); + asrClient.startListening(new AsrIntent()); + File file = new File(wavCachePath); + if (!file.exists()) { + return; + } + int initialSize = new Long(file.length()).intValue(); + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(initialSize); + BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) { + byte[] buffer = new byte[VALID_LENGTH]; + int len; + while (true) { + len = in.read(buffer, 0, VALID_LENGTH); + if (len == -1) { + break; + } + bos.reset(); + bos.write(buffer, 0, len); + asrClient.writePcm(bos.toByteArray(), VALID_LENGTH); + } + } catch (IOException e) { + LogUtil.error(TAG, "startParse : IOException "); + } + } + + private void initAIEngine() { + asrClient = AsrClient.createAsrClient(this).orElse(null); + if (asrClient != null) { + asrIntent = new AsrIntent(); + useDefaultAsrIntent(asrIntent); + asrIntent.setAudioSourceType(AsrIntent.AsrAudioSrcType.ASR_SRC_TYPE_PCM); + asrClient.init(asrIntent, asrListener); + } + } + + private void useDefaultAsrIntent(AsrIntent asrIntent) { + asrIntent.setVadEndWaitMs(2000); + asrIntent.setVadFrontWaitMs(4800); + asrIntent.setTimeoutThresholdMs(20000); + } + + @Override + public void setTitle() { + aiTitle.setText("Speech Recognition"); + } + + @Override + protected void onStop() { + super.onStop(); + release(); + } + + private void parseJson() { + try { + AsrBean asrBean = ZSONObject.stringToClass(result, AsrBean.class); + if (asrBean != null && asrBean.getResult().size() > 0) { + AsrBean.Result resultContent = asrBean.getResult().get(0); + outText.setText("Result:" + System.lineSeparator() + resultContent.getWord()); + } + } catch (ZSONException e) { + LogUtil.error(TAG, "parseJson ZSONException"); + } + } + + private AsrListener asrListener = new AsrListener() { + @Override + public void onInit(PacMap pacMap) { + result = pacMap.getString(AsrResultKey.RESULTS_RECOGNITION); + handler.sendEvent(EVENT_MSG_INIT); + } + + @Override + public void onBeginningOfSpeech() { + LogUtil.info(TAG, "onBeginningOfSpeech"); + } + + @Override + public void onRmsChanged(float value) { + LogUtil.info(TAG, "onRmsChanged :" + value); + } + + @Override + public void onBufferReceived(byte[] bytes) { + LogUtil.info(TAG, "onRmsChanged :" + bytes.toString()); + } + + @Override + public void onEndOfSpeech() { + LogUtil.info(TAG, "onEndOfSpeech "); + } + + @Override + public void onError(int errorCode) { + result = "onError code :" + errorCode; + handler.sendEvent(EVENT_MSG_ERROR); + } + + @Override + public void onResults(PacMap pacMap) { + LogUtil.info(TAG, "onResults :" + result); + result = pacMap.getString(AsrResultKey.RESULTS_RECOGNITION); + handler.sendEvent(EVENT_MSG_PARSE_END); + } + + @Override + public void onIntermediateResults(PacMap pacMap) { + LogUtil.info(TAG, "onIntermediateResults :" + result); + result = pacMap.getString(AsrResultKey.RESULTS_RECOGNITION); + handler.sendEvent(EVENT_MSG_PARSE_END); + } + + @Override + public void onEnd() { + LogUtil.info(TAG, "onEnd"); + } + + @Override + public void onEvent(int i, PacMap pacMap) { + LogUtil.info(TAG, "onEvent"); + } + + @Override + public void onAudioStart() { + LogUtil.info(TAG, "onAudioStart"); + handler.sendEvent(EVENT_MSG_PARSE_START); + } + + @Override + public void onAudioEnd() { + LogUtil.info(TAG, "onAudioEnd"); + } + }; + + private void release() { + if (asrClient != null) { + asrClient.stopListening(); + asrClient.cancel(); + asrClient.destroy(); + } + } + + private void writeToDisk(String rawFilePathString, String targetFilePath) { + File file = new File(targetFilePath); + if (file.exists()) { + return; + } + RawFileEntry rawFileEntry = getResourceManager().getRawFileEntry(rawFilePathString); + try (FileOutputStream output = new FileOutputStream(new File(targetFilePath))) { + Resource resource = rawFileEntry.openRawFile(); + byte[] cache = new byte[1024]; + int len = resource.read(cache); + while (len != -1) { + output.write(cache, 0, len); + len = resource.read(cache); + } + } catch (IOException e) { + LogUtil.error(TAG, "writeToDisk IOException"); + } + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextImageSuperResolutionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextImageSuperResolutionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..c8e0ed5dd7966c28f3bf54a5c16b7d5490b3cf11 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextImageSuperResolutionSlice.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.utils.LogUtil; +import ohos.samples.ai.utils.WidgetHelper; + +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.ai.cv.common.ConnectionCallback; +import ohos.ai.cv.common.ImageResult; +import ohos.ai.cv.common.VisionConfiguration; +import ohos.ai.cv.common.VisionImage; +import ohos.ai.cv.common.VisionManager; +import ohos.ai.cv.sr.ITxtImageSuperResolution; +import ohos.ai.cv.sr.TxtImageSuperResolutionConfiguration; +import ohos.media.image.PixelMap; + +/** + * Text Image Super Resolution Slice + */ +public class TextImageSuperResolutionSlice extends BaseSlice { + private static final String IMAGE_PATH = "entry/resources/rawfile/super_text.png"; + + private Image originalImage; + + private Image outImage; + + private ITxtImageSuperResolution txtImageSuperResolution; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_text_image_superresolution_slice, null, false); + rootLayout.addComponent(currComponent); + initComponents(); + initHiAIEngine(); + } + + private void initComponents() { + originalImage = (Image) findComponentById(ResourceTable.Id_image); + outImage = (Image) findComponentById(ResourceTable.Id_out_image); + Component startButton = findComponentById(ResourceTable.Id_start); + startButton.setClickedListener(this::doSuperResolution); + originalImage.setPixelMap(WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH)); + } + + private void initHiAIEngine() { + int result = VisionManager.init(this, connectionCallback); + LogUtil.info(getLocalClassName(), "VisionManager.init result code : " + result); + } + + private void doSuperResolution(Component component) { + txtImageSuperResolution = VisionManager.getTisr(this); + PixelMap pixelMap = WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH); + + VisionImage image = VisionImage.fromPixelMap(pixelMap); + TxtImageSuperResolutionConfiguration.Builder builder = new TxtImageSuperResolutionConfiguration.Builder(); + builder.setProcessMode(VisionConfiguration.MODE_IN); + TxtImageSuperResolutionConfiguration paras = builder.build(); + txtImageSuperResolution.setVisionConfiguration(paras); + + ImageResult imageResult = new ImageResult(); + int result = txtImageSuperResolution.doSuperResolution(image, imageResult, null); + showTips("DoSuperResolution : " + (result == 0 ? "Succeeded " : "Failed code = " + result)); + outImage.setPixelMap(imageResult.getPixelMap()); + } + + @Override + public void setTitle() { + aiTitle.setText("Text Image Super Resolution"); + } + + private ConnectionCallback connectionCallback = new ConnectionCallback() { + @Override + public void onServiceConnect() { + LogUtil.info(getLocalClassName(), "Service connect succeeded"); + } + + @Override + public void onServiceDisconnect() { + LogUtil.info(getLocalClassName(), "Service disconnect succeeded"); + } + }; + + @Override + protected void onStop() { + super.onStop(); + if (txtImageSuperResolution != null) { + txtImageSuperResolution.release(); + } + VisionManager.destroy(); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextRecognitionSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextRecognitionSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..92abe5c86ece7ee27a27d0735095026470b99ffa --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextRecognitionSlice.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.utils.LogUtil; +import ohos.samples.ai.utils.WidgetHelper; + +import ohos.agp.components.Component; +import ohos.agp.components.Image; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.cv.common.ConnectionCallback; +import ohos.ai.cv.common.VisionConfiguration; +import ohos.ai.cv.common.VisionImage; +import ohos.ai.cv.common.VisionManager; +import ohos.ai.cv.text.ITextDetector; +import ohos.ai.cv.text.Text; +import ohos.ai.cv.text.TextConfiguration; +import ohos.ai.cv.text.TextDetectType; +import ohos.media.image.PixelMap; + +/** + * Text Recognition Slice + */ +public class TextRecognitionSlice extends BaseSlice { + private static final String IMAGE_PATH = "entry/resources/rawfile/text_recognition.png"; + + private Image originalImage; + + private TextField outText; + + private ITextDetector textDetector; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_text_recognition_slice, null, false); + rootLayout.addComponent(currComponent); + initComponents(); + initEngine(); + } + + private void initComponents() { + originalImage = (Image) findComponentById(ResourceTable.Id_image); + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + Component startButton = findComponentById(ResourceTable.Id_start); + startButton.setClickedListener(this::doSuperResolution); + originalImage.setPixelMap(WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH)); + } + + private void initEngine() { + int result = VisionManager.init(this, connectionCallback); + LogUtil.info(getLocalClassName(), "VisionManager.init result code : " + result); + } + + private void doSuperResolution(Component component) { + textDetector = VisionManager.getTextDetector(this); + TextConfiguration.Builder builder = new TextConfiguration.Builder(); + builder.setProcessMode(VisionConfiguration.MODE_IN); + builder.setLanguage(TextConfiguration.ENGLISH); + TextConfiguration config = builder.build(); + + textDetector.setVisionConfiguration(config); + PixelMap pixelMap = WidgetHelper.getPixelMapFromRaw(this, IMAGE_PATH); + VisionImage image = VisionImage.fromPixelMap(pixelMap); + Text text = new Text(); + int result = textDetector.detect(image, text, null); + showTips("Text Detect : " + (result == 0 ? "Succeeded " : "Failed code = " + result)); + this.outText.setText("Result:" + System.lineSeparator() + text.getValue()); + } + + @Override + public void setTitle() { + aiTitle.setText("Text Recognition"); + } + + private ConnectionCallback connectionCallback = new ConnectionCallback() { + @Override + public void onServiceConnect() { + LogUtil.info(getLocalClassName(), "Service connect succeeded"); + } + + @Override + public void onServiceDisconnect() { + LogUtil.info(getLocalClassName(), "Service disconnect succeeded"); + } + }; + + @Override + protected void onStop() { + super.onStop(); + if (textDetector != null) { + textDetector.release(); + } + VisionManager.destroy(); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextToSpeechSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextToSpeechSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..92b1337c6856366d8381a91855f08bfcd9398edf --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/TextToSpeechSlice.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; +import ohos.samples.ai.utils.LogUtil; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.tts.TtsClient; +import ohos.ai.tts.TtsListener; +import ohos.ai.tts.TtsParams; +import ohos.ai.tts.constants.TtsEvent; +import ohos.eventhandler.EventHandler; +import ohos.eventhandler.EventRunner; +import ohos.eventhandler.InnerEvent; +import ohos.utils.PacMap; + +import java.util.UUID; + +/** + * Text To Speech Slice + */ +public class TextToSpeechSlice extends BaseSlice { + private static final String TAG = SpeechRecognitionSlice.class.getSimpleName(); + + private static final int EVENT_MSG_INIT = 0x1000001; + + private boolean initItsResult; + + private TextField inputText; + + private EventHandler handler = new EventHandler(EventRunner.current()) { + @Override + protected void processEvent(InnerEvent event) { + initTTSEngine(); + } + }; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_text_to_speech_slice, null, false); + rootLayout.addComponent(currComponent); + + initTTSEngine(); + initComponents(); + } + + @Override + public void setTitle() { + aiTitle.setText("Text To Speech"); + } + + private void initComponents() { + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + Component startPlay = findComponentById(ResourceTable.Id_start_play); + startPlay.setClickedListener(this::startPlay); + } + + private void startPlay(Component component) { + if (initItsResult) { + TtsClient.getInstance().speakText(inputText.getText(), null); + } else { + showTips("InitTTSEngine Failed"); + } + } + + private void initTTSEngine() { + TtsClient.getInstance().create(this, ttsListener); + TtsParams ttsParams = new TtsParams(); + ttsParams.setDeviceId(UUID.randomUUID().toString()); + initItsResult = TtsClient.getInstance().init(ttsParams); + if (initItsResult) { + showTips("InitTTSEngine Succeeded"); + } else { + handler.sendEvent(EVENT_MSG_INIT, 1000); + } + } + + private TtsListener ttsListener = new TtsListener() { + @Override + public void onEvent(int eventType, PacMap pacMap) { + if (eventType == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) { + LogUtil.info(TAG, "TTS Client create succeeded"); + } + } + + @Override + public void onStart(String utteranceId) { + LogUtil.info(TAG, utteranceId + " audio synthesis begins"); + } + + @Override + public void onProgress(String utteranceId, byte[] audioData, int progress) { + LogUtil.info(TAG, utteranceId + " audio synthesis progress:" + progress); + } + + @Override + public void onFinish(String utteranceId) { + LogUtil.info(TAG, utteranceId + " audio synthesis completed"); + } + + @Override + public void onSpeechStart(String utteranceId) { + LogUtil.info(TAG, utteranceId + " begins to speech"); + } + + @Override + public void onSpeechProgressChanged(String utteranceId, int progress) { + LogUtil.info(TAG, utteranceId + " speech progress:" + progress); + } + + @Override + public void onSpeechFinish(String utteranceId) { + LogUtil.info(TAG, utteranceId + " speech completed"); + } + + @Override + public void onError(String utteranceId, String errorMessage) { + LogUtil.info(TAG, utteranceId + " errorMessage: " + errorMessage); + } + }; +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/slice/WordSegmentationSlice.java b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/WordSegmentationSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..1e6b4c3110d56077d6a7b6c96575d87ba7535d26 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/slice/WordSegmentationSlice.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.slice; + +import ohos.samples.ai.ResourceTable; + +import ohos.agp.components.Component; +import ohos.agp.components.LayoutScatter; +import ohos.agp.components.TextField; +import ohos.ai.nlu.NluClient; +import ohos.ai.nlu.NluRequestType; +import ohos.ai.nlu.ResponseResult; +import ohos.utils.zson.ZSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Word Segmentation Slice + */ +public class WordSegmentationSlice extends BaseSlice { + private TextField inputText; + + private TextField outText; + + private boolean initEngineResult; + + @Override + public void initLayout() { + currComponent = LayoutScatter.getInstance(this) + .parse(ResourceTable.Layout_word_segmentation_slice, null, false); + rootLayout.addComponent(currComponent); + + initEngine(); + initComponents(); + } + + private void initEngine() { + NluClient.getInstance().init(this, result -> initEngineResult = true, true); + } + + private void initComponents() { + outText = (TextField) findComponentById(ResourceTable.Id_out_text); + inputText = (TextField) findComponentById(ResourceTable.Id_input_text); + Component startButton = findComponentById(ResourceTable.Id_start_parse); + startButton.setClickedListener(this::start); + inputText.setText("It's a nice day. Let's go to the movies."); + } + + private void start(Component component) { + Map map = new HashMap<>(); + map.put("text", inputText.getText()); + map.put("type", 0); + String requestJson = ZSONObject.toZSONString(map); + if (initEngineResult) { + ResponseResult responseResult = NluClient.getInstance() + .getWordSegment(requestJson, NluRequestType.REQUEST_TYPE_LOCAL); + if (responseResult != null) { + String result = responseResult.getResponseResult(); + outText.setText("Result:" + System.lineSeparator() + result); + } + } + } + + @Override + public void setTitle() { + aiTitle.setText("Word Segmentation"); + } + + @Override + protected void onStop() { + super.onStop(); + NluClient.getInstance().destroy(this); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/utils/LogUtil.java b/UI/AI/entry/src/main/java/ohos/samples/ai/utils/LogUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..24b7c4727cc800469f5f39bfab51d04c515735bf --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/utils/LogUtil.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.utils; + +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; + +import java.util.Locale; + +/** + * LogUtil + */ +public class LogUtil { + private static final String TAG_LOG = "AI "; + + private static final int DOMAIN_ID = 0xD000F00; + + private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, LogUtil.TAG_LOG); + + private static final String LOG_FORMAT = "%s: %s"; + + private LogUtil() { + /* Do nothing */ + } + + /** + * Print info log + * + * @param tag log tag + * @param msg log message + */ + public static void info(String tag, String msg) { + HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg)); + } + + /** + * Print error log + * + * @param tag log tag + * @param msg log message + */ + public static void error(String tag, String msg) { + HiLog.error(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg)); + } +} diff --git a/UI/AI/entry/src/main/java/ohos/samples/ai/utils/WidgetHelper.java b/UI/AI/entry/src/main/java/ohos/samples/ai/utils/WidgetHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..b34e3e218a86776a7149d29efdec8ad115e539d6 --- /dev/null +++ b/UI/AI/entry/src/main/java/ohos/samples/ai/utils/WidgetHelper.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.ai.utils; + +import ohos.app.Context; +import ohos.global.resource.RawFileEntry; +import ohos.global.resource.Resource; +import ohos.media.image.ImageSource; +import ohos.media.image.PixelMap; + +import java.io.IOException; + +/** + * Widget helper + * + */ +public final class WidgetHelper { + /** + * Rawfile to PixelMap + * + * @param context app context + * @param path copy file's path + * @return PixelMap pixelMap + */ + public static PixelMap getPixelMapFromRaw(Context context, String path) { + PixelMap pixelMap = null; + try { + RawFileEntry rawFileEntry = context.getResourceManager().getRawFileEntry(path); + Resource resource = rawFileEntry.openRawFile(); + ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions()); + pixelMap = imageSource.createPixelmap(null); + } catch (IOException e) { + LogUtil.error("getPixelMapFromRaw", "IOException :" + e.toString()); + } + return pixelMap; + } +} diff --git a/UI/AI/entry/src/main/resources/base/element/string.json b/UI/AI/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..75bc3eb6a4628f54198f3294d613eb17136f1e0c --- /dev/null +++ b/UI/AI/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "AI" + }, + { + "name": "mainability_description", + "value": "hap sample empty page" + } + ] +} \ No newline at end of file diff --git a/UI/AI/entry/src/main/resources/base/graphic/button_bg.xml b/UI/AI/entry/src/main/resources/base/graphic/button_bg.xml new file mode 100644 index 0000000000000000000000000000000000000000..f4a98035344fc8f1a3211c66ec8098eb24fdaf66 --- /dev/null +++ b/UI/AI/entry/src/main/resources/base/graphic/button_bg.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/UI/AI/entry/src/main/resources/base/graphic/text_bg.xml b/UI/AI/entry/src/main/resources/base/graphic/text_bg.xml new file mode 100644 index 0000000000000000000000000000000000000000..b428e6b4f47cf21e596368de884fe52d98ba90a6 --- /dev/null +++ b/UI/AI/entry/src/main/resources/base/graphic/text_bg.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/UI/AI/entry/src/main/resources/base/graphic/textfield_bg.xml b/UI/AI/entry/src/main/resources/base/graphic/textfield_bg.xml new file mode 100644 index 0000000000000000000000000000000000000000..32ba5fbe5cf260dd4fac82f3d051098e6ddc8b38 --- /dev/null +++ b/UI/AI/entry/src/main/resources/base/graphic/textfield_bg.xml @@ -0,0 +1,25 @@ + + + + + + + \ No newline at end of file diff --git a/UI/AI/entry/src/main/resources/base/layout/base_layout.xml b/UI/AI/entry/src/main/resources/base/layout/base_layout.xml new file mode 100644 index 0000000000000000000000000000000000000000..d598502a7817c48c38cf77e1df10d25e897b7a64 --- /dev/null +++ b/UI/AI/entry/src/main/resources/base/layout/base_layout.xml @@ -0,0 +1,64 @@ + + + + + + +