阿里云智能视觉AI Quick Start

概述

智能视觉AI开放平台是面向企业用户、软硬件服务商和开发者,提供简单、易用、优质视觉算法的开放平台。平台算法能力由阿里巴巴达摩院机器智能实验室以及集团内多个视觉团队多年研发沉淀,并经过阿里巴巴电商、泛娱乐、物流等大规模业务验证,通过API/SDK为用户提供视觉类算法服务,帮助用户快速集成,实现数字化、智能化转型和产品创新。

目前公测阶段提供万物识别:车型识别、动物识别和植物识别,商品理解:商品标签和商品分类 五款API接口。下面基于阿里云Java Core SDK介绍相关接口的调用。

测试

1、服务开通

服务开通

阿里云智能视觉AI Quick Start
2、服务调用

  • 2.1 pom.xml
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>[4.4.2,5.0.0)</version>
        </dependency>
  • 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 {

    //DefaultProfile.getProfile的参数分别是地域,access_key_id, access_key_secret https://yq.aliyun.com/articles/693979
    public static DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAIOZZg********", "v7CjUJCMk7j9aKduMAQLjy********");
    public static DefaultAcsClient client = new DefaultAcsClient(profile);

    public static void main(String[] args) throws IOException {

        // 万物理解
        String animalPicUrl = "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";
        String vehiclePicUrl = "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";
        String plantPicUrl = "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";
        String animalAction = "IdentifyAnimal";
        String vehicleAction = "RecognizeVehicle";
        String plantAction = "IdentifyPlant";

        Identify(animalAction, animalPicUrl); //动物识别
        Identify(vehicleAction,vehiclePicUrl); // 车型识别
        Identify(plantAction, plantPicUrl); // 植物识别

        // 商品理解
        String prodmlabelPicUrl = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=21055346,2142400964&fm=26&gp=0.jpg";
        String prodcategoryPicUrl = "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=21055346,2142400964&fm=26&gp=0.jpg";
        String prodcategoryAction = "RecognizeProdcategory";
        String prodmlabelAction = "RecognizeProdmlabel";

        Identify(prodcategoryAction, prodcategoryPicUrl); // 商品分类
        Identify(prodmlabelAction, prodmlabelPicUrl); // 商品标签

    }

    /***
     * 识别统一接口
     * @param imageUrl 待检测图片URL
     */
    public static void Identify(String Action, String imageUrl) throws IOException {
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("visionai.cn-beijing.aliyuncs.com");
        request.setVersion("2019-10-24");
        request.setAction(Action);
        request.setRegionId("cn-beijing");
        Base64 base64 = new Base64();
        String encodePicContent = base64.encodeToString(getFileFromUrl(imageUrl));
        request.putBodyParameter("ImageContent", encodePicContent);
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        System.out.println(response.getData());
    }

    /**
     * 通过oss 中的URL的读取图片到Byte[]
     * @param url 公网可以访问的图片URL
     * @return byte[]
     * @throws IOException
     */
    private static byte[] getFileFromUrl(String url) throws IOException{
        URL urlConet = new URL(url);
        HttpURLConnection con = (HttpURLConnection)urlConet.openConnection();
        con.setRequestMethod("GET");
        con.setConnectTimeout(4 * 1000);
        InputStream inStream = con .getInputStream();//通过输入流获取图片数据
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        byte[] data =  outStream.toByteArray();
        return 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"}

更多参考

API
Java SDK

上一篇:python 装饰器,自定义验证函数


下一篇:Spring Cloud Stream消费失败后的处理策略(一):自动重试