From ec8fe1cccfe0a5c41f37743d4575485833eb6fae Mon Sep 17 00:00:00 2001 From: divenswu Date: Thu, 13 Apr 2023 10:38:33 +0800 Subject: [PATCH] =?UTF-8?q?[update]:=E9=98=B2=E6=AD=A2=E7=94=B1=E4=BA=8E?= =?UTF-8?q?=E8=BD=A6=E7=89=8C=E5=8D=A0=E7=94=A8=E5=A4=A7=EF=BC=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=A3=80=E6=B5=8B=E6=A8=A1=E5=9E=8B=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../open/anpr/core/domain/PlateInfo.java | 17 ++++++++++++++ .../anpr/core/extract/PlateExtractorImpl.java | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/open-anpr-core/src/main/java/com/visual/open/anpr/core/domain/PlateInfo.java b/open-anpr-core/src/main/java/com/visual/open/anpr/core/domain/PlateInfo.java index e128c85..d163835 100755 --- a/open-anpr-core/src/main/java/com/visual/open/anpr/core/domain/PlateInfo.java +++ b/open-anpr-core/src/main/java/com/visual/open/anpr/core/domain/PlateInfo.java @@ -303,6 +303,23 @@ public class PlateInfo implements Comparable, 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 diff --git a/open-anpr-core/src/main/java/com/visual/open/anpr/core/extract/PlateExtractorImpl.java b/open-anpr-core/src/main/java/com/visual/open/anpr/core/extract/PlateExtractorImpl.java index b0390ae..b549896 100644 --- a/open-anpr-core/src/main/java/com/visual/open/anpr/core/extract/PlateExtractorImpl.java +++ b/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.PlateInfo; import com.visual.open.anpr.core.utils.CropUtil; +import org.opencv.core.Core; import org.opencv.core.Mat; import java.util.HashMap; @@ -27,6 +28,28 @@ public class PlateExtractorImpl implements PlateExtractor { @Override public PlateImage extract(ImageMat image, ExtParam extParam, Map params) { List 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 int topK = (extParam.getTopK() > 0) ? extParam.getTopK() : 5; if(plateInfos.size() > topK){