Commit 3fbd46dc by 石头

整合词条页面

1 parent 2fb7eb7f
......@@ -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());
}
}
......@@ -250,7 +250,7 @@ public final class AipWordUtilBean {
* @author houkun
* @date 2017/12/6
*/
private List<LexerItem> getLexerItems(String document) throws JSONException {
public List<LexerItem> getLexerItems(String document) throws JSONException {
List<String> documents = splitDocument(document, 10000);
List<LexerItem> lexerItems = new ArrayList<>();
for (String s : documents) {
......
......@@ -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;
......
......@@ -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("暂无检索结果");
}
}
......
......@@ -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);
}
})
}
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!