图片比对优化

pull/1/head
lzCodeGarden 1 year ago
parent 188ef2019c
commit 137790a115

@ -301,6 +301,11 @@ public class OcrPicture implements BaseDto, java.io.Serializable {
@TableField(value = "is_download") @TableField(value = "is_download")
private Integer isdownload; private Integer isdownload;
@ApiModelProperty(value = "图片hash值")
@Schema(description = "图片hash值")
@TableField(value = "img_hash")
private String imgHash;
@ApiModelProperty(value = "图片查重的比对图片集合") @ApiModelProperty(value = "图片查重的比对图片集合")
@TableField(exist = false) @TableField(exist = false)
private List<OcrPicture> listCom; private List<OcrPicture> listCom;
@ -392,7 +397,8 @@ public class OcrPicture implements BaseDto, java.io.Serializable {
new SimpleEntry<>("field16", "field16"), new SimpleEntry<>("field16", "field16"),
new SimpleEntry<>("field17", "field17"), new SimpleEntry<>("field17", "field17"),
new SimpleEntry<>("field18", "field18"), new SimpleEntry<>("field18", "field18"),
new SimpleEntry<>("localpictrueurl", "localpictrueurl") new SimpleEntry<>("localpictrueurl", "localpictrueurl"),
new SimpleEntry<>("imgHash", "imgHash")
) )
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)); .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));

@ -3,6 +3,7 @@ package cn.jyjz.xiaoyao.ocr.service;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import cn.jyjz.xiaoyao.common.base.vo.ResultVo; import cn.jyjz.xiaoyao.common.base.vo.ResultVo;
import cn.jyjz.xiaoyao.common.base.vo.UserToken; import cn.jyjz.xiaoyao.common.base.vo.UserToken;
@ -98,4 +99,6 @@ public interface OcrPictureService extends BaseService<OcrPicture> {
int savePictures(List<OcrPicture> ocrPictureList); int savePictures(List<OcrPicture> ocrPictureList);
ResultVo createPackageTask(List<OcrPicture> pageList, String tenantId, UserToken userToken, String searchMonth, HttpServletRequest req, String buessinessno); ResultVo createPackageTask(List<OcrPicture> pageList, String tenantId, UserToken userToken, String searchMonth, HttpServletRequest req, String buessinessno);
Map<String,String> getOcrPictureClassifyAndHash(String img);
} }

