作者:俏巴
概述
智能视觉AI开放平台是面向企业用户、软硬件服务商和开发者,提供简单、易用、优质视觉算法的开放平台。平台算法能力由阿里巴巴达摩院机器智能实验室以及集团内多个视觉团队多年研发沉淀,并经过阿里巴巴电商、泛娱乐、物流等大规模业务验证,通过API/SDK为用户提供视觉类算法服务,帮助用户快速集成,实现数字化、智能化转型和产品创新。
目前公测阶段提供万物识别:车型识别、动物识别和植物识别,商品理解:商品标签和商品分类 五款API接口。下面基于阿里云Java Core SDK介绍相关接口的调用。
测试
1、服务开通
2、服务调用
- 2.1 pom.xml
<dependency>
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>com.aliyun<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span></span>
<artifactId>aliyun-java-sdk-core<<span class="hljs-regexp">/artifactId>
<version>[4.4.2,5.0.0)</version>
</dependency></span></code></pre>
2.2 code sample
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class Demo1 {
<span class="hljs-comment">//DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret https://yq.aliyun.com/articles/693979</span>
public <span class="hljs-keyword">static</span> DefaultProfile profile = DefaultProfile.getProfile(<span class="hljs-string">"cn-beijing"</span>, <span class="hljs-string">"LTAIOZZg********"</span>, <span class="hljs-string">"v7CjUJCMk7j9aKduMAQLjy********"</span>);
public <span class="hljs-keyword">static</span> DefaultAcsClient client = <span class="hljs-keyword">new</span> DefaultAcsClient(profile);
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(<span class="hljs-built_in">String</span>[] args) throws IOException {
<span class="hljs-comment">// 万物理解</span>
<span class="hljs-built_in">String</span> animalPicUrl = <span class="hljs-string">"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1577683988&di=940085ab7a77bbbe2f796bd2ad8e3ba2&src=http://image.biaobaiju.com/uploads/20180803/20/1533300579-gnUBlQZPbt.jpg"</span>;
<span class="hljs-built_in">String</span> vehiclePicUrl = <span class="hljs-string">"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577695567568&di=0db0a43cf0579e8050a451cdb5a5bbee&imgtype=0&src=http%3A%2F%2Fimg.kutoo8.com%2Fupload%2Fimage%2F36015894%2F1367481408093666_960x540.jpg"</span>;
<span class="hljs-built_in">String</span> plantPicUrl = <span class="hljs-string">"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577695588307&di=0ecab47da106889bed43dfc75065b619&imgtype=0&src=http%3A%2F%2Fpro.user.img32.51sole.com%2FproductImages3%2F20140405%2F1292497_20140405225052.jpg"</span>;
<span class="hljs-built_in">String</span> animalAction = <span class="hljs-string">"IdentifyAnimal"</span>;
<span class="hljs-built_in">String</span> vehicleAction = <span class="hljs-string">"RecognizeVehicle"</span>;
<span class="hljs-built_in">String</span> plantAction = <span class="hljs-string">"IdentifyPlant"</span>;
Identify(animalAction, animalPicUrl); <span class="hljs-comment">//动物识别</span>
Identify(vehicleAction,vehiclePicUrl); <span class="hljs-comment">// 车型识别</span>
Identify(plantAction, plantPicUrl); <span class="hljs-comment">// 植物识别</span>
<span class="hljs-comment">// 商品理解</span>
<span class="hljs-built_in">String</span> prodmlabelPicUrl = <span class="hljs-string">"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=21055346,2142400964&fm=26&gp=0.jpg"</span>;
<span class="hljs-built_in">String</span> prodcategoryPicUrl = <span class="hljs-string">"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=21055346,2142400964&fm=26&gp=0.jpg"</span>;
<span class="hljs-built_in">String</span> prodcategoryAction = <span class="hljs-string">"RecognizeProdcategory"</span>;
<span class="hljs-built_in">String</span> prodmlabelAction = <span class="hljs-string">"RecognizeProdmlabel"</span>;
Identify(prodcategoryAction, prodcategoryPicUrl); <span class="hljs-comment">// 商品分类</span>
Identify(prodmlabelAction, prodmlabelPicUrl); <span class="hljs-comment">// 商品标签</span>
}
<span class="hljs-comment">/***
* 识别统一接口
* @param imageUrl 待检测图片URL
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> Identify(<span class="hljs-built_in">String</span> Action, <span class="hljs-built_in">String</span> imageUrl) throws IOException {
CommonRequest request = <span class="hljs-keyword">new</span> CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain(<span class="hljs-string">"visionai.cn-beijing.aliyuncs.com"</span>);
request.setVersion(<span class="hljs-string">"2019-10-24"</span>);
request.setAction(Action);
request.setRegionId(<span class="hljs-string">"cn-beijing"</span>);
Base64 base64 = <span class="hljs-keyword">new</span> Base64();
<span class="hljs-built_in">String</span> encodePicContent = base64.encodeToString(getFileFromUrl(imageUrl));
request.putBodyParameter(<span class="hljs-string">"ImageContent"</span>, encodePicContent);
CommonResponse response = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">try</span> {
response = client.getCommonResponse(request);
} <span class="hljs-keyword">catch</span> (ClientException e) {
e.printStackTrace();
}
System.out.println(response.getData());
}
<span class="hljs-comment">/**
* 通过oss 中的URL的读取图片到Byte[]
* @param url 公网可以访问的图片URL
* @return byte[]
* @throws IOException
*/</span>
private <span class="hljs-keyword">static</span> byte[] getFileFromUrl(<span class="hljs-built_in">String</span> url) throws IOException{
URL urlConet = <span class="hljs-keyword">new</span> URL(url);
HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();
con.setRequestMethod(<span class="hljs-string">"GET"</span>);
con.setConnectTimeout(<span class="hljs-number">4</span> * <span class="hljs-number">1000</span>);
InputStream inStream = con .getInputStream();<span class="hljs-comment">//通过输入流获取图片数据</span>
ByteArrayOutputStream outStream = <span class="hljs-keyword">new</span> ByteArrayOutputStream();
byte[] buffer = <span class="hljs-keyword">new</span> byte[<span class="hljs-number">2048</span>];
int len = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span>( (len=inStream.read(buffer)) != <span class="hljs-number">-1</span> ){
outStream.write(buffer, <span class="hljs-number">0</span>, len);
}
inStream.close();
byte[] data = outStream.toByteArray();
<span class="hljs-keyword">return</span> data;
}
}
2.3 test result
{"Data":{"Name":"狸花猫","Description":"狸花猫的原产地是中国,属于自然猫,是在千百年中经过许多品种的自然淘汰而保留下来的品种。人们最熟悉的就要算是“狸猫换太子”(宋朝)的故事了,这也是能够找到的最早有关于狸花猫的记录了,所以,狸花猫就有了一个这样的英文名字DragonLi,由CFA猫会授予。它非常受百姓们喜欢,因为它有漂亮、厚实的皮毛,健康的身体。容易喂养,并且对捕捉老鼠十分在行。","SpuId":"51","Score":0.7072075},"RequestId":"3A439130-04A7-48B5-BC23-D4FC2BFBF97E"}
{"Data":{"Name":"奔驰 CLS级","Description":"德国戴姆勒集团旗下品牌梅赛德斯-奔驰在2004年第74届日内瓦车展上揭露一个全新的车系:CLS级。这种新的4门轿车将双门轿跑车的经典比例,具有感染力的外表以及豪华轿车的舒适以及功能性完美的结合在一起。完美继承奔驰车一贯的风采。","SpuId":2641,"Score":0.9904089},"RequestId":"60A794F2-2889-4946-8E07-D9BECA715132"}
{"Data":{"Name":"荷花玉兰","Description":"荷花玉兰(学名:Magnolia grandiflora L):常绿乔木,在原产地高达30米;树皮淡褐色或灰色,薄鳞片状开裂;小枝粗壮。叶厚革质,椭圆形,长圆状椭圆形或倒卵状椭圆形,叶面深绿色,有光泽。花白色,有芳香,直径15-20厘米;花被片9-12,厚肉质,倒卵形,长6-10厘米,宽5-7厘米。聚合果圆柱状长圆形或卵圆形,蓇葖背裂,背面圆,顶端外侧具长喙;种子近卵圆形或卵形,长约14毫米,径约6毫米,外种皮红色,除去外种皮的种子,顶端延长成短颈。花期5-6月,果期9-10月。原产北美洲东南部。中国长江流域以南各城市有栽培。兰州及北京公园也有栽培。该种广泛栽培,超过150个栽培品系。花大,白色,状如荷花,芳香,为美丽的庭园绿化观赏树种,适生于湿润肥沃土壤,对二氧化硫、氯气、氟化氢等有毒气体抗性较强;也耐烟尘。木材黄白色,材质坚重,可供装饰材用。叶、幼枝和花可提取芳香油;花制浸膏用。叶入药治高血压。种子榨油,含油率42.5%。(概述图片参考资料来源: )","SpuId":129,"Score":0.9992467},"RequestId":"8FA0E710-CC9C-435E-840E-2E7575DDFAB6"}
{"Data":[{"CategoryName":"靴子","CategoryId":50011743,"Score":0.535344},{"CategoryName":"户外休闲鞋","CategoryId":50019279,"Score":0.338715},{"CategoryName":"登山鞋/徒步鞋","CategoryId":50019272,"Score":0.027337},{"CategoryName":"高帮鞋","CategoryId":50012907,"Score":0.026892},{"CategoryName":"滑雪鞋/雪地靴","CategoryId":50019278,"Score":0.01566}],"RequestId":"97185D6B-1872-4409-BCB0-3A27BD2A13DD"}
{"Data":[{"PropertyValue":"高帮","PropertyId":122216523,"SubId":"63","PropertyName":"鞋帮高度","Score":1.0},{"PropertyValue":"男","PropertyId":122216608,"SubId":"1570","PropertyName":"适用对象","Score":0.9797},{"PropertyValue":"系带","PropertyId":20490,"SubId":"4371","PropertyName":"闭合方式","Score":0.9748},{"PropertyValue":"青年","PropertyId":122216608,"SubId":"1581","PropertyName":"适用对象","Score":0.9028},{"PropertyValue":"圆头","PropertyId":122216351,"SubId":"47","PropertyName":"鞋头款式","Score":0.8999}],"RequestId":"E821C6CD-7871-4D53-BF81-EF5A3215C1AE"}
更多参考