From fa798a47aeb1cec4212b6aeee198002811910e41 Mon Sep 17 00:00:00 2001 From: CodingGorit Date: Wed, 16 Jul 2025 15:08:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=84=E8=8C=83=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 3 +- README.md | 3 +- entry/src/main/ets/pages/ImageGenerate.ets | 19 +++++----- entry/src/main/ets/pages/Index.ets | 5 ++- entry/src/main/ets/utils/Gaussion.ets | 5 ++- entry/src/main/ets/utils/Logger.ets | 44 ++++++++++++++++++++++ entry/src/main/ets/utils/Predict.ets | 13 ++++--- 7 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 entry/src/main/ets/utils/Logger.ets diff --git a/README.en.md b/README.en.md index bbc13e5..fd34b27 100644 --- a/README.en.md +++ b/README.en.md @@ -28,6 +28,7 @@ This example utilizes the ArkTS API provided by `@ohos.ai.mindSporeLite` to impl │ │ └──ImageGenerate.ets // Original and composite image preview interface │ └──util │ ├──Gaussion.ets // Gaussian filtering algorithm utility class +│ ├──Logger.ets // Logging tool class │ └──Predict.ets // Model inference implementation └──entry/src/main/resources/ └──rawfile @@ -39,7 +40,7 @@ The terminal image segmentation model used in this sample application is `model_ - The home page uses [@ohos.file.photoAccessHelper](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-photoaccesshelper) (image file selection) to open the album, [@ohos.multimedia.image](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-image) (image processing effects), and [@ohos.file.fs](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-file-fs) (basic file operations) APIs to fetch and process album images. The complete code can be found in [Index.ets](entry/src/main/ets/pages/Index.ets) and [ImageGenerate.ets](entry/src/main/ets/pages/ImageGenerate.ets). -- The image composition page uses the [@kit.MindSporeLiteKit](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-mindsporelite) (inference capability) API to perform on-device inference. The complete code can be found in [Predict.ets](entry/src/main/ets/utils/Predict.ets). +- The image composition page uses the [@ohos.ai.mindSporeLite](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-mindsporelite) (inference capability) API to perform on-device inference. The complete code can be found in [Predict.ets](entry/src/main/ets/utils/Predict.ets). - The inference function is called, and the results are processed. The complete code can be found in [ImageGenerate.ets](entry/src/main/ets/pages/ImageGenerate.ets). diff --git a/README.md b/README.md index c03ea4a..17b93ac 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ │ │ └──ImageGenerate.ets // 原图和合成图预览界面 │ └──util │ ├──Gaussion.ets // 高斯滤波算法工具类 +│ ├──Logger.ets // 日志工具类 │ └──Predict.ets // 模型推理实现 └──entry/src/main/resources/ └──rawfile @@ -41,7 +42,7 @@ - 首页调用[@ohos.file.photoAccessHelper](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-photoaccesshelper) (图片文件选择)拉起相册、[@ohos.multimedia.image](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-image) (图片处理效果)、[@ohos.file.fs](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-file-fs) (基础文件操作) 等API实现相册图片获取及图片处理。完整代码请参见[Index.ets](entry/src/main/ets/pages/Index.ets)、[ImageGenerate.ets](entry/src/main/ets/pages/ImageGenerate.ets) -- 图像合成页调用[@kit.MindSporeLiteKit](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-mindsporelite) (推理能力) API实现端侧推理。完整代码请参见[Predict.ets](entry/src/main/ets/utils/Predict.ets) +- 图像合成页调用[@ohos.ai.mindSporeLite](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-mindsporelite) (推理能力) API实现端侧推理。完整代码请参见[Predict.ets](entry/src/main/ets/utils/Predict.ets) - 调用推理函数并处理结果。完整代码请参见[ImageGenerate.ets](entry/src/main/ets/pages/ImageGenerate.ets) diff --git a/entry/src/main/ets/pages/ImageGenerate.ets b/entry/src/main/ets/pages/ImageGenerate.ets index c860f21..8b13a38 100644 --- a/entry/src/main/ets/pages/ImageGenerate.ets +++ b/entry/src/main/ets/pages/ImageGenerate.ets @@ -26,6 +26,7 @@ import { } from "../common/constants/ImageDataListConstant"; import NavigationParam from "../model/NavigationParam"; import modelPredict from "../utils/Predict"; +import Logger from '../utils/Logger'; import { gaussianRun } from "../utils/Gaussion"; @@ -165,31 +166,31 @@ export struct ImageGenerate { try { // 1. use the `fileIo.openSync` interface to open the file via URI and obtain the file descriptor (fd) let file = fileIo.openSync(this.photoUri, fileIo.OpenMode.READ_ONLY); - console.log('MS_LITE_LOG: file.fd: ' + file.fd); + Logger.info('MS_LITE_LOG: file.fd: ' + file.fd); // 2. Read the data within this file using the fileIo.readSync interface through fd let inputBuffer = new ArrayBuffer(4096000); let readLen = fileIo.readSync(file.fd, inputBuffer); - console.log('MS_LITE_LOG: readSync data to file succeed and inputBuffer size is: ' + readLen); + Logger.info('MS_LITE_LOG: readSync data to file succeed and inputBuffer size is: ' + readLen); // 3. Preprocessing through PixelMap let imageSource = image.createImageSource(file.fd); let pixelMap = imageSource.createPixelMapSync(); let info = pixelMap.getImageInfoSync(); - console.log(`MS_LITE_LOG: info.size is => ${JSON.stringify(info.size)}`); + Logger.info(`MS_LITE_LOG: info.size is => ${JSON.stringify(info.size)}`); // 4. Obtain the image buffer data readBuffer and process it. pixelMap.scaleSync(MODEL_INPUT_WIDTH / info.size.width, MODEL_INPUT_HEIGHT / info.size.height); // The size of the pixel buffer that needs to be created. let readBuffer = new ArrayBuffer(MODEL_INPUT_HEIGHT * MODEL_INPUT_WIDTH * 4); await pixelMap.readPixelsToBuffer(readBuffer); - console.log('MS_LITE_LOG: Succeeded in reading image pixel data, buffer: ' + readBuffer.byteLength); + Logger.info('MS_LITE_LOG: Succeeded in reading image pixel data, buffer: ' + readBuffer.byteLength); pixelMap.release(); // Process the read buffer const imageArr = new Uint8Array( readBuffer.slice(0, MODEL_INPUT_HEIGHT * MODEL_INPUT_WIDTH * 4)); - console.info('MS_LITE_LOG: imageArr length: ' + imageArr.length); + Logger.info('MS_LITE_LOG: imageArr length: ' + imageArr.length); // Construct Input Tensor (Input Preprocessing); // === Attention! This step needs to be performed according to the image storage format of the model, as NCHW and NHWC are constructed differently! === @@ -208,10 +209,10 @@ export struct ImageGenerate { } let inputs: ArrayBuffer[] = [float32View.buffer]; - console.log('inputs length is ' + inputs.length); + Logger.info('inputs length is ' + inputs.length); // predict modelPredict(modelBuffer.buffer.slice(0), inputs).then(async outputs => { - console.info("==== MS_LITE_LOG: MS_LITE predict success ===="); + Logger.info("==== MS_LITE_LOG: MS_LITE predict success ===="); let output: Float32Array = new Float32Array(outputs[0].getData()); @@ -287,14 +288,14 @@ export struct ImageGenerate { imageSourceBg.release(); }) } catch (error) { - console.error("Failed to get media content", JSON.stringify(error)); + Logger.error("Failed to get media content", JSON.stringify(error)); } }) // 5. close fileIo fileIo.closeSync(file); imageSource.release(); } catch (err) { - console.error("MS_LITE_LOG: uri: open file fd failed." + err); + Logger.error("MS_LITE_LOG: uri: open file fd failed." + err); } } diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4a15dfc..5955f81 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -16,6 +16,7 @@ import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { BusinessError } from '@kit.BasicServicesKit'; import NavigationParam from '../model/NavigationParam'; +import Logger from '../utils/Logger'; @Entry @Component @@ -45,10 +46,10 @@ struct Index { 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)); + Logger.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.photoUris)}`); + Logger.info('MS_LITE_LOG: PhotoViewPicker.select successfully, ' + `photoSelectResult uri + ${JSON.stringify(photoSelectResult.photoUris)}`); let param = new NavigationParam(photoSelectResult.photoUris[0]); if (param && param.photoUri.length > 0) { this.pathStack.pushPath({ name: 'ImageGenerate', param}); diff --git a/entry/src/main/ets/utils/Gaussion.ets b/entry/src/main/ets/utils/Gaussion.ets index c8822b8..ac48126 100644 --- a/entry/src/main/ets/utils/Gaussion.ets +++ b/entry/src/main/ets/utils/Gaussion.ets @@ -14,9 +14,10 @@ */ import { MODEL_INPUT_HEIGHT } from '../common/constants/ImageDataListConstant'; +import Logger from './Logger'; function gaussianRun(mat: Uint8Array) { - console.log("----- gaussian run -----"); + Logger.info("----- gaussian run -----"); let rMat = new Array>(); let gMat = new Array>(); let bMat = new Array>(); @@ -49,7 +50,7 @@ function gaussianRun(mat: Uint8Array) { } function calcGaussian(calcMat: Array>) { - console.log("_...-----calcGaussian run----------") + Logger.info("_...-----calcGaussian run----------") for (let i = 1; i < calcMat.length - 1; ++i) { for (let j = 1; j < calcMat[i].length - 1; ++j) { calcMat[i][j] = getOpValue(calcMat, i, j) diff --git a/entry/src/main/ets/utils/Logger.ets b/entry/src/main/ets/utils/Logger.ets new file mode 100644 index 0000000..4c3d14f --- /dev/null +++ b/entry/src/main/ets/utils/Logger.ets @@ -0,0 +1,44 @@ +/* + * Copyright (c) 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'; + +class Logger { + private domain: number; + private prefix: string; + private format: string = '%{public}s, %{public}s'; + + constructor(prefix: string) { + this.prefix = prefix; + this.domain = 0xff00; + } + + debug(...args: string[]): void { + hilog.debug(this.domain, this.prefix, this.format, args); + } + + info(...args: string[]): void { + hilog.info(this.domain, this.prefix, this.format, args); + } + + warn(...args: string[]): void { + hilog.warn(this.domain, this.prefix, this.format, args); + } + + error(...args: string[]): void { + hilog.error(this.domain, this.prefix, this.format, args); + } +} + +export default new Logger('[Samples_MSLiteHumanSegmentation]'); \ No newline at end of file diff --git a/entry/src/main/ets/utils/Predict.ets b/entry/src/main/ets/utils/Predict.ets index 5403f06..3004e85 100644 --- a/entry/src/main/ets/utils/Predict.ets +++ b/entry/src/main/ets/utils/Predict.ets @@ -14,6 +14,7 @@ */ import { mindSporeLite } from '@kit.MindSporeLiteKit'; +import Logger from './Logger'; export default async function modelPredict (modelBuffer: ArrayBuffer, inputsBuffer: ArrayBuffer[]): Promise { @@ -24,23 +25,23 @@ export default async function modelPredict context.cpu.threadNum = 2; context.cpu.threadAffinityMode = 1; context.cpu.precisionMode = 'enforce_fp32'; - console.info("MS_LITE_LOG load model before ...") + Logger.info("MS_LITE_LOG load model before ...") // 2. Load Model let msLiteModel: mindSporeLite.Model = await mindSporeLite.loadModelFromBuffer(modelBuffer, context); - console.info("MS_LITE_LOG load model after...") + Logger.info("MS_LITE_LOG load model after...") // 3. Set input data let modelInputs: mindSporeLite.MSTensor[] = msLiteModel.getInputs(); - console.info("MS_LITE_L06 msLiteModel.getInputs ...") + Logger.info("MS_LITE_L06 msLiteModel.getInputs ...") // This model does not support resizing other shapes. for (let i = 0; i < inputsBuffer.length; i++) { let inputBuffer = inputsBuffer[i]; - if (inputBuffer != null) { + if (inputBuffer !== null) { modelInputs[i].setData(inputBuffer as ArrayBuffer); } } // 4. Execute reasoning - console.info('=========MS_LITE_L06: MS_LITE predict start====='); + Logger.info('=========MS_LITE_L06: MS_LITE predict start====='); let modelOutputs: mindSporeLite.MSTensor[] = await msLiteModel.predict(modelInputs) ; - console.info("MS_LITE_LOG predict after ..."); + Logger.info("MS_LITE_LOG predict after ..."); return modelOutputs; } \ No newline at end of file -- Gitee