Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
石磊
/
cihai
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 3fbd46dc
authored
Dec 07, 2017
by
石头
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
整合词条页面
1 parent
2fb7eb7f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
78 additions
and
32 deletions
serverside/cihai-core/src/main/java/com/dookay/cihai/core/aip/AipImageClassifyClient.java
serverside/cihai-core/src/main/java/com/dookay/cihai/core/aip/AipWordUtilBean.java
serverside/cihai-core/src/main/java/com/dookay/cihai/core/word/query/WordQuery.java
serverside/cihai-wechat/src/main/java/com/dookay/cihai/wechat/controller/HomeController.java
serverside/cihai-wechat/src/main/webapp/WEB-INF/jsp/portal/home/index.jsp
serverside/cihai-core/src/main/java/com/dookay/cihai/core/aip/AipImageClassifyClient.java
View file @
3fbd46d
...
...
@@ -19,7 +19,6 @@ package com.dookay.cihai.core.aip;
import
com.alibaba.fastjson.JSON
;
import
com.baidu.aip.imageclassify.AipImageClassify
;
import
com.baidu.aip.util.Base64Util
;
import
com.dookay.cihai.core.aip.config.AipProperties
;
import
com.dookay.cihai.core.aip.enums.ImageInputTypeEnum
;
import
com.dookay.cihai.core.aip.model.ImageResult
;
import
com.dookay.coral.common.core.exception.ServiceException
;
...
...
@@ -32,13 +31,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Base64Utils
;
import
java.util.ArrayList
;
import
java.util.Comparator
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.*
;
/**
* 图片识别客户端
*
* @author Luxor
* @version v0.0.1
* @since 2017/12/7
...
...
@@ -54,19 +51,20 @@ public class AipImageClassifyClient {
/**
* 获取动物图片识别结果列表,可能会有多个结果
*
* @param
* @author luxor
* @date 2017/12/7
*/
private
List
<
ImageResult
>
getAnimalDetect
(
String
image
,
ImageInputTypeEnum
typeEnum
)
throws
JSONException
{
private
List
<
ImageResult
>
getAnimalDetect
(
String
image
,
ImageInputTypeEnum
typeEnum
)
throws
JSONException
{
// 初始化一个AipImageClassifyClient
AipImageClassify
client
=
new
AipImageClassify
(
APP_ID
,
API_KEY
,
SECRET_KEY
);
JSONObject
res
;
// 参数为本地图片路径
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
res
=
client
.
animalDetect
(
image
,
new
HashMap
<
String
,
String
>());
}
else
{
}
else
{
byte
[]
file
=
Base64Utils
.
decodeFromString
(
image
);
res
=
client
.
animalDetect
(
file
,
new
HashMap
<
String
,
String
>());
log
.
info
(
image
);
...
...
@@ -78,40 +76,42 @@ public class AipImageClassifyClient {
/**
* 获取动物图片识别结果列表,可能会有多个结果
*
* @param
* @author luxor
* @date 2017/12/7
*/
private
List
<
ImageResult
>
getPlantDetect
(
String
image
,
ImageInputTypeEnum
typeEnum
)
throws
JSONException
{
private
List
<
ImageResult
>
getPlantDetect
(
String
image
,
ImageInputTypeEnum
typeEnum
)
throws
JSONException
{
// 初始化一个AipImageClassifyClient
AipImageClassify
client
=
new
AipImageClassify
(
"10492760"
,
"TPo1FLFn3cIuEAyvsjG2jpC4"
,
"YUGcqkGioxrceK4CgD3wB90vmmG7KzRf"
);
JSONObject
res
;
// 参数为本地图片路径
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
res
=
client
.
plantDetect
(
image
,
new
HashMap
<
String
,
String
>());
}
else
{
}
else
{
byte
[]
file
=
Base64Utils
.
decodeFromString
(
image
);
res
=
client
.
plantDetect
(
file
,
new
HashMap
<
String
,
String
>());
log
.
info
(
image
);
}
// 调用接口
return
parseResult
(
res
);
return
parseResult
(
res
);
}
/**
* 处理返回结果
*
* @param
* @author luxor
* @date 2017/12/7
*/
private
List
<
ImageResult
>
parseResult
(
JSONObject
res
)
throws
JSONException
{
if
(
res
.
has
(
"error_code"
))
{
log
.
info
(
"图像识别错误:"
+
res
.
toString
(
2
));
throw
new
ServiceException
(
"图像识别错误:"
+
res
.
toString
(
2
));
}
else
{
private
List
<
ImageResult
>
parseResult
(
JSONObject
res
)
throws
JSONException
{
if
(
res
.
has
(
"error_code"
))
{
log
.
info
(
"图像识别错误:"
+
res
.
toString
(
2
));
throw
new
ServiceException
(
"图像识别错误:"
+
res
.
toString
(
2
));
}
else
{
List
<
ImageResult
>
imageResultList
=
new
ArrayList
<>();
JSONArray
jsonArray
=
res
.
getJSONArray
(
"result"
);
for
(
int
i
=
0
;
i
<
jsonArray
.
length
();
i
++)
{
...
...
@@ -127,23 +127,25 @@ public class AipImageClassifyClient {
/**
* 获取单个图片识别结果,根据置信度从高到底排序
*
* @param
* @author luxor
* @date 2017/12/7
*/
public
ImageResult
getImageResult
(
String
imageBase64
)
throws
JSONException
{
if
(
StringUtils
.
isBlank
(
imageBase64
))
{
public
ImageResult
getImageResult
(
String
imageBase64
)
throws
JSONException
{
if
(
StringUtils
.
isBlank
(
imageBase64
))
{
return
new
ImageResult
();
}
String
image
=
imageBase64
.
replace
(
"data:image/png;base64,"
,
""
)
.
replace
(
"data:image/jpg;base64,"
,
""
)
.
replace
(
"data:image/jpeg;base64,"
,
""
);
log
.
info
(
"image:"
+
image
);
List
<
ImageResult
>
imageResultList
=
this
.
getAnimalDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
List
<
ImageResult
>
imageResultList2
=
this
.
getPlantDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
String
image
=
imageBase64
.
replace
(
"data:image/png;base64,"
,
""
)
.
replace
(
"data:image/jpg;base64,"
,
""
)
.
replace
(
"data:image/jgp;base64,"
,
""
)
.
replace
(
"data:image/jpeg;base64,"
,
""
);
log
.
info
(
"image:"
+
image
);
List
<
ImageResult
>
imageResultList
=
this
.
getAnimalDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
List
<
ImageResult
>
imageResultList2
=
this
.
getPlantDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
imageResultList
.
addAll
(
imageResultList2
);
log
.
info
(
JSON
.
toJSONString
(
imageResultList
));
return
imageResultList
.
stream
().
filter
(
x
->
x
.
getScore
()<
0.99
).
sorted
(
Comparator
.
comparing
(
ImageResult:
:
getScore
).
reversed
()).
findFirst
().
orElse
(
new
ImageResult
());
return
imageResultList
.
stream
().
filter
(
x
->
!
Objects
.
equals
(
x
.
getName
(),
"非动物"
)
).
sorted
(
Comparator
.
comparing
(
ImageResult:
:
getScore
).
reversed
()).
findFirst
().
orElse
(
new
ImageResult
());
}
}
serverside/cihai-core/src/main/java/com/dookay/cihai/core/aip/AipWordUtilBean.java
View file @
3fbd46d
...
...
@@ -250,7 +250,7 @@ public final class AipWordUtilBean {
* @author houkun
* @date 2017/12/6
*/
p
rivate
List
<
LexerItem
>
getLexerItems
(
String
document
)
throws
JSONException
{
p
ublic
List
<
LexerItem
>
getLexerItems
(
String
document
)
throws
JSONException
{
List
<
String
>
documents
=
splitDocument
(
document
,
10000
);
List
<
LexerItem
>
lexerItems
=
new
ArrayList
<>();
for
(
String
s
:
documents
)
{
...
...
serverside/cihai-core/src/main/java/com/dookay/cihai/core/word/query/WordQuery.java
View file @
3fbd46d
...
...
@@ -23,7 +23,8 @@ public class WordQuery extends Query {
QueryCriteria
queryCriteria
=
new
QueryCriteria
(
WordDomain
.
class
);
Example
.
Criteria
criteria
=
queryCriteria
.
createCriteria
();
if
(
valid
(
keyword
))
{
String
str
=
"%"
+
keyword
+
"%"
;
criteria
.
andCondition
(
String
.
format
(
"(word like '%s' or CONTAINS(%s,word))"
,
str
,
keyword
));
}
//todo 写查询逻辑
return
queryCriteria
;
...
...
serverside/cihai-wechat/src/main/java/com/dookay/cihai/wechat/controller/HomeController.java
View file @
3fbd46d
...
...
@@ -14,13 +14,18 @@
package
com
.
dookay
.
cihai
.
wechat
.
controller
;
import
com.dookay.cihai.core.aip.AipImageClassifyClient
;
import
com.dookay.cihai.core.aip.AipWordUtilBean
;
import
com.dookay.cihai.core.aip.model.ImageResult
;
import
com.dookay.cihai.core.theme.domain.ThemeDomain
;
import
com.dookay.cihai.core.theme.query.ThemeQuery
;
import
com.dookay.cihai.core.theme.service.IThemeService
;
import
com.dookay.cihai.core.word.domain.WordDomain
;
import
com.dookay.cihai.core.word.query.CustomDictionaryQuery
;
import
com.dookay.cihai.core.word.query.WordQuery
;
import
com.dookay.cihai.core.word.service.ICustomDictionaryService
;
import
com.dookay.cihai.core.word.service.IWordService
;
import
com.dookay.coral.common.web.constant.MediaTypes
;
import
com.dookay.coral.common.web.controller.BaseController
;
import
com.dookay.coral.common.web.response.JsonResult
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -49,6 +54,9 @@ public class HomeController extends BaseController {
@Autowired
private
AipWordUtilBean
aipWordUtilBean
;
@Autowired
private
AipImageClassifyClient
aipImageClassifyClient
;
/**
* @author 石磊
* @date 2017/12/6
...
...
@@ -82,11 +90,36 @@ public class HomeController extends BaseController {
ThemeQuery
query
=
new
ThemeQuery
();
query
.
setKeyword
(
result
);
ThemeDomain
themeDomain
=
themeService
.
getFirst
(
query
);
if
(
themeDomain
==
null
)
return
errorResult
(
String
.
format
(
"暂无\"%s\"检索结果"
,
keyword
));
if
(
themeDomain
==
null
)
return
errorResult
(
String
.
format
(
"暂无\"%s\"检索结果"
,
keyword
));
System
.
out
.
println
(
themeDomain
);
return
successResult
(
"success"
,
"/theme/detail/"
+
themeDomain
.
getId
());
}
catch
(
Exception
ex
)
{
return
errorResult
(
String
.
format
(
"暂无\"%s\"检索结果"
,
keyword
));
return
errorResult
(
String
.
format
(
"暂无\"%s\"检索结果"
,
keyword
));
}
}
/**
* @author 石磊
* @date 2017/12/7
* @description 图片搜索
*/
@RequestMapping
(
value
=
"/image/search"
,
method
=
RequestMethod
.
POST
,
produces
=
MediaTypes
.
JSON_UTF_8
)
@ResponseBody
public
JsonResult
imageSearch
(
String
image
)
{
System
.
out
.
println
(
"进入图片检索"
);
try
{
ImageResult
imageResult
=
aipImageClassifyClient
.
getImageResult
(
image
);
System
.
out
.
println
(
imageResult
);
String
name
=
imageResult
.
getName
();
System
.
out
.
println
(
name
);
WordQuery
wordQuery
=
new
WordQuery
();
wordQuery
.
setKeyword
(
name
);
WordDomain
wordDomain
=
wordService
.
getFirst
(
wordQuery
);
if
(
wordDomain
==
null
)
return
errorResult
(
"暂无检索结果"
);
return
successResult
(
"success"
,
"/word/baike/"
+
wordDomain
.
getId
());
}
catch
(
Exception
ex
)
{
return
errorResult
(
"暂无检索结果"
);
}
}
...
...
serverside/cihai-wechat/src/main/webapp/WEB-INF/jsp/portal/home/index.jsp
View file @
3fbd46d
...
...
@@ -105,9 +105,19 @@
localId: localIds[0], // 图片的localID
success: function (res) {
myApp.dialog.preloader();
var localData = res.localData; // localData是图片的base64数据,可以用img标签显示
alert(localData);
jQuery.post("/image/search",{image:localData},function (data) {
myApp.dialog.close();
if (data.code == "OK") {
// myApp.view.main.router.load({
// url:data.data
// });
location.href = data.data;
} else {
myApp.dialog.alert(data.message);
}
})
}
});
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment