10 changed files with 428 additions and 11 deletions
			
			
		@ -0,0 +1,131 @@ | 
				
			|||
package com.visual.open.anpr.core.domain; | 
				
			|||
 | 
				
			|||
import org.opencv.core.CvType; | 
				
			|||
import org.opencv.core.Mat; | 
				
			|||
 | 
				
			|||
import javax.imageio.ImageIO; | 
				
			|||
import java.awt.*; | 
				
			|||
import java.awt.image.BufferedImage; | 
				
			|||
import java.awt.image.DataBufferByte; | 
				
			|||
import java.io.File; | 
				
			|||
import java.io.IOException; | 
				
			|||
 | 
				
			|||
 | 
				
			|||
public class DrawImage { | 
				
			|||
 | 
				
			|||
    private BufferedImage image; | 
				
			|||
 | 
				
			|||
    private DrawImage(BufferedImage image){ | 
				
			|||
        this.image = image; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public static DrawImage build(String image){ | 
				
			|||
        try { | 
				
			|||
            return build(ImageIO.read(new File(image))); | 
				
			|||
        } catch (IOException e) { | 
				
			|||
            throw new RuntimeException(e); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public static DrawImage build(BufferedImage image){ | 
				
			|||
        return new DrawImage(image); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    private static int getLength(String text) { | 
				
			|||
        int textLength = text.length(); | 
				
			|||
        int length = textLength; | 
				
			|||
        for (int i = 0; i < textLength; i++) { | 
				
			|||
            if (String.valueOf(text.charAt(i)).getBytes().length > 1) { | 
				
			|||
                length++; | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
        return (length % 2 == 0) ? length / 2 : length / 2 + 1; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public DrawImage drawRect(Rect rect, int lineWidth, Color color){ | 
				
			|||
        Graphics2D g = (Graphics2D)image.getGraphics(); | 
				
			|||
        g.setColor(color); | 
				
			|||
        g.setStroke(new BasicStroke(lineWidth)); | 
				
			|||
        g.drawRect(rect.x, rect.y, rect.width, rect.height); | 
				
			|||
        return this; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public DrawImage drawLine(Point point1, Point point2, int lineWidth, Color color){ | 
				
			|||
        Graphics2D g = (Graphics2D)image.getGraphics(); | 
				
			|||
        g.setColor(color); | 
				
			|||
        g.setStroke(new BasicStroke(lineWidth)); | 
				
			|||
        g.drawLine(point1.x, point1.y, point2.x, point2.y); | 
				
			|||
        return this; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public DrawImage drawText(String text, Point point, int fontSize, Color color){ | 
				
			|||
        Graphics2D g = image.createGraphics(); | 
				
			|||
        g.setColor(color); | 
				
			|||
        g.setFont(new Font("微软雅黑", Font.PLAIN, fontSize)); | 
				
			|||
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1)); | 
				
			|||
        int width_1 = fontSize * getLength(text); | 
				
			|||
        int height_1 = fontSize; | 
				
			|||
        int widthDiff = image.getWidth() - width_1; | 
				
			|||
        int heightDiff = image.getHeight() - height_1; | 
				
			|||
        if(point.x < 0){ | 
				
			|||
            point.x = widthDiff / 2; | 
				
			|||
        }else if(point.x > widthDiff){ | 
				
			|||
            point.x = widthDiff; | 
				
			|||
        } | 
				
			|||
        if(point.y < 0){ | 
				
			|||
            point.y = heightDiff / 2; | 
				
			|||
        }else if(point.y > heightDiff){ | 
				
			|||
            point.y = heightDiff; | 
				
			|||
        } | 
				
			|||
        g.drawString(text, point.x, point.y + height_1); | 
				
			|||
        return this; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public Mat toMat(){ | 
				
			|||
        try { | 
				
			|||
            if(image.getType() != BufferedImage.TYPE_3BYTE_BGR){ | 
				
			|||
                BufferedImage temp = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR); | 
				
			|||
                Graphics2D g = temp.createGraphics(); | 
				
			|||
                try { | 
				
			|||
                    g.setComposite(AlphaComposite.Src); | 
				
			|||
                    g.drawImage(image, 0, 0, null); | 
				
			|||
                } finally { | 
				
			|||
                    g.dispose(); | 
				
			|||
                } | 
				
			|||
                image = temp; | 
				
			|||
            } | 
				
			|||
            byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); | 
				
			|||
            Mat mat = Mat.eye(image.getHeight(), image.getWidth(), CvType.CV_8UC3); | 
				
			|||
            mat.put(0, 0, pixels); | 
				
			|||
            return mat; | 
				
			|||
        }catch (Exception e){ | 
				
			|||
            throw new RuntimeException(e); | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public static class Rect{ | 
				
			|||
        public int x; | 
				
			|||
        public int y; | 
				
			|||
        public int width; | 
				
			|||
        public int height; | 
				
			|||
 | 
				
			|||
        public Rect(int x, int y, int width, int height) { | 
				
			|||
            this.x = x; | 
				
			|||
            this.y = y; | 
				
			|||
            this.width = width; | 
				
			|||
            this.height = height; | 
				
			|||
        } | 
				
			|||
 | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public static class Point{ | 
				
			|||
        public int x; | 
				
			|||
        public int y; | 
				
			|||
 | 
				
			|||
        public Point(int x, int y) { | 
				
			|||
            this.x = x; | 
				
			|||
            this.y = y; | 
				
			|||
        } | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,39 @@ | 
				
			|||
package com.visual.open.anpr.core.domain; | 
				
			|||
 | 
				
			|||
import org.opencv.core.Point; | 
				
			|||
 | 
				
			|||
import java.util.List; | 
				
			|||
 | 
				
			|||
public class Polygon { | 
				
			|||
    private List<Point> points; | 
				
			|||
 | 
				
			|||
    public Polygon(List<Point> points){ | 
				
			|||
        this.points = points; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public double getArea(){ | 
				
			|||
        int i, j; | 
				
			|||
        double area = 0; | 
				
			|||
        for (i = 0; i < this.points.size(); i++) | 
				
			|||
        { | 
				
			|||
            j = (i + 1) % this.points.size(); | 
				
			|||
            area += this.points.get(i).x * this.points.get(j).y; | 
				
			|||
            area -= this.points.get(i).y * this.points.get(j).x; | 
				
			|||
        } | 
				
			|||
        area /= 2; | 
				
			|||
        return Math.abs(area); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    public double getLength(){ | 
				
			|||
        double result = 0; | 
				
			|||
        for (int i = 0; i < this.points.size(); i++){ | 
				
			|||
            Point u = points.get(i); | 
				
			|||
            Point v = (i+1==this.points.size()) ? points.get(0): points.get(i+1); | 
				
			|||
            double m = u.x - v.x; | 
				
			|||
            double n = u.y - v.y; | 
				
			|||
            result += Math.sqrt(m * m + n * n); | 
				
			|||
        } | 
				
			|||
        return result; | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
} | 
				
			|||
@ -0,0 +1,41 @@ | 
				
			|||
package com.visual.open.anpr.core.models; | 
				
			|||
 | 
				
			|||
import com.visual.open.anpr.core.domain.DrawImage; | 
				
			|||
import com.visual.open.anpr.core.domain.ImageMat; | 
				
			|||
import com.visual.open.anpr.core.domain.PlateInfo; | 
				
			|||
 | 
				
			|||
import java.awt.*; | 
				
			|||
import java.util.HashMap; | 
				
			|||
import java.util.List; | 
				
			|||
 | 
				
			|||
public class PaddlePlateDetectionTest { | 
				
			|||
 | 
				
			|||
    public static void main(String[] args) { | 
				
			|||
        PaddlePlateDetection detection = new PaddlePlateDetection("open-anpr-core/src/main/resources/models/det.onnx", 1); | 
				
			|||
 | 
				
			|||
        String imagePath = "open-anpr-core/src/test/resources/images/image005.jpg"; | 
				
			|||
        ImageMat imageMat = ImageMat.fromImage(imagePath); | 
				
			|||
        List<PlateInfo> plateInfos = detection.inference(imageMat, 1,1, new HashMap<>()); | 
				
			|||
 | 
				
			|||
        DrawImage drawImage = DrawImage.build(imagePath); | 
				
			|||
        for(PlateInfo plateInfo : plateInfos){ | 
				
			|||
            PlateInfo.Point [] points = plateInfo.box.toArray(); | 
				
			|||
            for(int i =0; i< points.length; i++){ | 
				
			|||
                if(i+1 == points.length){ | 
				
			|||
                    drawImage.drawLine( | 
				
			|||
                            new DrawImage.Point((int)points[i].x, (int)points[i].y), | 
				
			|||
                            new DrawImage.Point((int)points[0].x, (int)points[0].y), | 
				
			|||
                            2, Color.RED | 
				
			|||
                    ); | 
				
			|||
                }else{ | 
				
			|||
                    drawImage.drawLine( | 
				
			|||
                            new DrawImage.Point((int)points[i].x, (int)points[i].y), | 
				
			|||
                            new DrawImage.Point((int)points[i+1].x, (int)points[i+1].y), | 
				
			|||
                            2, Color.RED | 
				
			|||
                    ); | 
				
			|||
                } | 
				
			|||
            } | 
				
			|||
        } | 
				
			|||
        ImageMat.fromCVMat(drawImage.toMat()).imShow(); | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
| 
		 After Width: | Height: | Size: 107 KiB  | 
| 
		 After Width: | Height: | Size: 64 KiB  | 
| 
		 After Width: | Height: | Size: 40 KiB  | 
| 
		 After Width: | Height: | Size: 390 KiB  | 
| 
		 After Width: | Height: | Size: 67 KiB  | 
					Loading…
					
					
				
		Reference in new issue