去年5月的时候,Jerry曾经写了一篇文章:使用Java程序消费SAP Leonardo的机器学习API,而最近另外做的一个项目,需要在Web应用里做同样的事情。
因为有了前一篇文章的铺垫,避免了很多重复的工作量。本文还是选择使用SAP Leonardo里的一个Product Image Classification API,即给定一张产品的图片,该API能识别出此产品的类别。
再回顾下这个API的功能:该API的模型是由SAP基于大约5万张Icecat图片训练而成,能区分29种不同的类别,这些类别具体罗列于官方文档上,比如电脑显示器,数码相机,外部存储设备,键盘,液晶电视,手机充电器,笔记本和其他外设等等。如果我们消费这个API时指定的图片代表的产品不属于这29种类别之一,API的表现如何?先卖个关子,文末解答。

关于如何在api.sap.com里找到这个API并且在API console里测试,请参考Jerry之前的文章:使用Java程序消费SAP Leonardo的机器学习API。
这里假定我们已经找到了该API,点击进入明细页面,将API Key复制下来,后续的UI5应用需要使用到。


然后进入SAP云平台的Neo环境。Jerry这个练习,使用免费的SAP Cloud Platform Neo测试环境即可。

在Service列表里找到WebIDE——我们将使用WebIDE进行UI5应用的开发。

Jerry已经开发好了一个UI5应用上传到我的Github上了:https://github.com/i042416/MachineLearning-UI5
大家可以直接在WebIDE里clone这个仓库,或者把仓库的内容以zip包的形式下载到本地,再使用WebIDE的本地Import功能导入。


我们要告诉UI5应用这个API的url,因此在Neo环境里创建一个Destination(作用和ABAP Netweaver事务码SM59里创建的Destination相同):

属性如上图所示,因为是Neo测试环境,所以url为对应的sandbox环境:https://sandbox.api.sap.com/ml
记下这个Destination名称sapui5ml-api, 因为稍后的UI5代码里需要使用。
记得维护额外的属性WebIDEnabled为true,这样该Destination才能在UI5应用里被使用。点击Check Connection确保看到绿灯。

打开WebIDE里UI5工程里的settings.json文件,将您之前从API console里拷贝的API Key粘贴到此处:

在项目根目录下的neo-app.json文件里,把类型为destination的target对象的名称维护成之前在SAP Cloud Platform Cockpit里创建的destination相同的名称。

运行这个UI5应用,能看到如下界面:

做一些简单的测试:

SAP Leonardo的机器学习API,识别出这张图片有74.7%的可能性是一台笔记本电脑,13.8%的可能性是键盘,11.3%的可能性是Tablets。

点击按钮View JSON,能看到调用SAP Leonardo API返回的技术明细。

鼠标的图片也成功识别出来了:

本文开始曾经提到这个API能识别出29种不同的产品类别,现在换一种产品,如下图:

这是Jerry小时候就很痴迷的《终结者》系列的T800,我在2017年回复SAP社区上一篇博客时也引用到了这款经典的模型,和阿诺德 - 施瓦辛格那句激励无数中老年程序员的著名台词:I am old but I am NOT OBSOLETE
原文链接: https://blogs.sap.com/2016/12/19/and-now-for-something-completely-different/

言归正传,如果希望SAP Leonardo的Product Image Classification API也能成功将T800识别出来,需要做些什么?

答案是重新训练模型。具体如何做,Jerry也还在研究,等研究完毕再分享。感谢阅读。
要获取更多Jerry的原创文章,请关注公众号"汪子熙":

网友评论