阿里云Ai训练营DayThree

主题

今天的主题是制作AI识别相册.

实例截图

主要是实现对图片内容的解析识别,然后提取标签做一个双向分类.

主要依赖项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
        <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.67</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun/facebody -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>facebody</artifactId>
<version>0.0.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.aliyun/imagerecog -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imagerecog</artifactId>
<version>0.0.5</version>
</dependency>

前端是由简单的Vue来实现的.

这里没有用SSR这些后端渲染的方法来实现,而是使用AXIOS来实现数据请求

主要逻辑

在web端上传图片,通过upload组件将图片传到后端,后端将图片通过阿里云SDK传到对应的处理服务,然后获取识别结果,存入到本地,再做对前台的响应.

当前端点击网页上的标签时就会发起请求,后端服务从存在本地的JSON文件读取出数据,再进行响应.

主要业务代码分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public List<String> recognizeScene(InputStream inputStream) throws Exception {
// 实例化图像识别请求
RecognizeSceneAdvanceRequest request = new RecognizeSceneAdvanceRequest();
// 将图像流挂载到请求上
request.imageURLObject = inputStream;
// 实例化一个标签缓冲区
List<String> labels = new ArrayList<>();
try {
// 实例化图像识别请求client
com.aliyun.imagerecog.Client client = getImageRecogClient(imageRecogEndpoint);
// 获取识别结果
RecognizeSceneResponse resp = client.recognizeSceneAdvance(request, new RuntimeObject());
for (RecognizeSceneResponse.RecognizeSceneResponseDataTags tag: resp.data.tags) {
// 将识别结果加入到缓冲区中
labels.add(tag.value);
}
} catch (ClientException e) {
// 错误处理
log.error("ErrCode:{}, ErrMsg:{}, RequestId:{}", e.getErrCode(), e.getErrMsg(), e.getRequestId());
}
return labels;
} labels;
}

上面的代码实现了标签提取,并加入了链表.

其他部分的业务代码大多类似雷同,不再赘述

Author: TankNee
Link: https://www.tanknee.cn/2020/06/06/aliyunAiDayThree/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.