关于hms-core:ML拍照购物功能集成教程分享

121次阅读

共计 4892 个字符,预计需要花费 13 分钟才能阅读完成。

利用场景

拍照购物服务次要利用于线上购物场景。例如,购物类 App 集成该服务能够提供图片搜寻商品性能,用户拍摄商品图像时,能够利用该性能疾速找到想要的商品。

一. 开启服务

  1. 在 AppGallery Connect 上的 我的我的项目 à 构建 à 机器学习服务 à 配置 à 拍照购物 里增加商品集:

  1. 增加完后须要分割 ml 相干人员来进行审核,审核通过后即可增加离线商品,这里介绍用 postman 来增加商品。

审核实现后的商品集:

二. 简略增加商品

  1. 申请 accessToken:

拍照购服务会对每个拜访的申请进行身份验证,所以无论是.com 或.cn 提交申请,都须要在申请头中蕴含签名 (Authorization) 信息。拍照购服务通过应用用户在华为利用联盟申请的 clientId 和 clientSecre 获取 accessToken 进行对称加密的办法来验证申请的发送者身份,accessToken 有效期为一个小时。

这里的 URI 为 https://oauth-login.cloud.hua…

clientId 和 clientSecre 都是在 AppGallery Connect 中的利用信息里的参数。

申请后果示例:

HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
{
    "access_token":"CFyJ7eTl8WIPi9603E7Ro9Icy+K0JYe2qVjS8uzwCPltlO0fC7mZ0gzZX9p8CCwAaiU17nyP+N8+ORRzjjk1EA==",
    "expires_in":3600,
    "token_type":"Bearer"
}
  1. 新增商品

(1)首先将取得的 token 输出在 Authorization 的 token 处,TYPE 选 Bearer Token

(2)配置 post 的 Headers 参数

这里 post 的参数为服务地址 + 音讯头,中国区的地址为 https://ml-api-drcn.ai.dbankc…。

URI 为 /v1/mlkit/snapshop/set/${product-set-id}/product/add,其中 ${product-set-id}就是申请的商品集的名称。
HMS-APPLICATION-ID:APK 的标识 app_id 华为开发者联盟申请的 client_id。
X-Request-ID:申请 ID 通过 UUID.randomUUID()生成。
X-Package-Name:包名 package_name 用户的包门路。
X-Mlkit-Version:MLKIT 版本号。

(3)最初在 Body 的 raw 中配置参数(JSON)。

{
"appPackage":"com.huawei.industrydemo.shopping",
"category": "4",
"customContent": "test",
"images": [{"imageId": "","region":"", "url": "https://res.vmallres.com/pimages//product/6941487204656/group//800_800_2A4099A441BF0670CA0F6BA0EEF5D70E16430F99A6699CB3.png"}],
"productId": 5,
"productUrl": "https://res.vmallres.com/pimages//product/6941487204656/group//800_800_2A4099A441BF0670CA0F6BA0EEF5D70E16430F99A6699CB3.png"
}

其中必须要有的参数为 productId、images 以及 images 里的 url。

category:0-others,1-clothing,2-shoes,3-bags,4-digital & home appliances,5-homegoods,6-toys,7-cosmetics,8-accessories,9-food

customContent:用户自定义商品信息,查问的时候会返回该参数

productId:商品 ID,用于惟一标识商品,雷同商品集不可能反复;格局: 只容许蕴含大小写字母数字、-、_

images:图片列表

productUrl:商品的 Url,可选参数,查问的时候会返回该参数

(1)以上都设置结束就能够发送 post 申请了,胜利后返回:
{“retCode”:”0″,”retMsg”:”Success”}

  1. 删除商品

既然有增加商品,这里再介绍一下删除商品,以便能够更好地调试。
(1)第一步和下面一样都是配置 Authorization。
(2)配置 Headers。

这里的是 DELETE 申请 URI 为 /v1/mlkit/snapshop/set/${product-set-id}/product/${product-id}
${product-set-id}就是申请的商品集的名称,${product-id}为商品 ID。
其余参数与下面的基本一致,多了个 X -Country-Code:cn,填写地区信息。

(3)删除不必配置 Body 的参数,上述 2 部实现即可发送,胜利删除时返回:
{“retCode”:”0″,”retMsg”:”Success”}

三. Demo 开发

  1. 在我的项目级 gradle 里增加 maven 仓
buildscript {
repositories {google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
dependencies {
...
classpath 'com.huawei.agconnect:agcp:1.4.1.300'
}
}
 
allprojects {
repositories {google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
  1. 在利用级的 build.gradle 外面加上 SDK 依赖
dependencies{
// 引入拍照购物服务 SDK
implementation 'com.huawei.hms:ml-computer-vision-cloud:2.0.3.300'
}
 
在 apply plugin: 'com.android.application' 的上面加上:apply plugin: 'com.huawei.agconnect'
  1. 在 AndroidManifest.xml 文件外面申请必要的权限
<uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.INTERNET" />
  1. 取得照片后,创立拍照购物分析器,进行图片剖析
MLApplication.getInstance().setApiKey(AGConnectServicesConfig.fromContext(this).getString("client/api_key"));
 MLRemoteProductVisionSearchAnalyzerSetting setting = new MLRemoteProductVisionSearchAnalyzerSetting.Factory()     // 设置最多返回的商品信息数。.setLargestNumOfReturns(2)     // 设置商品集 ID。.setProductSetId("demo")     // 设置站点区域
     .setRegion(MLRemoteProductVisionSearchAnalyzerSetting.REGION_DR_CHINA)
     .create();
 MLRemoteProductVisionSearchAnalyzer analyzer =
     MLAnalyzerFactory.getInstance().getRemoteProductVisionSearchAnalyzer(setting);// 通过 bitmap 创立 MLFrame,bitmap 为通过拍照取得的图片。MLFrame frame = new MLFrame.Creator().setBitmap(photo).create();// 进行图片检测
 Task<List<MLProductVisionSearch>> task = analyzer.asyncAnalyseFrame(frame);
 task.addOnSuccessListener(new OnSuccessListener<List<MLProductVisionSearch>>() {
     @Override
     public void onSuccess(List<MLProductVisionSearch> productVisionSearchList) {if (productVisionSearchList == null || productVisionSearchList.size() == 0) {return;}
         for (MLProductVisionSearch productVisionSearch : productVisionSearchList) {for (MLVisionSearchProduct product : productVisionSearch.getProductList()) {             // 辨认胜利的解决逻辑,这里是获取商品的 url 显示图片
                 String url = product.getProductUrl();
                 if (url != null && !(url.equals(""))) {MyAsyncTask asyncTask = new MyAsyncTask(resultImg);
                     asyncTask.execute(url);
                     resultText.setText(product.getCustomContent());
                 }                     }
         }
 
     }
 }).addOnFailureListener(new OnFailureListener() {
     @Override
     public void onFailure(Exception e) {       // 辨认失败的解决逻辑
         MLException mlException = (MLException) e;
         Log.e(TAG, "error" + "error code:" + mlException.getErrCode() + "\n" + "error message:"
                 + mlException.getMessage());
     }
 });
  1. 辨认实现,进行分析器,开释检测资源。
if (analyzer != null) {analyzer.stop();
         }

四. 成果动图:

欲了解更多详情,请参阅:
华为开发者联盟官网:https://developer.huawei.com/consumer/cn/hms?ha_source=hms1
获取开发领导文档:https://developer.huawei.com/consumer/cn/doc/development?ha_source=hms1
参加开发者探讨请到 Reddit 社区:https://www.reddit.com/r/HuaweiDevelopers/
下载 demo 和示例代码请到 Github:https://github.com/HMS-Core
解决集成问题请到 Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0201436704554280219?fid=18

原作者:胡椒

正文完
 0