8 changed files with 118 additions and 181 deletions
			
			
		@ -0,0 +1,23 @@ | 
				
			|||||
 | 
					package com.visual.open.anpr.core.base; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import com.visual.open.anpr.core.domain.ImageMat; | 
				
			||||
 | 
					import com.visual.open.anpr.core.domain.PlateInfo; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import java.util.List; | 
				
			||||
 | 
					import java.util.Map; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					public interface PlateDetection { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     *获取人脸信息 | 
				
			||||
 | 
					     * @param image     图像信息 | 
				
			||||
 | 
					     * @param scoreTh   人脸人数阈值 | 
				
			||||
 | 
					     * @param iouTh     人脸iou阈值 | 
				
			||||
 | 
					     * @param params    参数信息 | 
				
			||||
 | 
					     * @return | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    List<PlateInfo> inference(ImageMat image, float scoreTh, float iouTh, Map<String, Object> params); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,44 @@ | 
				
			|||||
 | 
					package com.visual.open.anpr.core.models; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import ai.onnxruntime.OnnxTensor; | 
				
			||||
 | 
					import com.visual.open.anpr.core.base.BaseOnnxInfer; | 
				
			||||
 | 
					import com.visual.open.anpr.core.base.PlateDetection; | 
				
			||||
 | 
					import com.visual.open.anpr.core.domain.ImageMat; | 
				
			||||
 | 
					import com.visual.open.anpr.core.domain.PlateInfo; | 
				
			||||
 | 
					import com.visual.open.anpr.core.utils.ReleaseUtil; | 
				
			||||
 | 
					import org.opencv.core.Scalar; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import java.util.List; | 
				
			||||
 | 
					import java.util.Map; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					public class PaddlePlateDetection extends BaseOnnxInfer implements PlateDetection { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private float std[] = {0.229f, 0.224f, 0.225f}; | 
				
			||||
 | 
					    private Scalar mean = new Scalar(0.485, 0.456, 0.406); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public PaddlePlateDetection(String modelPath, int threads) { | 
				
			||||
 | 
					        super(modelPath, threads); | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Override | 
				
			||||
 | 
					    public List<PlateInfo> inference(ImageMat image, float scoreTh, float iouTh, Map<String, Object> params) { | 
				
			||||
 | 
					        ImageMat imageMat = image.clone(); | 
				
			||||
 | 
					        OnnxTensor tensor = null; | 
				
			||||
 | 
					        try { | 
				
			||||
 | 
					            tensor = imageMat | 
				
			||||
 | 
					                    .blobFromImageAndDoReleaseMat(1.0/255, mean, true) | 
				
			||||
 | 
					                    .to4dFloatOnnxTensorAndNoReleaseMat(std,true); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        }catch (Exception e){ | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        }finally { | 
				
			||||
 | 
					            ReleaseUtil.release(tensor); | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        return null; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
@ -1,111 +0,0 @@ | 
				
			|||||
package com.visual.open.anpr.core.utils; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
import com.visual.face.search.core.domain.FaceInfo; | 
					 | 
				
			||||
import com.visual.face.search.core.domain.ImageMat; | 
					 | 
				
			||||
import org.opencv.core.*; | 
					 | 
				
			||||
import org.opencv.imgproc.Imgproc; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
import java.util.ArrayList; | 
					 | 
				
			||||
import java.util.List; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
public class MaskUtil { | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /**添加遮罩层所需要的索引号:InsightCoordFaceKeyPoint**/ | 
					 | 
				
			||||
    private static int [] MASK_106_IST_ROUND_INDEX = new int[]{ | 
					 | 
				
			||||
            1,9,10,11,12,13,14,15,16,2,3,4,5,6,7,8,0, | 
					 | 
				
			||||
            24,23,22,21,20,19,18,32,31,30,29,28,27,26,25,17, | 
					 | 
				
			||||
            101,105,104,103,102,50,51,49,48,43 | 
					 | 
				
			||||
    }; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 添加遮罩层 | 
					 | 
				
			||||
     * @param image         原始图像 | 
					 | 
				
			||||
     * @param pts           指定不不需要填充的区域 | 
					 | 
				
			||||
     * @param release   是否释放参数image | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static Mat mask(Mat image, List<MatOfPoint> pts, boolean release){ | 
					 | 
				
			||||
        Mat pattern = null; | 
					 | 
				
			||||
        try { | 
					 | 
				
			||||
            pattern = MatOfPoint.zeros(image.size(), CvType.CV_8U); | 
					 | 
				
			||||
            Imgproc.fillPoly(pattern, pts, new Scalar(1,1,1)); | 
					 | 
				
			||||
            Mat dst = new Mat(); | 
					 | 
				
			||||
            image.copyTo(dst, pattern); | 
					 | 
				
			||||
            return dst; | 
					 | 
				
			||||
        }finally { | 
					 | 
				
			||||
            if(null != pattern){ | 
					 | 
				
			||||
                pattern.release(); | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
            if(release && null != pts){ | 
					 | 
				
			||||
                for(MatOfPoint pt : pts){ | 
					 | 
				
			||||
                    pt.release(); | 
					 | 
				
			||||
                } | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
            if(release && null != image){ | 
					 | 
				
			||||
                image.release(); | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 添加遮罩层 | 
					 | 
				
			||||
     * @param image         原始图像 | 
					 | 
				
			||||
     * @param fillPoints    指定不不需要填充的区域的点 | 
					 | 
				
			||||
     * @param release   是否释放参数image | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static Mat mask(Mat image, Point[] fillPoints, boolean release){ | 
					 | 
				
			||||
        List<MatOfPoint> pts = null; | 
					 | 
				
			||||
        try { | 
					 | 
				
			||||
            pts = new ArrayList<>(); | 
					 | 
				
			||||
            pts.add(new MatOfPoint(fillPoints)); | 
					 | 
				
			||||
            return mask(image, pts, false); | 
					 | 
				
			||||
        }finally { | 
					 | 
				
			||||
            if(null != pts){ | 
					 | 
				
			||||
                for(MatOfPoint pt : pts){ | 
					 | 
				
			||||
                    pt.release(); | 
					 | 
				
			||||
                } | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
            if(release && null != image){ | 
					 | 
				
			||||
                image.release(); | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 添加遮罩层:InsightCoordFaceKeyPoint | 
					 | 
				
			||||
     * @param image     原始图像 | 
					 | 
				
			||||
     * @param points    人脸标记点 | 
					 | 
				
			||||
     * @param release   是否释放参数image | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static Mat maskFor106InsightCoordModel(Mat image, FaceInfo.Points points, boolean release){ | 
					 | 
				
			||||
        try { | 
					 | 
				
			||||
            Point[] fillPoints = PointUtil.convert(points.select(MASK_106_IST_ROUND_INDEX)); | 
					 | 
				
			||||
            return mask(image, fillPoints, false); | 
					 | 
				
			||||
        }finally { | 
					 | 
				
			||||
            if(release && null != image){ | 
					 | 
				
			||||
                image.release(); | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 添加遮罩层:InsightCoordFaceKeyPoint | 
					 | 
				
			||||
     * @param image     原始图像 | 
					 | 
				
			||||
     * @param points    人脸标记点 | 
					 | 
				
			||||
     * @param release       是否释放参数image | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static ImageMat maskFor106InsightCoordModel(ImageMat image, FaceInfo.Points points, boolean release){ | 
					 | 
				
			||||
        try { | 
					 | 
				
			||||
            Mat mat = maskFor106InsightCoordModel(image.toCvMat(), points, false); | 
					 | 
				
			||||
            return ImageMat.fromCVMat(mat); | 
					 | 
				
			||||
        }finally { | 
					 | 
				
			||||
            if(release && null != image){ | 
					 | 
				
			||||
                image.release(); | 
					 | 
				
			||||
            } | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
} | 
					 | 
				
			||||
@ -1,39 +0,0 @@ | 
				
			|||||
package com.visual.open.anpr.core.utils; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
import com.visual.face.search.core.domain.FaceInfo; | 
					 | 
				
			||||
import org.opencv.core.Point; | 
					 | 
				
			||||
 | 
					 | 
				
			||||
public class PointUtil { | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 转换点对象 | 
					 | 
				
			||||
     * @param point | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static FaceInfo.Point convert(Point point){ | 
					 | 
				
			||||
        return FaceInfo.Point.build((float)point.x, (float)point.y); | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 转换点对象 | 
					 | 
				
			||||
     * @param point | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static Point convert(FaceInfo.Point point){ | 
					 | 
				
			||||
        return new Point(point.x, point.y); | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
    /** | 
					 | 
				
			||||
     * 转换点对象 | 
					 | 
				
			||||
     * @param points | 
					 | 
				
			||||
     * @return | 
					 | 
				
			||||
     */ | 
					 | 
				
			||||
    public static Point[] convert(FaceInfo.Points points){ | 
					 | 
				
			||||
        Point[] result = new Point[points.size()]; | 
					 | 
				
			||||
        for(int i=0; i< points.size(); i++){ | 
					 | 
				
			||||
            result[i] = convert(points.get(i)); | 
					 | 
				
			||||
        } | 
					 | 
				
			||||
        return result; | 
					 | 
				
			||||
    } | 
					 | 
				
			||||
 | 
					 | 
				
			||||
} | 
					 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue