Browse Source

ai识别优化

cangku
wangfukang 2 years ago
parent
commit
306cd49b58
  1. 32
      hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/AliOcrUtil.java

32
hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/utils/AliOcrUtil.java

@ -40,6 +40,7 @@ import java.util.regex.Pattern;
@Component @Component
public class AliOcrUtil { public class AliOcrUtil {
private static final Pattern COMPILED = Pattern.compile("...");
@Autowired @Autowired
private static AliOcrConfig aliOcrConfig; private static AliOcrConfig aliOcrConfig;
@ -127,7 +128,7 @@ public class AliOcrUtil {
// attributeList 有时候解析的不是json格式,特殊处理一下 // attributeList 有时候解析的不是json格式,特殊处理一下
String jsonStr = '[' + text.substring(startIndex, endIndex + 1) + ']'; String jsonStr = '[' + text.substring(startIndex, endIndex + 1) + ']';
// 有时候返回的是productCounts,改为productCount // 有时候返回的是productCounts,改为productCount
jsonStr = fixProductCounts(jsonStr); jsonStr = fixJsonStr(jsonStr);
JSONArray endJson = new JSONArray(); JSONArray endJson = new JSONArray();
try{ try{
endJson = JSON.parseArray(jsonStr); endJson = JSON.parseArray(jsonStr);
@ -192,7 +193,7 @@ public class AliOcrUtil {
// attributeList 有时候解析的不是json格式,特殊处理一下 // attributeList 有时候解析的不是json格式,特殊处理一下
String errorJsonJsonStr = '[' + text.substring(secondStartIndex, secondEndIndex + 1) + ']'; String errorJsonJsonStr = '[' + text.substring(secondStartIndex, secondEndIndex + 1) + ']';
// 有时候返回的是productCounts,改为productCount // 有时候返回的是productCounts,改为productCount
errorJsonJsonStr = fixProductCounts(errorJsonJsonStr); errorJsonJsonStr = fixJsonStr(errorJsonJsonStr);
try{ try{
// //
@ -267,7 +268,7 @@ public class AliOcrUtil {
}else{ }else{
String secondJsonStr = '[' + secondText.substring(secondStartIndex, secondEndIndex + 1) + ']'; String secondJsonStr = '[' + secondText.substring(secondStartIndex, secondEndIndex + 1) + ']';
// 有时候返回的是productCounts,改为productCount // 有时候返回的是productCounts,改为productCount
secondJsonStr = fixProductCounts(secondJsonStr); secondJsonStr = fixJsonStr(secondJsonStr);
try{ try{
final JSONArray secondJson = JSON.parseArray(secondJsonStr); final JSONArray secondJson = JSON.parseArray(secondJsonStr);
final int secondResultCount = secondJson.size(); final int secondResultCount = secondJson.size();
@ -344,7 +345,7 @@ public class AliOcrUtil {
// attributeList 有时候解析的不是json格式,特殊处理一下 // attributeList 有时候解析的不是json格式,特殊处理一下
String errorJsonJsonStr = '[' + secondText.substring(errorSsecondStartIndex, errorSecondEndIndex + 1) + ']'; String errorJsonJsonStr = '[' + secondText.substring(errorSsecondStartIndex, errorSecondEndIndex + 1) + ']';
// 有时候返回的是productCounts,改为productCount // 有时候返回的是productCounts,改为productCount
errorJsonJsonStr = fixProductCounts(errorJsonJsonStr); errorJsonJsonStr = fixJsonStr(errorJsonJsonStr);
try{ try{
// //
// endJson = JSON.parseArray(errorJsonJsonStr); // endJson = JSON.parseArray(errorJsonJsonStr);
@ -450,14 +451,28 @@ public class AliOcrUtil {
} }
/** /**
* 有时候返回的是productCounts改为productCount * 1.有时候返回的是productCounts改为productCount
* 2. 有的时候中间会有...,需要特殊处理一下
* *
* @param str * @param str
* @return String * @return String
* @author 王富康 * @author 王富康
* @date 2024/5/12 * @date 2024/5/12
*/ */
public static String fixProductCounts(String str) { public static String fixJsonStr(String str) {
// 如果包含"...",那么就截取掉,在商品中不会出现,并且在json应该只会出现一次,如果会出现多次的话,那这里需要再次优化
if(str.contains("...")){
final String[] newStr = COMPILED.split(str);
final int newStrLength = newStr.length;
if(newStrLength ==2){
// 第一个字符串从开头截取到第一个}
newStr[0] = newStr[0].substring(0, newStr[0].lastIndexOf('}'));
// 第二个字符串从第一个{截取到最后
newStr[1] = newStr[1].substring(newStr[1].indexOf('{'));
// 重新拼接,并且中间加上逗号
str = newStr[0]+ ',' +newStr[1];
}
}
str = str.replace("productCounts", "productCount"); str = str.replace("productCounts", "productCount");
return str; return str;
} }
@ -690,11 +705,11 @@ public class AliOcrUtil {
// orderId // orderId
final Map<String, PurchaseDetail> purchaseDetailMap = new ConcurrentHashMap<>(); final Map<String, PurchaseDetail> purchaseDetailMap = new ConcurrentHashMap<>();
for (PurchaseOcrVo purchaseOcrVo : purchaseOcrVos) { for (PurchaseOcrVo purchaseOcrVo : purchaseOcrVos) {
String productSn = purchaseOcrVo.getProductSn(); final String productSn = purchaseOcrVo.getProductSn();
final String productName = purchaseOcrVo.getProductName(); final String productName = purchaseOcrVo.getProductName();
// 根据商品的货号或者 // 根据商品的货号或者
Product product = productSnMap.get(productSn); final Product product = productSnMap.get(productSn);
// 如果货号存在,就以货号为准查询库存商品能查到就是有,没有查到就是没有,如果有货号,不再根据名称查询 // 如果货号存在,就以货号为准查询库存商品能查到就是有,没有查到就是没有,如果有货号,不再根据名称查询
/*if (product == null && StringUtils.isEmpty(productSn)) { /*if (product == null && StringUtils.isEmpty(productSn)) {
product = productNameMap.get(productName); product = productNameMap.get(productName);
@ -771,6 +786,7 @@ public class AliOcrUtil {
purchaseDetail.setCategoryId(product.getCategoryId()); purchaseDetail.setCategoryId(product.getCategoryId());
purchaseDetail.setPrice(product.getPrice()); purchaseDetail.setPrice(product.getPrice());
purchaseDetail.setWholesalePrice(product.getWholesalePrice()); purchaseDetail.setWholesalePrice(product.getWholesalePrice());
purchaseDetail.setProductName(product.getProductName());
stockLog.setProductId(product.getId()); stockLog.setProductId(product.getId());
} }
stockLogList.add(stockLog); stockLogList.add(stockLog);

Loading…
Cancel
Save