天天看點

阿裡雲智能視覺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