Browse Source

[update]:防止由于车牌占用大,导致检测模型识别失败

master
divenswu 1 year ago
parent
commit
ec8fe1cccf
  1. 17
      open-anpr-core/src/main/java/com/visual/open/anpr/core/domain/PlateInfo.java
  2. 23
      open-anpr-core/src/main/java/com/visual/open/anpr/core/extract/PlateExtractorImpl.java

17
open-anpr-core/src/main/java/com/visual/open/anpr/core/domain/PlateInfo.java

@ -303,6 +303,23 @@ public class PlateInfo implements Comparable<PlateInfo>, Serializable {
); );
} }
/**
* 将框进行平移
* @param top 向上移动的像素点数
* @param bottom 向下移动的像素点数
* @param left 向左移动的像素点数
* @param right 向右移动的像素点数
* @return 平移后的框
*/
public PlateBox move(int left, int right, int top, int bottom){
return new PlateBox(
new Point(leftTop.x - left + right, leftTop.y - top + bottom),
new Point(rightTop.x - left + right, rightTop.y - top + bottom),
new Point(rightBottom.x - left + right, rightBottom.y - top + bottom),
new Point(leftBottom.x - left + right, leftBottom.y - top + bottom)
);
}
/** /**
* 转换为数组 * 转换为数组
* @return * @return

23
open-anpr-core/src/main/java/com/visual/open/anpr/core/extract/PlateExtractorImpl.java

@ -7,6 +7,7 @@ import com.visual.open.anpr.core.domain.ImageMat;
import com.visual.open.anpr.core.domain.PlateImage; import com.visual.open.anpr.core.domain.PlateImage;
import com.visual.open.anpr.core.domain.PlateInfo; import com.visual.open.anpr.core.domain.PlateInfo;
import com.visual.open.anpr.core.utils.CropUtil; import com.visual.open.anpr.core.utils.CropUtil;
import org.opencv.core.Core;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import java.util.HashMap; import java.util.HashMap;
@ -27,6 +28,28 @@ public class PlateExtractorImpl implements PlateExtractor {
@Override @Override
public PlateImage extract(ImageMat image, ExtParam extParam, Map<String, Object> params) { public PlateImage extract(ImageMat image, ExtParam extParam, Map<String, Object> params) {
List<PlateInfo> plateInfos = plateDetection.inference(image, extParam.getScoreTh(),extParam.getIouTh(), new HashMap<>()); List<PlateInfo> plateInfos = plateDetection.inference(image, extParam.getScoreTh(),extParam.getIouTh(), new HashMap<>());
if(plateInfos.isEmpty()){
//防止由于车牌占用大,导致检测模型识别失败
Mat tempMat = new Mat();
int t = image.toCvMat().height() / 2;
int b = image.toCvMat().height() / 2;
int l = image.toCvMat().width() / 2;
int r = image.toCvMat().width() / 2;
Core.copyMakeBorder(image.toCvMat(), tempMat, t, b, l, r, Core.BORDER_CONSTANT);
plateInfos = plateDetection.inference(ImageMat.fromCVMat(tempMat), extParam.getScoreTh(),extParam.getIouTh(), new HashMap<>());
for(PlateInfo plateInfo : plateInfos){
plateInfo.box = plateInfo.box.move(l, 0, t, 0);
plateInfo.box.leftTop.x = Math.max(0, plateInfo.box.leftTop.x);
plateInfo.box.leftTop.y = Math.max(0, plateInfo.box.leftTop.y);
plateInfo.box.rightTop.x = Math.min(image.getWidth(), plateInfo.box.rightTop.x);
plateInfo.box.rightTop.y = Math.max(0, plateInfo.box.rightTop.y);
plateInfo.box.rightBottom.x = Math.min(image.getWidth(), plateInfo.box.rightBottom.x);
plateInfo.box.rightBottom.y = Math.min(image.getHeight(), plateInfo.box.rightBottom.y);
plateInfo.box.leftBottom.x = Math.max(0, plateInfo.box.leftBottom.x);
plateInfo.box.leftBottom.y = Math.min(image.getHeight(), plateInfo.box.leftBottom.y);
}
tempMat.release();
}
//取车牌topK //取车牌topK
int topK = (extParam.getTopK() > 0) ? extParam.getTopK() : 5; int topK = (extParam.getTopK() > 0) ? extParam.getTopK() : 5;
if(plateInfos.size() > topK){ if(plateInfos.size() > topK){

Loading…
Cancel
Save