@ -40,10 +40,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
/** /**
* *
@ -320,20 +317,38 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
} }
//TODO 接口请求判断重复情况 //TODO 接口请求判断重复情况
logService.addLog(103, "AI获取图片相似度接口", sysUser, "sendParams"); logService.addLog(103, "AI获取图片相似度接口", sysUser, "sendParams");
JSONObject jsonObjectSimi = ImageClassUtil.getSimilarity(StringUtils.isBlank(ocrPicture.getLocalpictrueurl()) ? ocrPicture.getImgurl() : ocrPicture.getLocalpictrueurl(), if(StringUtils.isBlank(ocrPicture.getImgHash())){
StringUtils.isBlank(ocrPictureNext.getLocalpictrueurl()) ? ocrPictureNext.getImgurl() : ocrPictureNext.getLocalpictrueurl(), Map<String,String> map = this.getOcrPictureClassifyAndHash(ocrPicture.getLocalpictrueurl());
similarityPath); if(map!=null){
System.out.println("responseDataXs--code = [" + jsonObjectSimi.get("code") + "]"); ocrPicture.setImgHash(map.get("hash"));
System.out.println("responseDataXs--similarity = [" + jsonObjectSimi.get("similarity") + "]"); ocrPicture.setClassificationid(map.get("classId"));
String resultValue = ""; }
//返回内容为零,表示成功,否则表示失败 this.updateById(ocrPicture);
if (null != jsonObjectSimi && jsonObjectSimi.getString("code").equals("0")) {
resultValue = jsonObjectSimi.getString("similarity");
ocrPictureNext.setResult(resultValue);
} else {
resultValue = "0";
ocrPictureNext.setResult(resultValue);
} }
if(StringUtils.isBlank(ocrPictureNext.getImgHash())){
Map<String,String> map = this.getOcrPictureClassifyAndHash(ocrPictureNext.getLocalpictrueurl());
if(map!=null){
ocrPictureNext.setImgHash(map.get("hash"));
ocrPictureNext.setClassificationid(map.get("classId"));
}
this.updateById(ocrPictureNext);
}
String img=StringUtils.isBlank(ocrPicture.getImgHash())?StringUtils.isBlank(ocrPicture.getLocalpictrueurl()) ? ocrPicture.getImgurl() : ocrPicture.getLocalpictrueurl():ocrPicture.getImgHash();
String imgNext=StringUtils.isBlank(ocrPictureNext.getImgHash())?StringUtils.isBlank(ocrPictureNext.getLocalpictrueurl()) ? ocrPictureNext.getImgurl() : ocrPictureNext.getLocalpictrueurl():ocrPictureNext.getImgHash();
// JSONObject jsonObjectSimi = ImageClassUtil.getSimilarity(img,imgNext,similarityPath);
// System.out.println("responseDataXs--code = [" + jsonObjectSimi.get("code") + "]");
// System.out.println("responseDataXs--similarity = [" + jsonObjectSimi.get("similarity") + "]");
String resultValue = HashCompareUtil.cmpHash(img, imgNext);
//返回内容为零,表示成功,否则表示失败
// if (null != jsonObjectSimi && jsonObjectSimi.getString("code").equals("0")) {
// resultValue = jsonObjectSimi.getString("similarity");
// ocrPictureNext.setResult(resultValue);
// } else {
// resultValue = "0";
// ocrPictureNext.setResult(resultValue);
// }
ocrPictureNext.setResult(resultValue);
BigDecimal b1 = new BigDecimal(resultValue); BigDecimal b1 = new BigDecimal(resultValue);
BigDecimal b2 = new BigDecimal("100"); BigDecimal b2 = new BigDecimal("100");
int val = b1.compareTo(b2); int val = b1.compareTo(b2);
@ -351,32 +366,32 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
ocrPicture.setListCom(compareList); ocrPicture.setListCom(compareList);
//非历史图片主键集合 //非历史图片主键集合
ocrPicture.setPicturecompareList(picturecompareList); ocrPicture.setPicturecompareList(picturecompareList);
//查询项目对象 // //查询项目对象
if (null == ocrPicture.getCategoryid()) { // if (null == ocrPicture.getCategoryid()) {
//TODO 没有对应项目,返回提示信息 // //TODO 没有对应项目,返回提示信息
if (null != error && error.length() > 1) { // if (null != error && error.length() > 1) {
error.append("," + ocrPicture.getImgname()); // error.append("," + ocrPicture.getImgname());
} else { // } else {
error.append(ocrPicture.getImgname()); // error.append(ocrPicture.getImgname());
} // }
continue; // continue;
} // }
Category category = categoryService.getById(ocrPicture.getCategoryid()); // Category category = categoryService.getById(ocrPicture.getCategoryid());
if (null == category) { // if (null == category) {
if (null != errorProject && errorProject.length() > 1) { // if (null != errorProject && errorProject.length() > 1) {
errorProject.append("," + ocrPicture.getImgname()); // errorProject.append("," + ocrPicture.getImgname());
} else { // } else {
errorProject.append(ocrPicture.getImgname()); // errorProject.append(ocrPicture.getImgname());
} // }
continue; // continue;
} else if (StringUtils.isBlank(category.getProcessdefinitionid())) { // } else if (StringUtils.isBlank(category.getProcessdefinitionid())) {
if (null != errorFlow && errorFlow.length() > 1) { // if (null != errorFlow && errorFlow.length() > 1) {
errorFlow.append("," + category.getCategoryname()); // errorFlow.append("," + category.getCategoryname());
} else { // } else {
errorFlow.append(category.getCategoryname()); // errorFlow.append(category.getCategoryname());
} // }
continue; // continue;
} // }
//创建工单,如果创建成功,增加标志,用于过滤条件使用 //创建工单,如果创建成功,增加标志,用于过滤条件使用
OcrTaskchildPicture ocrTaskchildPicture = this.updateTaskChild(ocrPicture, ocrPackagetask.getId(), buessinessno); OcrTaskchildPicture ocrTaskchildPicture = this.updateTaskChild(ocrPicture, ocrPackagetask.getId(), buessinessno);
@ -417,6 +432,20 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
return ResultVoUtil.error("失败。"); return ResultVoUtil.error("失败。");
} }
@Override
public Map<String, String> getOcrPictureClassifyAndHash(String img) {
JSONObject jsonObjectSimi = ImageClassUtil.getClassify(img,"","http://127.0.0.1:8000/api/classify/");
//返回内容为零,表示成功,否则表示失败
if (null != jsonObjectSimi && jsonObjectSimi.getString("code").equals("0")) {
Map<String,String> map=new HashMap();
map.put("classId",jsonObjectSimi.getString("classId"));
map.put("hash",jsonObjectSimi.getString("hash"));
return map;
}else{
return null;
}
}
public OcrTaskchildPicture updateTaskChild(OcrPicture ocrPicture, Long packageid, String buessinessno) { public OcrTaskchildPicture updateTaskChild(OcrPicture ocrPicture, Long packageid, String buessinessno) {
OcrTaskchildPicture ocrTaskchildPicture = ocrTaskchildPictureService.getOne(new LambdaQueryWrapper<OcrTaskchildPicture>().eq(OcrTaskchildPicture::getPictureid, ocrPicture.getId())); OcrTaskchildPicture ocrTaskchildPicture = ocrTaskchildPictureService.getOne(new LambdaQueryWrapper<OcrTaskchildPicture>().eq(OcrTaskchildPicture::getPictureid, ocrPicture.getId()));
ocrTaskchildPicture.setBuessinessno(buessinessno); ocrTaskchildPicture.setBuessinessno(buessinessno);

@ -9,9 +9,12 @@ import cn.jyjz.xiaoyao.ocr.dataobject.OcrPicture;
import cn.jyjz.xiaoyao.ocr.service.OcrPictureService; import cn.jyjz.xiaoyao.ocr.service.OcrPictureService;
import cn.jyjz.xiaoyao.ocr.thread.entity.PictureImgToLocalEntity; import cn.jyjz.xiaoyao.ocr.thread.entity.PictureImgToLocalEntity;
import cn.jyjz.xiaoyao.ocr.util.DownloadImgUtil; import cn.jyjz.xiaoyao.ocr.util.DownloadImgUtil;
import cn.jyjz.xiaoyao.ocr.util.ImageClassUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -28,6 +31,8 @@ public class PictureImgToLocalTask implements Runnable {
private PictureImgToLocalEntity pictureImgToLocal; private PictureImgToLocalEntity pictureImgToLocal;
public PictureImgToLocalTask(PictureImgToLocalEntity pictureImgToLocal) { public PictureImgToLocalTask(PictureImgToLocalEntity pictureImgToLocal) {
this.pictureImgToLocal = pictureImgToLocal; this.pictureImgToLocal = pictureImgToLocal;
} }
@ -42,6 +47,20 @@ public class PictureImgToLocalTask implements Runnable {
if (ObjectUtil.isNotEmpty(picture)) { if (ObjectUtil.isNotEmpty(picture)) {
picture.setLocalpictrueurl(pictureImgToLocal.getLocalPath()); picture.setLocalpictrueurl(pictureImgToLocal.getLocalPath());
picture.setIsdownload(1); picture.setIsdownload(1);
//TODO 接口请求判断重复情况
JSONObject jsonObjectSimi = ImageClassUtil.getClassify(picture.getLocalpictrueurl(),"","http://127.0.0.1:8000/api/classify/");
System.out.println("responseDataXs " +jsonObjectSimi.toJSONString());
System.out.println("responseDataXs--classId = [" + jsonObjectSimi.get("classId") + "]");
System.out.println("responseDataXs--hash = [" + jsonObjectSimi.get("hash") + "]");
String classId = "";
String hash="";
//返回内容为零,表示成功,否则表示失败
if (null != jsonObjectSimi && jsonObjectSimi.getString("code").equals("0")) {
classId = jsonObjectSimi.getString("classId");
hash = jsonObjectSimi.getString("hash");
}
picture.setImgHash(hash);
picture.setClassificationid(classId);
ocrPictureService.updateById(picture); ocrPictureService.updateById(picture);
} }
} }

@ -0,0 +1,39 @@
package cn.jyjz.xiaoyao.ocr.util;
/**
* @Title:
* @Description: hash
* @Author
* @Date 2024-03-18 11:36
* @Version V1.0
*/
public class HashCompareUtil {
public static String cmpHash(String hash1,String hash2){
int n = 0;
// hash长度不同则返回-1代表传参出错
if (hash1.length() != hash2.length()){
return "-1";
}
for(int i = 0; i < hash1.length(); i++){
if( hash1.charAt(i) != hash2.charAt(i)){
n=n + 1;
}
}
return String.format("%.2f", 100.0 - n/64.0 * 100.0);
}
public static void main(String[] args) {
String a="1111111100000111000110110001101100110011001100000000000011110001";
String b="1011111100000111000110110001101100110011001100000000000011110001";
long start = System.currentTimeMillis();
int ss=0;
// for(int i=0;i<5000000;i++){
// String a="1111111100000111000110110001101100110011001100000000000011110001"+i;
// String b="1011111100000111000110110001101100110011001100000000000011110001"+i;
String dd = HashCompareUtil.cmpHash(a, b);
System.out.println(dd);
// ss=i;
// }
}
}
Loading…
Cancel
Save