工具类
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.TextDetectionModel_DB;
import org.opencv.dnn.TextRecognitionModel;
import org.opencv.highgui.HighGui;
import org.opencv.highgui.ImageWindow;
import org.opencv.imgproc.Imgproc;
public class OpencvUtil {
static String textRecognition(Mat mat, Size inputSize1) {
Path path = Paths.get("src","main","resources","text_recognition_CRNN_CH_2023feb_fp16.onnx");
System.out.println(path.toAbsolutePath());
TextRecognitionModel rec = new TextRecognitionModel(path.toAbsolutePath().toString());
rec.setDecodeType("CTC-prefix-beam-search");
try {
Path vocabulary = Paths.get("src","main","resources","alphabet_94.txt");
rec.setVocabulary(Files.readAllLines(vocabulary.toAbsolutePath()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Normalization parameters
double scale = 1.0 / 127.5;
Scalar mean = new Scalar(127.5, 127.5, 127.5);
// The input shape
Size inputSize = new Size(100, 32);
rec.setInputParams(scale, inputSize, mean);
// Mat mat= Imgcodecs.imread("d:\\text_rec_test.png");
String str = rec.recognize(mat);
// System.out.println(str);
// imshow("展示图片", mat);
return str;
}
static List<MatOfPoint> textDetection(Mat mat) {
Path path = Paths.get("src","main","resources","text_detection_DB_TD500_resnet18_2021sep.onnx");
System.out.println(path.toAbsolutePath());
TextDetectionModel_DB model = new TextDetectionModel_DB(path.toAbsolutePath().toString());
// Post-processing parameters
float binThresh = 0.3f;
float polyThresh = 0.5f;
int maxCandidates = 200;
double unclipRatio = 2.0;
model.setBinaryThreshold(binThresh)
.setPolygonThreshold(polyThresh)
.setMaxCandidates(maxCandidates)
.setUnclipRatio(unclipRatio)
;
// Normalization parameters
double scale = 1.0 / 255.0;
Scalar mean = new Scalar(122.67891434, 116.66876762, 104.00698793);
// The input shape
Size inputSize = new Size(736, 736);
model.setInputParams(scale, inputSize, mean);
// TextDetectionModel_EAST model = new TextDetectionModel_EAST("EAST.pb");
// float confThreshold = 0.5f;
// float nmsThreshold = 0.4f;
// model.setConfidenceThreshold(confThreshold)
// .setNMSThreshold(nmsThreshold)
// ;
// double detScale = 1.0;
// Size detInputSize = new Size(320, 320);
// Scalar detMean = new Scalar(123.68, 116.78, 103.94);
// boolean swapRB = true;
// model.setInputParams(detScale, detInputSize, detMean, swapRB);
List<MatOfPoint> detections = new ArrayList<MatOfPoint>();
model.detect(mat, detections);
// if(detections.size()>0) {
// Imgproc.polylines(mat, detections, true, new Scalar(0,255,0), 2);
// }
//
// imshow("展示图片", mat);
return detections;
}
static void imshow(String name, Mat mat){
HighGui.imshow(name, mat);
HighGui.waitKey();
ImageWindow tmpWin = HighGui.windows.get(name);
tmpWin.frame.setDefaultCloseOperation(2);
HighGui.destroyWindow(name);
tmpWin.frame.dispose();
}
static void fourPointsTransform(Mat frame, MatOfPoint2f vertices, Mat result, Size outputSize)
{
MatOfPoint2f targetVertices = new MatOfPoint2f(
new Point(0, outputSize.height - 1),
new Point(0, 0),
new Point(outputSize.width - 1, 0),
new Point(outputSize.width - 1, outputSize.height - 1)
);
System.out.println(vertices.toList());
System.out.println(targetVertices.toList());
Mat rotationMatrix = Imgproc.getPerspectiveTransform(vertices, targetVertices);
Imgproc.warpPerspective(frame, result, rotationMatrix, outputSize);
}
static Size calcSize(MatOfPoint2f point2f) {
Point[] points = point2f.toArray();
double width = ((points[3].x-points[0].x)+(points[2].x-points[1].x))/2;
double height = ((points[0].y-points[1].y)+(points[3].y-points[2].y))/2;
return new Size(width, height);
}
}
调用
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class Library {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat= Imgcodecs.imread("d:\\text-ce.jpg");
System.out.println(mat);
List<MatOfPoint> matOfPoints = OpencvUtil.textDetection(mat);
if(matOfPoints.size()>0) {
List<MatOfPoint> contours = OpencvUtil.textDetection(mat);
Scalar txt = new Scalar(0, 0, 255);
for (int i = 0; i <matOfPoints.size(); i++) {
MatOfPoint quadrangle = matOfPoints.get(i);
if(quadrangle.depth()!=4)continue;
MatOfPoint2f point2f = new MatOfPoint2f();
quadrangle.convertTo(point2f, CvType.CV_32F);
Size p2fSize = OpencvUtil.calcSize(point2f);
Mat submat = new Mat(p2fSize, mat.type());
OpencvUtil.fourPointsTransform(mat, point2f, submat, p2fSize);
String ss=OpencvUtil.textRecognition(submat, p2fSize);
System.out.println(ss);
Imgproc.putText(mat, i+ss, quadrangle.toList().get(quadrangle.toList().size()-1), 1, 1, txt);
}
Imgproc.polylines(mat, contours, true, new Scalar(0,255,0), 1);
}
OpencvUtil.imshow("展示图片", mat);
// OpencvUtil.textRecognition();
}
}
识别效果太差