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
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
27 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;
...
@@ -19,7 +19,6 @@ package com.dookay.cihai.core.aip;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.baidu.aip.imageclassify.AipImageClassify
;
import
com.baidu.aip.imageclassify.AipImageClassify
;
import
com.baidu.aip.util.Base64Util
;
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.enums.ImageInputTypeEnum
;
import
com.dookay.cihai.core.aip.model.ImageResult
;
import
com.dookay.cihai.core.aip.model.ImageResult
;
import
com.dookay.coral.common.core.exception.ServiceException
;
import
com.dookay.coral.common.core.exception.ServiceException
;
...
@@ -32,13 +31,11 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -32,13 +31,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Base64Utils
;
import
org.springframework.util.Base64Utils
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.Comparator
;
import
java.util.HashMap
;
import
java.util.List
;
/**
/**
* 图片识别客户端
* 图片识别客户端
*
* @author Luxor
* @author Luxor
* @version v0.0.1
* @version v0.0.1
* @since 2017/12/7
* @since 2017/12/7
...
@@ -54,6 +51,7 @@ public class AipImageClassifyClient {
...
@@ -54,6 +51,7 @@ public class AipImageClassifyClient {
/**
/**
* 获取动物图片识别结果列表,可能会有多个结果
* 获取动物图片识别结果列表,可能会有多个结果
*
* @param
* @param
* @author luxor
* @author luxor
* @date 2017/12/7
* @date 2017/12/7
...
@@ -64,9 +62,9 @@ public class AipImageClassifyClient {
...
@@ -64,9 +62,9 @@ public class AipImageClassifyClient {
JSONObject
res
;
JSONObject
res
;
// 参数为本地图片路径
// 参数为本地图片路径
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
res
=
client
.
animalDetect
(
image
,
new
HashMap
<
String
,
String
>());
res
=
client
.
animalDetect
(
image
,
new
HashMap
<
String
,
String
>());
}
else
{
}
else
{
byte
[]
file
=
Base64Utils
.
decodeFromString
(
image
);
byte
[]
file
=
Base64Utils
.
decodeFromString
(
image
);
res
=
client
.
animalDetect
(
file
,
new
HashMap
<
String
,
String
>());
res
=
client
.
animalDetect
(
file
,
new
HashMap
<
String
,
String
>());
log
.
info
(
image
);
log
.
info
(
image
);
...
@@ -78,6 +76,7 @@ public class AipImageClassifyClient {
...
@@ -78,6 +76,7 @@ public class AipImageClassifyClient {
/**
/**
* 获取动物图片识别结果列表,可能会有多个结果
* 获取动物图片识别结果列表,可能会有多个结果
*
* @param
* @param
* @author luxor
* @author luxor
* @date 2017/12/7
* @date 2017/12/7
...
@@ -88,9 +87,9 @@ public class AipImageClassifyClient {
...
@@ -88,9 +87,9 @@ public class AipImageClassifyClient {
JSONObject
res
;
JSONObject
res
;
// 参数为本地图片路径
// 参数为本地图片路径
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
if
(
typeEnum
==
ImageInputTypeEnum
.
PATH
)
{
res
=
client
.
plantDetect
(
image
,
new
HashMap
<
String
,
String
>());
res
=
client
.
plantDetect
(
image
,
new
HashMap
<
String
,
String
>());
}
else
{
}
else
{
byte
[]
file
=
Base64Utils
.
decodeFromString
(
image
);
byte
[]
file
=
Base64Utils
.
decodeFromString
(
image
);
res
=
client
.
plantDetect
(
file
,
new
HashMap
<
String
,
String
>());
res
=
client
.
plantDetect
(
file
,
new
HashMap
<
String
,
String
>());
log
.
info
(
image
);
log
.
info
(
image
);
...
@@ -103,15 +102,16 @@ public class AipImageClassifyClient {
...
@@ -103,15 +102,16 @@ public class AipImageClassifyClient {
/**
/**
* 处理返回结果
* 处理返回结果
*
* @param
* @param
* @author luxor
* @author luxor
* @date 2017/12/7
* @date 2017/12/7
*/
*/
private
List
<
ImageResult
>
parseResult
(
JSONObject
res
)
throws
JSONException
{
private
List
<
ImageResult
>
parseResult
(
JSONObject
res
)
throws
JSONException
{
if
(
res
.
has
(
"error_code"
))
{
if
(
res
.
has
(
"error_code"
))
{
log
.
info
(
"图像识别错误:"
+
res
.
toString
(
2
));
log
.
info
(
"图像识别错误:"
+
res
.
toString
(
2
));
throw
new
ServiceException
(
"图像识别错误:"
+
res
.
toString
(
2
));
throw
new
ServiceException
(
"图像识别错误:"
+
res
.
toString
(
2
));
}
else
{
}
else
{
List
<
ImageResult
>
imageResultList
=
new
ArrayList
<>();
List
<
ImageResult
>
imageResultList
=
new
ArrayList
<>();
JSONArray
jsonArray
=
res
.
getJSONArray
(
"result"
);
JSONArray
jsonArray
=
res
.
getJSONArray
(
"result"
);
for
(
int
i
=
0
;
i
<
jsonArray
.
length
();
i
++)
{
for
(
int
i
=
0
;
i
<
jsonArray
.
length
();
i
++)
{
...
@@ -127,23 +127,25 @@ public class AipImageClassifyClient {
...
@@ -127,23 +127,25 @@ public class AipImageClassifyClient {
/**
/**
* 获取单个图片识别结果,根据置信度从高到底排序
* 获取单个图片识别结果,根据置信度从高到底排序
*
* @param
* @param
* @author luxor
* @author luxor
* @date 2017/12/7
* @date 2017/12/7
*/
*/
public
ImageResult
getImageResult
(
String
imageBase64
)
throws
JSONException
{
public
ImageResult
getImageResult
(
String
imageBase64
)
throws
JSONException
{
if
(
StringUtils
.
isBlank
(
imageBase64
))
{
if
(
StringUtils
.
isBlank
(
imageBase64
))
{
return
new
ImageResult
();
return
new
ImageResult
();
}
}
String
image
=
imageBase64
.
replace
(
"data:image/png;base64,"
,
""
)
String
image
=
imageBase64
.
replace
(
"data:image/png;base64,"
,
""
)
.
replace
(
"data:image/jpg;base64,"
,
""
)
.
replace
(
"data:image/jpg;base64,"
,
""
)
.
replace
(
"data:image/jpeg;base64,"
,
""
);
.
replace
(
"data:image/jgp;base64,"
,
""
)
log
.
info
(
"image:"
+
image
);
.
replace
(
"data:image/jpeg;base64,"
,
""
);
List
<
ImageResult
>
imageResultList
=
this
.
getAnimalDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
log
.
info
(
"image:"
+
image
);
List
<
ImageResult
>
imageResultList2
=
this
.
getPlantDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
List
<
ImageResult
>
imageResultList
=
this
.
getAnimalDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
List
<
ImageResult
>
imageResultList2
=
this
.
getPlantDetect
(
image
,
ImageInputTypeEnum
.
BASE64
);
imageResultList
.
addAll
(
imageResultList2
);
imageResultList
.
addAll
(
imageResultList2
);
log
.
info
(
JSON
.
toJSONString
(
imageResultList
));
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 {
...
@@ -250,7 +250,7 @@ public final class AipWordUtilBean {
* @author houkun
* @author houkun
* @date 2017/12/6
* @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
<
String
>
documents
=
splitDocument
(
document
,
10000
);
List
<
LexerItem
>
lexerItems
=
new
ArrayList
<>();
List
<
LexerItem
>
lexerItems
=
new
ArrayList
<>();
for
(
String
s
:
documents
)
{
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 {
...
@@ -23,7 +23,8 @@ public class WordQuery extends Query {
QueryCriteria
queryCriteria
=
new
QueryCriteria
(
WordDomain
.
class
);
QueryCriteria
queryCriteria
=
new
QueryCriteria
(
WordDomain
.
class
);
Example
.
Criteria
criteria
=
queryCriteria
.
createCriteria
();
Example
.
Criteria
criteria
=
queryCriteria
.
createCriteria
();
if
(
valid
(
keyword
))
{
if
(
valid
(
keyword
))
{
String
str
=
"%"
+
keyword
+
"%"
;
criteria
.
andCondition
(
String
.
format
(
"(word like '%s' or CONTAINS(%s,word))"
,
str
,
keyword
));
}
}
//todo 写查询逻辑
//todo 写查询逻辑
return
queryCriteria
;
return
queryCriteria
;
...
...
serverside/cihai-wechat/src/main/java/com/dookay/cihai/wechat/controller/HomeController.java
View file @
3fbd46d
...
@@ -14,13 +14,18 @@
...
@@ -14,13 +14,18 @@
package
com
.
dookay
.
cihai
.
wechat
.
controller
;
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.AipWordUtilBean
;
import
com.dookay.cihai.core.aip.model.ImageResult
;
import
com.dookay.cihai.core.theme.domain.ThemeDomain
;
import
com.dookay.cihai.core.theme.domain.ThemeDomain
;
import
com.dookay.cihai.core.theme.query.ThemeQuery
;
import
com.dookay.cihai.core.theme.query.ThemeQuery
;
import
com.dookay.cihai.core.theme.service.IThemeService
;
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.CustomDictionaryQuery
;
import
com.dookay.cihai.core.word.query.WordQuery
;
import
com.dookay.cihai.core.word.service.ICustomDictionaryService
;
import
com.dookay.cihai.core.word.service.ICustomDictionaryService
;
import
com.dookay.cihai.core.word.service.IWordService
;
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.controller.BaseController
;
import
com.dookay.coral.common.web.response.JsonResult
;
import
com.dookay.coral.common.web.response.JsonResult
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -49,6 +54,9 @@ public class HomeController extends BaseController {
...
@@ -49,6 +54,9 @@ public class HomeController extends BaseController {
@Autowired
@Autowired
private
AipWordUtilBean
aipWordUtilBean
;
private
AipWordUtilBean
aipWordUtilBean
;
@Autowired
private
AipImageClassifyClient
aipImageClassifyClient
;
/**
/**
* @author 石磊
* @author 石磊
* @date 2017/12/6
* @date 2017/12/6
...
@@ -82,11 +90,36 @@ public class HomeController extends BaseController {
...
@@ -82,11 +90,36 @@ public class HomeController extends BaseController {
ThemeQuery
query
=
new
ThemeQuery
();
ThemeQuery
query
=
new
ThemeQuery
();
query
.
setKeyword
(
result
);
query
.
setKeyword
(
result
);
ThemeDomain
themeDomain
=
themeService
.
getFirst
(
query
);
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
);
System
.
out
.
println
(
themeDomain
);
return
successResult
(
"success"
,
"/theme/detail/"
+
themeDomain
.
getId
());
return
successResult
(
"success"
,
"/theme/detail/"
+
themeDomain
.
getId
());
}
catch
(
Exception
ex
)
{
}
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 @@
...
@@ -105,9 +105,19 @@
localId: localIds[0], // 图片的localID
localId: localIds[0], // 图片的localID
success: function (res) {
success: function (res) {
myApp.dialog.preloader();
var localData = res.localData; // localData是图片的base64数据,可以用img标签显示
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