diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/.gitignore b/MindSporeLiteKit/MindSporeLiteArkTSDemo/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..d2ff20141ceed86d87c0ea5d99481973005bab2b --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/.gitignore @@ -0,0 +1,12 @@ +/node_modules +/oh_modules +/local.properties +/.idea +**/build +/.hvigor +.cxx +/.clangd +/.clang-format +/.clang-tidy +**/.test +/.appanalyzer \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/app.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/app.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f57e7ce1e39517468a1bdffe188aa2c52e39ace4 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/app.json5 @@ -0,0 +1,25 @@ +/* +* Copyright (c) 2024 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. +*/ + +{ + "app": { + "bundleName": "com.samples.mindsporelitearktsdemo", + "vendor": "samples", + "versionCode": 1000000, + "versionName": "1.0.0", + "icon": "$media:layered_image", + "label": "$string:app_name" + } +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/element/string.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..d26dc6c578f005f74a3c1d3fc0fe88664577a541 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/element/string.json @@ -0,0 +1,8 @@ +{ + "string": [ + { + "name": "app_name", + "value": "MindSporeLiteArkTSDemo" + } + ] +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/app_icon.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/app_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..1090fe07135c34ccf8875f73595ad0ddad9f66e1 Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/app_icon.png differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/background.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/background.png differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/foreground.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..eb9427585b36d14b12477435b6419d1f07b3e0bb Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/foreground.png differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/layered_image.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/AppScope/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/README_zh.md b/MindSporeLiteKit/MindSporeLiteArkTSDemo/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..51539f53cb5863066dae23e9c1fc15fa55782f8b --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/README_zh.md @@ -0,0 +1,76 @@ +# **基于ArkTS接口的MindSpore Lite应用开发** + +### 介绍 + +本文基于MindSpore Lite提供的ArkTS API,实现“端侧图像分类”示例程序,来演示端侧部署的流程。 + +1. 选择图像分类模型。 +2. 将模型转换成MindSpore Lite模型格式。 +3. 在端侧使用MindSpore Lite推理模型,显示出可能的分类结果。 + +### 效果预览 + +| 主页 | +|-----------------------------------------------------| +| | + +#### 使用说明 + +1. 在主界面,可以点击photo按钮,进入相册选择图片界面; +2. 在相册界面,选择图片,点击确定按钮; +3. 自动进行选择图片的图像分类模型推理,部分推理结果显示在主界面。 + +### 工程目录 + +``` +entry +├── src/main +│   ├── etc# +|   |   └── pages +|   |       └── Index.ets // 首页,获取图片及预处理 +|   |       └── model.ets // 推理 +│   ├── resources // 资源文件 +|   |   └── rawfile +|   |       └── mobilenetv2.ms // 存放的模型文件 +``` + +### 具体实现 + +* 本示例程序中使用的终端图像分类模型文件为mobilenetv2.ms,放置在entry\src\main\resources\rawfile工程目录下。 + + 注:开发者可按需手工下载[MindSpore Model Zoo中图像分类模型](https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_openimage_lite/1.5/mobilenetv2.ms)。 + +* 调用[@ohos.file.picker](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-file-picker.md) (图片文件选择)、[@ohos.multimedia.image](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-image-kit/js-apis-image.md) (图片处理效果)、[@ohos.file.fs](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-file-fs.md) (基础文件操作) 等API实现相册图片获取及图片处理。完整代码请参见Index.ets + +* 调用[@ohos.ai.mindSporeLite](https://docs.openharmony.cn/pages/v4.1/zh-cn/application-dev/reference/apis-mindspore-lite-kit/js-apis-mindSporeLite.md) (推理能力) API实现端侧推理。完整代码请参见model.ets + +* 调用推理函数并处理结果。完整代码请参见Index.ets + +### 相关权限 + +ohos.permission.READ_IMAGEVIDEO + +### 依赖 + +无。 + +### 约束与限制 + +1.本示例仅支持标准系统上运行,支持设备:RK3568; + +2.本示例为Stage模型,已适配API version 11版本SDK; + +3.本示例需要使用DevEco Studio 4.1 Release及以上版本才可编译运行。 + +### 下载 + +如需单独下载本工程,执行如下命令: + +``` +git init +git config core.sparsecheckout true +echo code/BasicFeature/ApplicationModels/MindSporeLiteArkTSDemo/ > .git/info/sparse-checkout +git remote add origin https://gitee.com/openharmony/applications_app_samples.git +git pull origin master +``` + diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/build-profile.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c8d37de18b638b34dcd8738db696e230eaa5fd66 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/build-profile.json5 @@ -0,0 +1,57 @@ +/* +* Copyright (c) 2024 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. +*/ + +{ + "app": { + "signingConfigs": [], + "products": [ + { + "name": "default", + "signingConfig": "default", + "targetSdkVersion": "5.1.1(19)", + "compatibleSdkVersion": "5.1.1(19)", + "runtimeOS": "HarmonyOS", + "buildOption": { + "strictMode": { + "caseSensitiveCheck": true, + "useNormalizedOHMUrl": true + } + } + } + ], + "buildModeSet": [ + { + "name": "debug", + }, + { + "name": "release" + } + ] + }, + "modules": [ + { + "name": "entry", + "srcPath": "./entry", + "targets": [ + { + "name": "default", + "applyToProducts": [ + "default" + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/code-linter.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/code-linter.json5 new file mode 100644 index 0000000000000000000000000000000000000000..073990fa45394e1f8e85d85418ee60a8953f9b99 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/code-linter.json5 @@ -0,0 +1,32 @@ +{ + "files": [ + "**/*.ets" + ], + "ignore": [ + "**/src/ohosTest/**/*", + "**/src/test/**/*", + "**/src/mock/**/*", + "**/node_modules/**/*", + "**/oh_modules/**/*", + "**/build/**/*", + "**/.preview/**/*" + ], + "ruleSet": [ + "plugin:@performance/recommended", + "plugin:@typescript-eslint/recommended" + ], + "rules": { + "@security/no-unsafe-aes": "error", + "@security/no-unsafe-hash": "error", + "@security/no-unsafe-mac": "warn", + "@security/no-unsafe-dh": "error", + "@security/no-unsafe-dsa": "error", + "@security/no-unsafe-ecdsa": "error", + "@security/no-unsafe-rsa-encrypt": "error", + "@security/no-unsafe-rsa-sign": "error", + "@security/no-unsafe-rsa-key": "error", + "@security/no-unsafe-dsa-key": "error", + "@security/no-unsafe-dh-key": "error", + "@security/no-unsafe-3des": "error" + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/.gitignore b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e2713a2779c5a3e0eb879efe6115455592caeea5 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/.gitignore @@ -0,0 +1,6 @@ +/node_modules +/oh_modules +/.preview +/build +/.cxx +/.test \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/build-profile.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..03c65099d8dad33408589726927afae8e237a645 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/build-profile.json5 @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2024 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. +*/ + +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + } + } + } + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest", + } + ] +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/hvigorfile.ts b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..b0e3a1ab98a91bc918d6404b2413111a5011f14a --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/hvigorfile.ts @@ -0,0 +1,6 @@ +import { hapTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: hapTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins: [] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/obfuscation-rules.txt b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/oh-package.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..248c3b7541a589682a250f86a6d3ecf7414d2d6a --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "name": "entry", + "version": "1.0.0", + "description": "Please describe the basic information.", + "main": "", + "author": "", + "license": "", + "dependencies": {} +} + diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/entryability/EntryAbility.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..508880af8c33aa838016d1cd4b2c68be2f447540 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e4de99282050bad799ac892eb85ac5449364a51 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..db05e70737230bfacb6a7d61c55b5b9c49f1e696 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/Index.ets @@ -0,0 +1,318 @@ +/* +* Copyright (c) 2024-2025 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. +*/ + +import modelPredict from './model'; +import { photoAccessHelper } from '@kit.MediaLibraryKit'; +import { BusinessError } from '@ohos.base'; +import image from '@ohos.multimedia.image'; +import { resourceManager } from '@kit.LocalizationKit' +import { fileIo } from '@kit.CoreFileKit'; +import { abilityAccessCtrl, Permissions } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const PERMISSIONS: Permissions[] = ['ohos.permission.READ_IMAGEVIDEO']; + +@Entry +@Component +struct Index { + @State modelPredict: string = 'MindSporeLite ArkTS Demo'; + @State modelName: string = 'mobilenetv2.ms'; + @State modelInputHeight: number = 224; + @State modelInputWidth: number = 224; + @State uris: Array = []; + @State max: number = 0; + @State maxIndex: number = 0; + @State maxArray: Array = []; + @State maxIndexArray: Array = []; + @State labelsNameMmap: string[] = [ + 'Herd', 'Safari', 'Bangle', 'Cushion', 'Countertop', + 'Prom', 'Branch', 'Sports', 'Sky', 'Community', + 'Wheel', 'Cola', 'Tuxedo', 'Flowerpot', 'Team', + 'Computer', 'Unicycle', 'Brig', 'Aerospace engineering', 'Scuba diving', + 'Goggles', 'Fruit', 'Badminton', 'Horse', 'Sunglasses', + 'Fun', 'Prairie', 'Poster', 'Flag', 'Speedboat', + 'Eyelash', 'Veil', 'Mobile phone', 'Wheelbarrow', 'Saucer', + 'Leather', 'Drawer', 'Paper', 'Pier', 'Waterfowl', + 'Tights', 'Rickshaw', 'Vegetable', 'Handrail', 'Ice', + 'Metal', 'Flower', 'Wing', 'Silverware', 'Event', + 'Skyline', 'Money', 'Comics', 'Handbag', 'Porcelain', + 'Rodeo', 'Curtain', 'Tile', 'Human mouth', 'Army', + 'Menu', 'Boat', 'Snowboarding', 'Cairn terrier', 'Net', + 'Pasteles', 'Cup', 'Rugby', 'Pho', 'Cap', + 'Human hair', 'Surfing', 'Loveseat', 'Museum', 'Shipwreck', + 'Trunk (Tree)', 'Plush', 'Monochrome', 'Volcano', 'Rock', + 'Pillow', 'Presentation', 'Nebula', 'Subwoofer', 'Lake', + 'Sledding', 'Bangs', 'Tablecloth', 'Necklace', 'Swimwear', + 'Standing', 'Jeans', 'Carnival', 'Softball', 'Centrepiece', + 'Skateboarder', 'Cake', 'Dragon', 'Aurora', 'Skiing', + 'Bathroom', 'Dog', 'Needlework', 'Umbrella', 'Church', + 'Fire', 'Piano', 'Denim', 'Bridle', 'Cabinetry', + 'Lipstick', 'Ring', 'Television', 'Roller', 'Seal', + 'Concert', 'Product', 'News', 'Fast food', 'Horn (Animal)', + 'Tattoo', 'Bird', 'Bridegroom', 'Love', 'Helmet', + 'Dinosaur', 'Icing', 'Miniature', 'Tire', 'Toy', + 'Icicle', 'Jacket', 'Coffee', 'Mosque', 'Rowing', + 'Wetsuit', 'Camping', 'Underwater', 'Christmas', 'Gelato', + 'Whiteboard', 'Field', 'Ragdoll', 'Construction', 'Lampshade', + 'Palace', 'Meal', 'Factory', 'Cage', 'Clipper (Boat)', + 'Gymnastics', 'Turtle', 'Human foot', 'Marriage', 'Web page', + 'Human beard', 'Fog', 'Wool', 'Cappuccino', 'Lighthouse', + 'Lego', 'Sparkler', 'Sari', 'Model', 'Temple', + 'Beanie', 'Building', 'Waterfall', 'Penguin', 'Cave', + 'Stadium', 'Smile', 'Human hand', 'Park', 'Desk', + 'Shetland sheepdog', 'Bar', 'Eating', 'Neon', 'Dalmatian', + 'Crocodile', 'Wakeboarding', 'Longboard', 'Road', 'Race', + 'Kitchen', 'Odometer', 'Cliff', 'Fiction', 'School', + 'Interaction', 'Bullfighting', 'Boxer', 'Gown', 'Aquarium', + 'Superhero', 'Pie', 'Asphalt', 'Surfboard', 'Cheeseburger', + 'Screenshot', 'Supper', 'Laugh', 'Lunch', 'Party ', + 'Glacier', 'Bench', 'Grandparent', 'Sink', 'Pomacentridae', + 'Blazer', 'Brick', 'Space', 'Backpacking', 'Stuffed toy', + 'Sushi', 'Glitter', 'Bonfire', 'Castle', 'Marathon', + 'Pizza', 'Beach', 'Human ear', 'Racing', 'Sitting', + 'Iceberg', 'Shelf', 'Vehicle', 'Pop music', 'Playground', + 'Clown', 'Car', 'Rein', 'Fur', 'Musician', + 'Casino', 'Baby', 'Alcohol', 'Strap', 'Reef', + 'Balloon', 'Outerwear', 'Cathedral', 'Competition', 'Joker', + 'Blackboard', 'Bunk bed', 'Bear', 'Moon', 'Archery', + 'Polo', 'River', 'Fishing', 'Ferris wheel', 'Mortarboard', + 'Bracelet', 'Flesh', 'Statue', 'Farm', 'Desert', + 'Chain', 'Aircraft', 'Textile', 'Hot dog', 'Knitting', + 'Singer', 'Juice', 'Circus', 'Chair', 'Musical instrument', + 'Room', 'Crochet', 'Sailboat', 'Newspaper', 'Santa claus', + 'Swamp', 'Skyscraper', 'Skin', 'Rocket', 'Aviation', + 'Airliner', 'Garden', 'Ruins', 'Storm', 'Glasses', + 'Balance', 'Nail (Body part)', 'Rainbow', 'Soil ', 'Vacation ', + 'Moustache', 'Doily', 'Food', 'Bride ', 'Cattle', + 'Pocket', 'Infrastructure', 'Train', 'Gerbil', 'Fireworks', + 'Pet', 'Dam', 'Crew', 'Couch', 'Bathing', + 'Quilting', 'Motorcycle', 'Butterfly', 'Sled', 'Watercolor paint', + 'Rafting', 'Monument', 'Lightning', 'Sunset', 'Bumper', + 'Shoe', 'Waterskiing', 'Sneakers', 'Tower', 'Insect', + 'Pool', 'Placemat', 'Airplane', 'Plant', 'Jungle', + 'Armrest', 'Duck', 'Dress', 'Tableware', 'Petal', + 'Bus', 'Hanukkah', 'Forest', 'Hat', 'Barn', + 'Tubing', 'Snorkeling', 'Cool', 'Cookware and bakeware', 'Cycling', + 'Swing (Seat)', 'Muscle', 'Cat', 'Skateboard', 'Star', + 'Toe', 'Junk', 'Bicycle', 'Bedroom', 'Person', + 'Sand', 'Canyon', 'Tie', 'Twig', 'Sphynx', + 'Supervillain', 'Nightclub', 'Ranch', 'Pattern', 'Shorts', + 'Himalayan', 'Wall', 'Leggings', 'Windsurfing', 'Deejay', + 'Dance', 'Van', 'Bento', 'Sleep', 'Wine', + 'Picnic', 'Leisure', 'Dune', 'Crowd', 'Kayak', + 'Ballroom', 'Selfie', 'Graduation', 'Frigate', 'Mountain', + 'Dude', 'Windshield', 'Skiff', 'Class', 'Scarf', + 'Bull', 'Soccer', 'Bag', 'Basset hound', 'Tractor', + 'Swimming', 'Running', 'Track', 'Helicopter', 'Pitch', + 'Clock', 'Song', 'Jersey', 'Stairs', 'Flap', + 'Jewellery', 'Bridge', 'Cuisine', 'Bread', 'Caving', + 'Shell', 'Wreath', 'Roof', 'Cookie', 'Canoe']; + + aboutToAppear() { + abilityAccessCtrl.createAtManager().requestPermissionsFromUser(getContext(this), PERMISSIONS).then((data) => { + if (data.authResults[0] == 0) { + hilog.info(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', 'request permission success'); + } else { + hilog.info(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', 'user rejected'); + } + }).catch((err: BusinessError) => { + hilog.error(0xFF00, 'MindSporeLiteArkTSDemo', '%{public}s', + `request permission failed, error message: ${(err as BusinessError).message}`); + }); + } + + build() { + Row() { + Column() { + Text(this.modelPredict) + .focusable(true) + .fontSize(30) + .fontWeight(FontWeight.Bold) + + //图片显示 + Image(this.uris[0]).width(320).height(540).margin(15) + //显示分类最大值 + if (this.max) { + Text(this.labelsNameMmap[this.maxIndexArray[0]] + + ': ' + + (this.maxArray[0] / 100).toString() + + '%\n' + + this.labelsNameMmap[this.maxIndexArray[1]] + + ': ' + + (this.maxArray[1] / 100).toString() + + '%\n' + + this.labelsNameMmap[this.maxIndexArray[2]] + + ': ' + + (this.maxArray[2] / 100).toString() + + '%\n' + + this.labelsNameMmap[this.maxIndexArray[3]] + + ': ' + + (this.maxArray[3] / 100).toString() + + '%').focusable(true).fontSize(20) + } + + Button() { + Text('photo') + .fontSize(30) + .fontWeight(FontWeight.Bold) + } + .type(ButtonType.Capsule) + .margin({ + top: 20 + }) + .backgroundColor('#0D9FFB') + .width('40%') + .height('5%') + .onClick(() => { + let resMgr: resourceManager.ResourceManager = getContext().getApplicationContext().resourceManager; + resMgr.getRawFileContent(this.modelName).then(modelBuffer => { + + //获取相册图片 + //1.创建图片-视频类型文件选择选项实例 + let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); + + //2.选择媒体文件类型和选择媒体文件的最大数目 + photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; // 过滤选择媒体文件类型为IMAGE + photoSelectOptions.maxSelectNumber = 1; // 选择媒体文件的最大数目 + + //3.创建图库选择器实例,调用select()接口拉起图库界面进行文件选择。文件选择成功后,返回 photoSelectResult 结果集。 + let photoPicker = new photoAccessHelper.PhotoViewPicker(); + photoPicker.select(photoSelectOptions, async ( + err: BusinessError, photoSelectResult: photoAccessHelper.PhotoSelectResult) => { + if (err) { + console.error('MS_LITE_ERR: PhotoViewPicker.select failed with err: ' + JSON.stringify(err)); + return; + } + console.info('MS_LITE_LOG: PhotoViewPicker.select successfully, ' + + 'photoSelectResult uri: ' + JSON.stringify(photoSelectResult)); + this.uris = photoSelectResult.photoUris; + console.info('MS_LITE_LOG: uri: ' + this.uris); + + // 预处理图片数据 + try { + // 1.使用fileIo.openSync接口,通过uri打开这个文件得到fd + let file = fileIo.openSync(this.uris[0], fileIo.OpenMode.READ_ONLY); + console.info('MS_LITE_LOG: file fd: ' + file.fd); + + // 2.通过fd使用fileIo.readSync接口读取这个文件内的数据 + let inputBuffer = new ArrayBuffer(4096000); + let readLen = fileIo.readSync(file.fd, inputBuffer); + console.info('MS_LITE_LOG: readSync data to file succeed and inputBuffer size is:' + readLen); + + // 3.通过 PixelMap 预处理 + let imageSource = image.createImageSource(file.fd); + let pixelMap = await imageSource.createPixelMap(); + let info = await pixelMap.getImageInfo(); + console.info('MS_LITE_LOG: info.width = ' + info.size.width); + console.info('MS_LITE_LOG: info.height = ' + info.size.height); + + // 4.获取图片buffer数据readBuffer,并进行处理 + pixelMap.scale(256.0 / info.size.width, 256.0 / info.size.height).then(() => { + pixelMap.crop( + { x: 16, y: 16, size: { height: this.modelInputHeight, width: this.modelInputWidth } } + ).then(async () => { + let info = await pixelMap.getImageInfo(); + console.info('MS_LITE_LOG: crop info.width = ' + info.size.width); + console.info('MS_LITE_LOG: crop info.height = ' + info.size.height); + + //需要创建的像素buffer大小 + let readBuffer = new ArrayBuffer(this.modelInputHeight * this.modelInputWidth * 4); + await pixelMap.readPixelsToBuffer(readBuffer); + console.info('MS_LITE_LOG: Succeeded in reading image pixel data, buffer: ' + + readBuffer.byteLength); + + //处理readBuffer + const imageArr = new Uint8Array( + readBuffer.slice(0, this.modelInputHeight * this.modelInputWidth * 4)); + console.info('MS_LITE_LOG: imageArr length: ' + imageArr.length); + + let means = [0.485, 0.456, 0.406]; + let stds = [0.229, 0.224, 0.225]; + let float32View = new Float32Array(this.modelInputHeight * this.modelInputWidth * 3); + let index = 0; + for (let i = 0; i < imageArr.length; i++) { + if ((i + 1) % 4 == 0) { + float32View[index] = (imageArr[i - 3] / 255.0 - means[0]) / stds[0]; // B + float32View[index+1] = (imageArr[i - 2] / 255.0 - means[1]) / stds[1]; // G + float32View[index+2] = (imageArr[i - 1] / 255.0 - means[2]) / stds[2]; // R + index += 3; + } + } + console.info('MS_LITE_LOG: float32View length: ' + float32View.length); + let printStr = 'float32View data:'; + for (let i = 0; i < 20; i++) { + printStr += ' ' + float32View[i]; + } + console.info('MS_LITE_LOG: float32View data: ' + printStr); + + let inputs: ArrayBuffer[] = [float32View.buffer]; + + // predict + modelPredict(modelBuffer.buffer.slice(0), inputs).then(outputs => { + console.info('=========MS_LITE_LOG: MS_LITE predict success====='); + + // 结果打印 + for (let i = 0; i < outputs.length; i++) { + let out = new Float32Array(outputs[i].getData()); + + let printStr = outputs[i].name + ':'; + for (let j = 0; j < out.length; j++) { + printStr += out[j].toString() + ','; + } + console.info('MS_LITE_LOG: ' + printStr); + + // 取分类最大值top5 + this.max = 0; + this.maxIndex = 0; + this.maxArray = []; + this.maxIndexArray = []; + let newArray = out.filter(value => value !== this.max) + for (let n = 0; n < 5; n++) { + this.max = out[0]; + this.maxIndex = 0; + // 取最大值 + for (let m = 0; m < newArray.length; m++) { + if (newArray[m] > this.max) { + this.max = newArray[m]; + this.maxIndex = m; + } + } + this.maxArray.push(Math.round(this.max * 10000)) + this.maxIndexArray.push(this.maxIndex) + // filter函数 数组过滤函数 + newArray = newArray.filter(value => value !== this.max) + } + console.info('MS_LITE_LOG: max:' + this.maxArray); + console.info('MS_LITE_LOG: maxIndex:' + this.maxIndexArray); + } + console.info('=========MS_LITE_LOG END========='); + }) + }) + }) + + // 5.关闭文件 + fileIo.closeSync(file) + } catch (err) { + console.error('MS_LITE_LOG: uri: open file fd failed.' + err) + } + }) + }) + }) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/model.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/model.ets new file mode 100644 index 0000000000000000000000000000000000000000..cdc00f3c4edb5595da6f3b463dbe1db1833bfc6c --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/ets/pages/model.ets @@ -0,0 +1,34 @@ +import { mindSporeLite } from '@kit.MindSporeLiteKit' + +export default async function modelPredict( + modelBuffer: ArrayBuffer, inputsBuffer: ArrayBuffer[]): Promise { + + //1.创建上下文 + let context: mindSporeLite.Context = {}; + context.target = ['cpu']; + context.cpu = {} + context.cpu.threadNum = 2; + context.cpu.threadAffinityMode = 1; + context.cpu.precisionMode = 'enforce_fp32'; + + //2.加载模型 + let msLiteModel: mindSporeLite.Model = await mindSporeLite.loadModelFromBuffer(modelBuffer, context); + + //3.设置输入数据 + let modelInputs: mindSporeLite.MSTensor[] = msLiteModel.getInputs(); + + // 本模型不支持其他shape resize + for (let i = 0; i < inputsBuffer.length; i++) { + let inputBuffer = inputsBuffer[i]; + if (inputBuffer != null) { + modelInputs[i].setData(inputBuffer as ArrayBuffer); + } + } + + //4.执行推理 + console.info('=========MS_LITE_LOG: MS_LITE predict start====='); + let modelOutputs: mindSporeLite.MSTensor[] = await msLiteModel.predict(modelInputs); + return modelOutputs; +} + + diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/module.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..757594e89002393bdfdebd7a91923d9be819c794 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/module.json5 @@ -0,0 +1,65 @@ +/* +* Copyright (c) 2024-2025 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. +*/ + +{ + "module": { + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + "abilities": [ + { + "name": "EntryAbility", + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:startIcon", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "ohos.want.action.home" + ] + } + ] + } + ], + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ] + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/color.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..3c712962da3c2751c2b9ddb53559afcbd2b54a02 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#FFFFFF" + } + ] +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/float.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..33ea22304f9b1485b5f22d811023701b5d4e35b6 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/float.json @@ -0,0 +1,8 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + } + ] +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1f44677e5e81bc172f0bcc1233623f90fe095f77 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "module_desc", + "value": "module description" + }, + { + "name": "EntryAbility_desc", + "value": "description" + }, + { + "name": "EntryAbility_label", + "value": "MindSporeLite" + }, + { + "name": "read_image_video_permission", + "value": "ȡûĿ¼ͼƬƵļ" + } + ] +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/background.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/background.png new file mode 100644 index 0000000000000000000000000000000000000000..923f2b3f27e915d6871871deea0420eb45ce102f Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/background.png differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/foreground.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/foreground.png new file mode 100644 index 0000000000000000000000000000000000000000..97014d3e10e5ff511409c378cd4255713aecd85f Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/foreground.png differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/layered_image.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/layered_image.json new file mode 100644 index 0000000000000000000000000000000000000000..fb49920440fb4d246c82f9ada275e26123a2136a --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/layered_image.json @@ -0,0 +1,7 @@ +{ + "layered-image": + { + "background" : "$media:background", + "foreground" : "$media:foreground" + } +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/startIcon.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/startIcon.png new file mode 100644 index 0000000000000000000000000000000000000000..1090fe07135c34ccf8875f73595ad0ddad9f66e1 Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/media/startIcon.png differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/profile/backup_config.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/profile/backup_config.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40ae7c494d71e2482278f359ec790ca73471a --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/profile/backup_config.json @@ -0,0 +1,3 @@ +{ + "allowToBackupRestore": true +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/profile/main_pages.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/profile/main_pages.json new file mode 100644 index 0000000000000000000000000000000000000000..1898d94f58d6128ab712be2c68acc7c98e9ab9ce --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/base/profile/main_pages.json @@ -0,0 +1,5 @@ +{ + "src": [ + "pages/Index" + ] +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/dark/element/color.json b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/dark/element/color.json new file mode 100644 index 0000000000000000000000000000000000000000..79b11c2747aec33e710fd3a7b2b3c94dd9965499 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/dark/element/color.json @@ -0,0 +1,8 @@ +{ + "color": [ + { + "name": "start_window_background", + "value": "#000000" + } + ] +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/rawfile/mobilenetv2.ms b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/rawfile/mobilenetv2.ms new file mode 100644 index 0000000000000000000000000000000000000000..1564f860d1d1c5ed31f5388d09d337a7cbed2878 Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/main/resources/rawfile/mobilenetv2.ms differ diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/mock/mock-config.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/mock/mock-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..40f1846fc4caa2aadee10e3898d2c0757ccfee66 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/mock/mock-config.json5 @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2024 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. +*/ + +{ +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..f6b16e6f0196bdc489d48c3209eabd08b460892f --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/Ability.test.ets @@ -0,0 +1,118 @@ +/* +* Copyright (c) 2024-2025 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. +*/ + +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { describe, it, expect } from '@ohos/hypium'; +import Want from '@ohos.app.ability.Want'; +import Base from '@ohos.base'; +import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; +import { Driver, ON } from '@ohos.UiTest'; +import { getString } from '../utils/ResourceUtil'; + +const BUNDLE = 'MindSporeLiteArkTSDemo_'; +const TAG = 'abilityTest'; +const domain: number = 0xF811; + +export default function abilityTest() { + let driver: Driver = Driver.create(); + describe('MindSporeLiteArkTSDemoTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + /* + * @tc.number: MindSporeLiteArkTSDemoTest_000 + * @tc.name: Start Ability + * @tc.desc: Start Ability + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level Level 1 + */ + it('EntryAbility', 0, async (done: Function) => { + hilog.info(domain, TAG, BUNDLE + 'EntryAbility_001 begin'); + // Ӧ + let want: Want = { + bundleName: 'com.samples.mindsporelitearktsdemo', + abilityName: 'EntryAbility' + }; + let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); + abilityDelegator.startAbility(want, (err: Base.BusinessError) => { + hilog.info(domain, TAG, 'StartAbility get err ' + JSON.stringify(err)); + expect(err).assertNull(); + }) + await driver.delayMs(3000); + // ûȨ + await driver.assertComponentExist(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + let permissionAllowBtn = await driver.findComponent(ON.text( + await abilityDelegator.getAppContext().resourceManager.getStringValue($r('app.string.allow')))); + await permissionAllowBtn.click(); + await driver.delayMs(1000); + hilog.info(domain, TAG, BUNDLE + 'EntryAbility_001 end'); + done(); + }) + /** + * @tc.number : MindSporeLiteArkTSDemoTest_001 + * @tc.name : open photo + * @tc.desc : open photo + * @tc.size : MEDIUM + * @tc.type : Function + * @tc.level : Level 1 + */ + it('IfExists', 0, async (done: Function) => { + hilog.info(domain, TAG, BUNDLE + 'IfExists_001 begin'); + await driver.delayMs(1000); + await driver.assertComponentExist(ON.text('MindSporeLite ArkTS Demo')); + await driver.assertComponentExist(ON.text('photo')); + expect(true).assertTrue(); + hilog.info(domain, TAG, BUNDLE + 'IfExists_001 end'); + done(); + }) + + /** + * @tc.number : MindSporeLiteArkTSDemoTest_002 + * @tc.name : open photo and predict + * @tc.desc : open photo and predict + * @tc.size : MEDIUM + * @tc.type : Function + * @tc.level : Level 1 + */ + it('PhotoPredict', 0, async (done: Function) => { + hilog.info(domain, TAG, BUNDLE + 'PhotoPredict_001 begin'); + await driver.delayMs(1000); + // photoť + hilog.info(domain, TAG, BUNDLE + 'PhotoPredict_001 click photo button'); + await driver.assertComponentExist(ON.text('photo')); + let msPhoto = await driver.findComponent(ON.text('photo')); + await msPhoto.click(); + await driver.delayMs(5000); + // ƬѡһƬ + let gridItems = await driver.findComponents(ON.type('GridItem')); + await gridItems[1].click(); + await driver.delayMs(5000); + // + let tipCompleted: string = getString($r('app.string.completed')); + await driver.assertComponentExist(ON.text(tipCompleted)); + let finish = await driver.findComponent(ON.text(tipCompleted)); + await driver.delayMs(5000); + await finish.click(); + await driver.delayMs(5000); + // ͼƬ + await driver.assertComponentExist(ON.text('MindSporeLite ArkTS Demo')); + await driver.assertComponentExist(ON.text('%', 1)); + await driver.delayMs(5000); + expect(true).assertTrue(); + hilog.info(domain, TAG, BUNDLE + 'PhotoPredict_001 end'); + done(); + }) + }) +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/List.test.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..794c7dc4ed66bd98fa3865e07922906e2fcef545 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/ets/test/List.test.ets @@ -0,0 +1,5 @@ +import abilityTest from './Ability.test'; + +export default function testsuite() { + abilityTest(); +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/module.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..97b155769d35ce3e79dd5650af4d2b1dcb66b885 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/ohosTest/module.json5 @@ -0,0 +1,26 @@ +/* +* Copyright (c) 2024 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. +*/ + +{ + "module": { + "name": "entry_test", + "type": "feature", + "deviceTypes": [ + "phone" + ], + "deliveryWithInstall": true, + "installationFree": false + } +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/test/List.test.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/test/List.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..bb5b5c3731e283dd507c847560ee59bde477bbc7 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/test/List.test.ets @@ -0,0 +1,5 @@ +import localUnitTest from './LocalUnit.test'; + +export default function testsuite() { + localUnitTest(); +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/test/LocalUnit.test.ets b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/test/LocalUnit.test.ets new file mode 100644 index 0000000000000000000000000000000000000000..165fc1615ee8618b4cb6a622f144a9a707eee99f --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/entry/src/test/LocalUnit.test.ets @@ -0,0 +1,33 @@ +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; + +export default function localUnitTest() { + describe('localUnitTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + it('assertContain', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + let a = 'abc'; + let b = 'b'; + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(a).assertContain(b); + expect(a).assertEqual(a); + }); + }); +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/hvigor/hvigor-config.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/hvigor/hvigor-config.json5 new file mode 100644 index 0000000000000000000000000000000000000000..c63e910373e5ead86612270e605a391acdc32ec9 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/hvigor/hvigor-config.json5 @@ -0,0 +1,23 @@ +{ + "modelVersion": "5.1.1", + "dependencies": { + }, + "execution": { + // "analyze": "normal", /* Define the build analyze mode. Value: [ "normal" | "advanced" | false ]. Default: "normal" */ + // "daemon": true, /* Enable daemon compilation. Value: [ true | false ]. Default: true */ + // "incremental": true, /* Enable incremental compilation. Value: [ true | false ]. Default: true */ + // "parallel": true, /* Enable parallel compilation. Value: [ true | false ]. Default: true */ + // "typeCheck": false, /* Enable typeCheck. Value: [ true | false ]. Default: false */ + // "optimizationStrategy": "memory" /* Define the optimization strategy. Value: [ "memory" | "performance" ]. Default: "memory" */ + }, + "logging": { + // "level": "info" /* Define the log level. Value: [ "debug" | "info" | "warn" | "error" ]. Default: "info" */ + }, + "debugging": { + // "stacktrace": false /* Disable stacktrace compilation. Value: [ true | false ]. Default: false */ + }, + "nodeOptions": { + // "maxOldSpaceSize": 8192 /* Enable nodeOptions maxOldSpaceSize compilation. Unit M. Used for the daemon process. Default: 8192*/ + // "exposeGC": true /* Enable to trigger garbage collection explicitly. Default: true*/ + } +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/hvigorfile.ts b/MindSporeLiteKit/MindSporeLiteArkTSDemo/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..47113e2e36ecefde41c136272a0bd6ff745cffe4 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/hvigorfile.ts @@ -0,0 +1,6 @@ +import { appTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins: [] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/oh-package.json5 b/MindSporeLiteKit/MindSporeLiteArkTSDemo/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..052cc24d6e7b3f754ab5557652fd9db52c4c2549 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/oh-package.json5 @@ -0,0 +1,10 @@ +{ + "modelVersion": "5.1.1", + "description": "Please describe the basic information.", + "dependencies": { + }, + "devDependencies": { + "@ohos/hypium": "1.0.21", + "@ohos/hamock": "1.0.0" + } +} diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/ohosTest.md b/MindSporeLiteKit/MindSporeLiteArkTSDemo/ohosTest.md new file mode 100644 index 0000000000000000000000000000000000000000..095f1d955b756cf4d5d8467da601f845698789e5 --- /dev/null +++ b/MindSporeLiteKit/MindSporeLiteArkTSDemo/ohosTest.md @@ -0,0 +1,9 @@ +# MindSporeLiteArkTSDemo 测试用例归档 + +## 用例表 + +|测试功能|预置条件|输入|预期输出|是否自动|测试结果| +|--------------------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------| +|拉起应用| 设备正常运行| |成功拉起应用|是|Pass| +|查看预览界面Text和Button| 进入预览界面 | |预览界面存在Text和Button|是|Pass| +|选取图片推理功能测试| 进入预览界面 | 点击photo按钮 |选取图片后进行推理|是|Pass| \ No newline at end of file diff --git a/MindSporeLiteKit/MindSporeLiteArkTSDemo/screenshots/MindSporeLiteArkTSDemo.png b/MindSporeLiteKit/MindSporeLiteArkTSDemo/screenshots/MindSporeLiteArkTSDemo.png new file mode 100644 index 0000000000000000000000000000000000000000..23be4d2c55f79620fc4206b5a1e3da400547e12b Binary files /dev/null and b/MindSporeLiteKit/MindSporeLiteArkTSDemo/screenshots/MindSporeLiteArkTSDemo.png differ