Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
A
Afrishop refactored project
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Whispa
Afrishop refactored project
Commits
203af447
提交
203af447
authored
10月 14, 2020
作者:
Whispa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit
上级
4a24139c
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
150 行增加
和
47 行删除
+150
-47
ImageSearchController.java
...xample/afrishop_v3/controllers/ImageSearchController.java
+86
-47
ItemLabel.java
src/main/java/com/example/afrishop_v3/models/ItemLabel.java
+38
-0
TbCfStationItem.java
.../java/com/example/afrishop_v3/models/TbCfStationItem.java
+16
-0
ItemLabelRepository.java
...m/example/afrishop_v3/repository/ItemLabelRepository.java
+8
-0
TbCfStationItemRepository.java
...ple/afrishop_v3/repository/TbCfStationItemRepository.java
+2
-0
没有找到文件。
src/main/java/com/example/afrishop_v3/controllers/ImageSearchController.java
浏览文件 @
203af447
...
...
@@ -2,68 +2,108 @@ package com.example.afrishop_v3.controllers;
import
com.example.afrishop_v3.base.Result
;
import
com.example.afrishop_v3.enums.ResultCodeEnum
;
import
com.google.api.client.util.IOUtils
;
import
com.example.afrishop_v3.models.ItemLabel
;
import
com.example.afrishop_v3.models.Post
;
import
com.example.afrishop_v3.models.TbCfStationItem
;
import
com.example.afrishop_v3.repository.ItemLabelRepository
;
import
com.example.afrishop_v3.repository.TbCfStationItemRepository
;
import
com.google.cloud.vision.v1.*
;
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.RequestParam
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.servlet.mvc.support.RedirectAttributes
;
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.
Optional
;
import
java.util.stream.Collectors
;
@RestController
@RequestMapping
(
"search/image"
)
public
class
ImageSearchController
extends
Controller
{
// Imports the Google Cloud client library
private
final
TbCfStationItemRepository
repository
;
private
final
ItemLabelRepository
labelRepository
;
p
rivate
final
CloudVisionTemplate
cloudVisionTemplate
;
p
ublic
ImageSearchController
(
TbCfStationItemRepository
repository
,
ItemLabelRepository
labelRepository
)
{
private
final
ResourceLoader
resourceLoader
;
this
.
repository
=
repository
;
this
.
labelRepository
=
labelRepository
;
}
public
ImageSearchController
(
CloudVisionTemplate
cloudVisionTemplate
,
ResourceLoader
resourceLoader
)
{
this
.
cloudVisionTemplate
=
cloudVisionTemplate
;
this
.
resourceLoader
=
resourceLoader
;
@GetMapping
public
Result
main
(
@RequestParam
(
"url"
)
String
url
)
{
// try {
// authExplicit();
// } catch (IOException e) {
// e.printStackTrace();
// }
return
new
Result
<>(
getItemLabels
(
url
));
}
@GetMapping
public
Result
main
(
@RequestParam
(
"url"
)
String
url
)
throws
Exception
{
//authExplicit();
try
(
ImageAnnotatorClient
vision
=
ImageAnnotatorClient
.
create
())
{
@PostMapping
(
"/upload"
)
public
Result
handleFileUpload
(
@RequestParam
(
"file"
)
MultipartFile
file
,
@RequestParam
(
value
=
"pageNo"
,
defaultValue
=
"0"
,
required
=
false
)
Integer
pageNo
,
@RequestParam
(
value
=
"pageSize"
,
required
=
false
,
defaultValue
=
"12"
)
Integer
pageSize
){
if
(
file
!=
null
){
try
{
List
<
ItemLabel
>
itemLabels
=
getItemLabels
(
file
.
getBytes
());
List
<
String
>
collect
=
itemLabels
.
stream
().
map
(
ItemLabel:
:
getDescription
).
collect
(
Collectors
.
toList
());
String
[]
strings
=
collect
.
toArray
(
new
String
[]{});
Page
<
TbCfStationItem
>
itemsByImageSearch
=
repository
.
getItemsByImageSearch
(
strings
,
PageRequest
.
of
(
pageNo
,
pageSize
));
return
new
Result
<>(
itemsByImageSearch
.
toList
());
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
}
}
return
new
Result
<>(
new
ArrayList
<>());
}
@GetMapping
(
"indexing"
)
public
Result
itemIndexing
(
@RequestParam
(
"itemId"
)
String
itemId
)
{
Optional
<
TbCfStationItem
>
byId
=
repository
.
findById
(
itemId
);
if
(
byId
.
isPresent
()
){
TbCfStationItem
stationItem
=
byId
.
get
();
if
(
labelRepository
.
existsByItemItemId
(
itemId
)
)
return
new
Result
<>(
stationItem
.
getLabelList
());
// The path to the image file to annotate
String
fileName
=
"./resources/wakeupcat.jpg"
;
String
itemImg
=
stationItem
.
getItemImg
();
String
[]
strings
=
itemImg
!=
null
?
itemImg
.
split
(
";"
)
:
new
String
[]{};
String
img
=
strings
.
length
>
0
?
strings
[
0
]
:
itemImg
;
if
(
img
!=
null
)
{
List
<
ItemLabel
>
itemLabels
=
getItemLabels
(
img
);
stationItem
.
setLabelList
(
itemLabels
);
repository
.
save
(
stationItem
);
return
new
Result
<>(
itemLabels
);
}
}
return
new
Result
<>(
ResultCodeEnum
.
SERVICE_ERROR
.
getCode
());
}
// Reads the image file into memory
Path
path
=
Paths
.
get
(
fileName
);
private
List
<
ItemLabel
>
getItemLabels
(
String
url
){
RestTemplate
restTemplate
=
new
RestTemplate
();
byte
[]
imageBytes
=
restTemplate
.
getForObject
(
url
,
byte
[].
class
);
return
this
.
getItemLabels
(
imageBytes
);
}
private
List
<
ItemLabel
>
getItemLabels
(
byte
[]
imageBytes
)
{
//authExplicit();
try
(
ImageAnnotatorClient
vision
=
ImageAnnotatorClient
.
create
())
{
ByteString
imgBytes
=
ByteString
.
copyFrom
(
imageBytes
);
// Builds the image annotation request
...
...
@@ -77,27 +117,26 @@ public class ImageSearchController extends Controller {
// Performs label detection on the image file
BatchAnnotateImagesResponse
response
=
vision
.
batchAnnotateImages
(
requests
);
List
<
AnnotateImageResponse
>
responses
=
response
.
getResponsesList
();
List
<
Map
<
String
,
Object
>
>
list
=
new
ArrayList
<>();
List
<
ItemLabel
>
list
=
new
ArrayList
<>();
for
(
AnnotateImageResponse
res
:
responses
)
{
if
(
res
.
hasError
())
{
System
.
out
.
format
(
"Error: %s%n"
,
res
.
getError
().
getMessage
());
return
new
Result
(
ResultCodeEnum
.
VALIDATE_ERROR
.
getCode
()
);
return
new
ArrayList
<>(
);
}
for
(
EntityAnnotation
annotation
:
res
.
getLabelAnnotationsList
())
{
Map
<
String
,
Object
>
map
=
new
LinkedHashMap
<>();
annotation
.
getAllFields
()
.
forEach
((
k
,
v
)
->
map
.
put
(
k
.
getJsonName
(),
v
));
list
.
add
(
map
);
}
for
(
EntityAnnotation
ann
:
res
.
getLabelAnnotationsList
())
{
return
new
Result
<>(
list
);
list
.
add
(
new
ItemLabel
(
ann
.
getScore
(),
ann
.
getTopicality
(),
ann
.
getDescription
(),
ann
.
getMid
())
);
}
return
list
;
}
return
new
Result
<>(
new
ArrayList
<>());
}
catch
(
IOException
e
)
{
return
new
ArrayList
<>();
}
return
new
ArrayList
<>();
}
}
src/main/java/com/example/afrishop_v3/models/ItemLabel.java
0 → 100644
浏览文件 @
203af447
package
com
.
example
.
afrishop_v3
.
models
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
lombok.Data
;
import
javax.persistence.Entity
;
import
javax.persistence.ManyToOne
;
@Entity
@Data
public
class
ItemLabel
extends
Model
{
@JsonIgnore
@ManyToOne
private
TbCfStationItem
item
;
private
String
description
;
private
double
score
;
private
double
topicality
;
private
String
mid
;
public
ItemLabel
(){
}
public
ItemLabel
(
double
score
,
double
topicality
,
String
description
,
String
mid
){
this
.
score
=
score
;
this
.
topicality
=
topicality
;
this
.
description
=
description
;
this
.
mid
=
mid
;
}
public
String
getDescription
()
{
return
description
;
}
}
src/main/java/com/example/afrishop_v3/models/TbCfStationItem.java
浏览文件 @
203af447
...
...
@@ -8,7 +8,9 @@ import org.hibernate.annotations.Where;
import
javax.persistence.*
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
* 站点商品实体
...
...
@@ -130,10 +132,19 @@ public class TbCfStationItem {
private
int
totalScore
;
@JsonIgnore
@OneToMany
(
mappedBy
=
"item"
)
private
List
<
ItemLabel
>
labelList
=
new
ArrayList
<>();
public
TbCfExpressTemplate
getExpress
()
{
return
express
;
}
public
List
<
ItemLabel
>
getLabelList
()
{
return
labelList
;
}
/**
* 设置:商品id
*/
...
...
@@ -141,6 +152,11 @@ public class TbCfStationItem {
this
.
itemId
=
itemId
;
}
public
void
setLabelList
(
List
<
ItemLabel
>
labelList
)
{
this
.
labelList
=
labelList
;
}
/**
* 获取:商品id
*/
...
...
src/main/java/com/example/afrishop_v3/repository/ItemLabelRepository.java
0 → 100644
浏览文件 @
203af447
package
com
.
example
.
afrishop_v3
.
repository
;
import
com.example.afrishop_v3.models.ItemLabel
;
import
org.springframework.data.repository.PagingAndSortingRepository
;
public
interface
ItemLabelRepository
extends
PagingAndSortingRepository
<
ItemLabel
,
String
>
{
boolean
existsByItemItemId
(
String
itemId
);
}
src/main/java/com/example/afrishop_v3/repository/TbCfStationItemRepository.java
浏览文件 @
203af447
...
...
@@ -16,6 +16,8 @@ public interface TbCfStationItemRepository extends PagingAndSortingRepository<Tb
Page
<
TbCfStationItem
>
findAllByItemLabelContaining
(
String
label
,
Pageable
pageable
);
@Query
(
value
=
"select a from #{#entityName} a WHERE a.itemId <> :itemId and (a.itemDescritionId = :descriptionId or a.itemCategorytwo = :categoryTwo or a.itemCategory = :category )"
)
Page
<
TbCfStationItem
>
getRecommendItems
(
@Param
(
"itemId"
)
String
itemId
,
@Param
(
"descriptionId"
)
String
descriptionId
,
@Param
(
"categoryTwo"
)
String
categoryTwo
,
@Param
(
"category"
)
String
category
,
Pageable
pageable
);
@Query
(
value
=
"select a from #{#entityName} a WHERE a in (SELECT l.item FROM ItemLabel l WHERE l.description IN :tags)"
)
Page
<
TbCfStationItem
>
getItemsByImageSearch
(
@Param
(
"tags"
)
String
[]
tags
,
Pageable
pageable
);
Page
<
TbCfStationItem
>
findAllByItemNameContainingOrItemTagsContaining
(
String
itemName
,
String
itemTags
,
Pageable
pageable
);
Page
<
TbCfStationItem
>
findByItemCategory
(
String
itemName
,
Pageable
pageable
);
List
<
TbCfStationItem
>
findAllByItemDescritionId
(
String
id
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论