Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
f714af83
提交
f714af83
authored
10月 14, 2020
作者:
Whispa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit
上级
ee0f4554
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
52 行增加
和
50 行删除
+52
-50
pom.xml
pom.xml
+16
-7
Controller.java
.../java/com/example/afrishop_v3/controllers/Controller.java
+1
-0
ImageSearchController.java
...xample/afrishop_v3/controllers/ImageSearchController.java
+35
-43
没有找到文件。
pom.xml
浏览文件 @
f714af83
...
...
@@ -18,7 +18,23 @@
<java.version>
1.8
</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-gcp-dependencies
</artifactId>
<version>
1.2.5.RELEASE
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-gcp-starter-vision
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-jdbc
</artifactId>
...
...
@@ -54,13 +70,6 @@
<artifactId>
unirest-java
</artifactId>
<version>
1.4.9
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-vision -->
<dependency>
<groupId>
com.google.cloud
</groupId>
<artifactId>
google-cloud-vision
</artifactId>
<version>
1.100.2
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hashids/hashids -->
<dependency>
<groupId>
org.hashids
</groupId>
...
...
src/main/java/com/example/afrishop_v3/controllers/Controller.java
浏览文件 @
f714af83
...
...
@@ -45,6 +45,7 @@ abstract class Controller {
.
createScoped
(
Lists
.
newArrayList
(
"https://www.googleapis.com/auth/cloud-platform"
));
Storage
storage
=
StorageOptions
.
newBuilder
().
setCredentials
(
credentials
).
build
().
getService
();
System
.
out
.
println
(
"Buckets:"
);
Page
<
Bucket
>
buckets
=
storage
.
list
();
for
(
Bucket
bucket
:
buckets
.
iterateAll
())
{
...
...
src/main/java/com/example/afrishop_v3/controllers/ImageSearchController.java
浏览文件 @
f714af83
...
...
@@ -7,16 +7,21 @@ import com.google.cloud.vision.v1.Feature.Type;
import
com.google.protobuf.ByteString
;
import
com.google.protobuf.Descriptors
;
import
com.google.protobuf.GeneratedMessageV3
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.gcp.vision.CloudVisionTemplate
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.util.ArrayList
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
...
...
@@ -26,55 +31,42 @@ import java.util.stream.Collectors;
public
class
ImageSearchController
extends
Controller
{
// Imports the Google Cloud client library
private
final
CloudVisionTemplate
cloudVisionTemplate
;
@GetMapping
public
Result
main
()
throws
Exception
{
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
Result
errorDecoding
=
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
(),
"Error decoding"
);
authExplicit
();
try
(
ImageAnnotatorClient
vision
=
ImageAnnotatorClient
.
create
())
{
// The path to the image file to annotate
String
fileName
=
"./resources/wakeupcat.jpg"
;
private
final
ResourceLoader
resourceLoader
;
// Reads the image file into memory
//Path path = Paths.get(fileName);
public
ImageSearchController
(
CloudVisionTemplate
cloudVisionTemplate
,
ResourceLoader
resourceLoader
)
{
this
.
cloudVisionTemplate
=
cloudVisionTemplate
;
this
.
resourceLoader
=
resourceLoader
;
// try {
// authExplicit();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
InputStream
in
=
new
ClassPathResource
(
"Blue_Tshirt.jpg"
).
getInputStream
();
byte
[]
data
=
IOUtils
.
serialize
(
in
);
//byte[] data = Files.readAllBytes(path);
ByteString
imgBytes
=
ByteString
.
copyFrom
(
data
);
// Builds the image annotation request
List
<
AnnotateImageRequest
>
requests
=
new
ArrayList
<>();
Image
img
=
Image
.
newBuilder
().
setContent
(
imgBytes
).
build
();
Feature
feat
=
Feature
.
newBuilder
().
setType
(
Type
.
OBJECT_LOCALIZATION
).
build
();
AnnotateImageRequest
request
=
AnnotateImageRequest
.
newBuilder
().
addFeatures
(
feat
).
setImage
(
img
).
build
();
requests
.
add
(
request
);
@GetMapping
public
Result
main
()
throws
Exception
{
//authExplicit();
AnnotateImageResponse
response
=
this
.
cloudVisionTemplate
.
analyzeImage
(
this
.
resourceLoader
.
getResource
(
"Blue_Tshirt.jpg"
),
Type
.
LABEL_DETECTION
);
// Performs label detection on the image file
BatchAnnotateImagesResponse
response
=
vision
.
batchAnnotateImages
(
requests
);
List
<
AnnotateImageResponse
>
responses
=
response
.
getResponsesList
();
Map
<
String
,
Float
>
imageLabels
=
response
.
getLabelAnnotationsList
()
.
stream
()
.
collect
(
Collectors
.
toMap
(
EntityAnnotation:
:
getDescription
,
EntityAnnotation:
:
getScore
,
(
u
,
v
)
->
{
throw
new
IllegalStateException
(
String
.
format
(
"Duplicate key %s"
,
u
));
},
LinkedHashMap:
:
new
));
for
(
AnnotateImageResponse
res
:
responses
)
{
if
(
res
.
hasError
())
{
System
.
out
.
format
(
"Error: %s%n"
,
res
.
getError
().
getMessage
());
return
errorDecoding
;
}
List
<
Map
<
Descriptors
.
FieldDescriptor
,
Object
>>
collect
=
res
.
getLocalizedObjectAnnotationsList
().
stream
().
map
(
GeneratedMessageV3:
:
getAllFields
).
collect
(
Collectors
.
toList
());
// for (LocalizedObjectAnnotation annotation : res.getLocalizedObjectAnnotationsList()) {
// annotation
// .getAllFields()
// .forEach((k, v) -> System.out.format("%s : %s%n", k., v.toString()));
// }
return
new
Result
<>(
collect
);
}
}
return
errorDecoding
;
return
new
Result
<>(
imageLabels
);
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论