Commit 89139b59 by 陈皓

.

parent a9ca035e
package com.zq.imgproc.constant;
import lombok.Getter;
/**
* <p>
* 常用的阈值
* </P>
*
* @author chenhao
* @since 2023/11/26
*/
@Getter
public enum Threshold {
/**
* 亮度过低阈值
*/
START_VALUE("img-start", 75),
/**
* 亮度过低阈值
*/
END_VALUE("img-end", 175),
/**
* 清晰度阈值
*/
CLARITY("img-clarity", 2.5),
/**
* 弯曲度阈值
*/
BEND("img-clarity", 2.5);
private final String key;
private final double value;
Threshold(String key, double value) {
this.key = key;
this.value = value;
}
}
package com.zq.imgproc.controller;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.zq.imgproc.constant.Threshold;
import com.zq.imgproc.server.ImgProcService;
import com.zq.imgproc.utils.AssertUtils;
import com.zq.imgproc.utils.DecompressUtil;
......@@ -26,7 +26,6 @@ import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* <p>
......@@ -53,10 +52,6 @@ public class ImgProcController {
.initialCapacity(1024)
.build();
private final double START_VALUE = 75;
private final double END_VALUE = 175;
private final double CLARITY = 2.5;
private final ImgProcService service;
@Autowired
public ImgProcController(ImgProcService service) {
......@@ -113,7 +108,7 @@ public class ImgProcController {
@ApiOperation("图片校正(deskew工具)")
@PostMapping("/imageCorrection")
public ResultVo<?> imageCorrection(@RequestBody List<ImgVO> pathList) throws IOException {
public ResultVo<?> imageCorrection(@RequestBody List<ImgVO> pathList) {
if (pathList.isEmpty()) {
return ResultVo.success();
}
......@@ -132,12 +127,12 @@ public class ImgProcController {
@ApiOperation("获取配置")
@GetMapping("/getSetting")
public ResultVo<?> getSetting() {
Double temp1 = cache.getIfPresent("img-start");
Double temp2 = cache.getIfPresent("img-end");
Double temp3 = cache.getIfPresent("img-clarity");
double start = temp1 == null ? START_VALUE : temp1;
double end = temp2 == null ? END_VALUE : temp2;
double clarity = temp3 == null ? CLARITY : temp3;
Double temp1 = cache.getIfPresent(Threshold.START_VALUE.getKey());
Double temp2 = cache.getIfPresent(Threshold.END_VALUE.getKey());
Double temp3 = cache.getIfPresent(Threshold.CLARITY.getKey());
double start = temp1 == null ? Threshold.START_VALUE.getValue() : temp1;
double end = temp2 == null ? Threshold.END_VALUE.getValue() : temp2;
double clarity = temp3 == null ? Threshold.CLARITY.getValue() : temp3;
return ResultVo.success(SettingVO.builder().startValue(start).endValue(end).clarity(clarity).build());
}
......@@ -147,9 +142,9 @@ public class ImgProcController {
if (setting == null) {
return ResultVo.fail("配置不能为空!");
}
cache.put("img-start", setting.getStartValue());
cache.put("img-end", setting.getEndValue());
cache.put("img-clarity", setting.getClarity());
cache.put(Threshold.START_VALUE.getKey(), setting.getStartValue());
cache.put(Threshold.END_VALUE.getKey(), setting.getEndValue());
cache.put(Threshold.CLARITY.getKey(), setting.getClarity());
return ResultVo.success();
}
......
package com.zq.imgproc.utils;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zq.imgproc.vo.BendResult;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
/**
* <p>
* 弯曲度检测
* </P>
*
* @author yww
* @since 2023/11/26
*/
@Slf4j
public class BendUtil {
private final static String URL = "http://129.204.37.121:8001/api/det/bending";
public static void main(String[] args) {
System.out.println(getBendResult("C:\\Users\\11419\\Desktop\\project\\company\\test\\9.png").toString());
}
public static BendResult getBendResult(String imgPath) {
// 设定参数
HashMap<String, Object> param = new HashMap<>();
param.put("file", FileUtil.file(imgPath));
BendResult res = null;
try {
String result = HttpUtil.post(URL, param, 1000 * 20);
res = parseResult(result);
} catch (Exception e) {
log.error("图片弯曲检测失败!", e);
}
if (res == null) {
res = BendResult.builder().confidence(0.0).build();
}
return res;
}
/**
* 解析弯曲检测结果
*/
public static BendResult parseResult(String resultJson) {
BendResult result = null;
ResultVo resultVo = JSONUtil.toBean(resultJson, ResultVo.class);
if (!resultVo.isSuccess()) {
log.warn("弯曲度检测出错: {}", resultVo.getErrMsg());
return null;
}
JSONObject results = JSONUtil.parseObj(Convert.toStr(resultVo.getData()));
List<BendResult> bendResults = JSONUtil.toList(results.getStr("results"), BendResult.class);
if (bendResults == null || bendResults.isEmpty()) {
return null;
}
double max = -1;
for (BendResult bendResult : bendResults) {
if (bendResult.getConfidence() == null) {
continue;
}
if (bendResult.getConfidence() > max) {
result = bendResult;
max = bendResult.getConfidence();
}
}
return result;
}
}
package com.zq.imgproc.vo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* <p>
* 弯曲检测结果
* </P>
*
* @author chenhao
* @since 2023/11/26
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BendResult {
/**
* 标签名称
*/
String label;
/**
* 置信度
*/
Double confidence;
/**
* 弯曲位置
*/
List<Double> coord;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment