vendor: OpenCV 5.0.0 snapshot at 40738fb16ceddb5fb3fea747585f7ce6abb0605b

This commit is contained in:
Gitea Mirror Bot
2026-08-22 00:10:33 +08:00
commit f7f077da11
6933 changed files with 2335208 additions and 0 deletions
@@ -0,0 +1,149 @@
package org.opencv.test.dnn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.core.Range;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Image2BlobParams;
import org.opencv.test.OpenCVTestCase;
public class DnnBlobFromImageWithParamsTest extends OpenCVTestCase {
// test for DATA_LAYOUT_* and DNN_LAYOUT_* access from Core
public void testDataLayoutConstants()
{
assertEquals(0, Core.DATA_LAYOUT_UNKNOWN);
assertEquals(1, Core.DATA_LAYOUT_ND);
assertEquals(2, Core.DATA_LAYOUT_NCHW);
assertEquals(3, Core.DATA_LAYOUT_NCDHW);
assertEquals(4, Core.DATA_LAYOUT_NHWC);
assertEquals(5, Core.DATA_LAYOUT_NDHWC);
assertEquals(6, Core.DATA_LAYOUT_PLANAR);
assertEquals(7, Core.DATA_LAYOUT_BLOCK);
}
public void testBlobFromImageWithParamsNHWCScalarScale()
{
// https://github.com/opencv/opencv/issues/27264
Mat img = new Mat(10, 10, CvType.CV_8UC4, new Scalar(0, 1, 2, 3));
Scalar scalefactor = new Scalar(0.1, 0.2, 0.3, 0.4);
Image2BlobParams params = new Image2BlobParams();
params.set_scalefactor(scalefactor);
params.set_datalayout(Core.DATA_LAYOUT_NHWC);
Mat blob = Dnn.blobFromImageWithParams(img, params); // [1, 10, 10, 4]
float[] expectedValues = { (float)scalefactor.val[0] * 0, (float)scalefactor.val[1] * 1, (float)scalefactor.val[2] * 2, (float)scalefactor.val[3] * 3 }; // Target Value.
for (int h = 0; h < 10; h++)
{
for (int w = 0; w < 10; w++)
{
float[] actualValues = new float[4];
blob.get(new int[]{0, h, w, 0}, actualValues);
for (int c = 0; c < 4; c++)
{
// Check equal
assertEquals(expectedValues[c], actualValues[c]);
}
}
}
}
public void testBlobFromImageWithParamsCustomPaddingLetterBox()
{
Mat img = new Mat(40, 20, CvType.CV_8UC4, new Scalar(0, 1, 2, 3));
// Custom padding value that you have added
Scalar customPaddingValue = new Scalar(5, 6, 7, 8); // Example padding value
Size targetSize = new Size(20, 20);
Mat targetImg = img.clone();
Core.copyMakeBorder(targetImg, targetImg, 0, 0, (int)targetSize.width / 2, (int)targetSize.width / 2, Core.BORDER_CONSTANT, customPaddingValue);
// Set up Image2BlobParams with your new functionality
Image2BlobParams params = new Image2BlobParams();
params.set_size(targetSize);
params.set_paddingmode(Dnn.DNN_PMODE_LETTERBOX);
params.set_borderValue(customPaddingValue); // Use your new feature here
// Create blob with custom padding
Mat blob = Dnn.blobFromImageWithParams(img, params);
// Create target blob for comparison
Mat targetBlob = Dnn.blobFromImage(targetImg, 1.0, targetSize);
assertEquals(0, Core.norm(targetBlob, blob, Core.NORM_INF), EPS);
}
public void testBlobFromImageWithParams4chLetterBox()
{
Mat img = new Mat(40, 20, CvType.CV_8UC4, new Scalar(0, 1, 2, 3));
// Construct target mat.
Mat[] targetChannels = new Mat[4];
// The letterbox will add zero at the left and right of output blob.
// After the letterbox, every row data would have same value showing as valVec.
byte[] valVec = { 0,0,0,0,0, 1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0};
Mat rowM = new Mat(1, 20, CvType.CV_8UC1);
rowM.put(0, 0, valVec);
for (int i = 0; i < 4; i++) {
Core.multiply(rowM, new Scalar(i), targetChannels[i] = new Mat());
}
Mat targetImg = new Mat();
Core.merge(Arrays.asList(targetChannels), targetImg);
Size targetSize = new Size(20, 20);
Image2BlobParams params = new Image2BlobParams();
params.set_size(targetSize);
params.set_paddingmode(Dnn.DNN_PMODE_LETTERBOX);
Mat blob = Dnn.blobFromImageWithParams(img, params);
Mat targetBlob = Dnn.blobFromImage(targetImg, 1.0, targetSize); // only convert data from uint8 to float32.
assertEquals(0, Core.norm(targetBlob, blob, Core.NORM_INF), EPS);
}
// https://github.com/opencv/opencv/issues/27264
public void testBlobFromImageWithParams4chMultiImage()
{
Mat img = new Mat(10, 10, CvType.CV_8UC4, new Scalar(0, 1, 2, 3));
Scalar scalefactor = new Scalar(0.1, 0.2, 0.3, 0.4);
Image2BlobParams param = new Image2BlobParams();
param.set_scalefactor(scalefactor);
param.set_datalayout(Core.DATA_LAYOUT_NHWC);
List<Mat> images = new ArrayList<>();
images.add(img);
Mat img2 = new Mat();
Core.multiply(img, Scalar.all(2), img2);
images.add(img2);
Mat blobs = Dnn.blobFromImagesWithParams(images, param);
Range[] ranges = new Range[4];
ranges[0] = new Range(0, 1);
ranges[1] = new Range(0, blobs.size(1));
ranges[2] = new Range(0, blobs.size(2));
ranges[3] = new Range(0, blobs.size(3));
Mat blob0 = blobs.submat(ranges).clone();
ranges[0] = new Range(1, 2);
Mat blob1 = blobs.submat(ranges).clone();
Core.multiply(blob0, Scalar.all(2), blob0);
assertEquals(0, Core.norm(blob0, blob1, Core.NORM_INF), EPS);
}
}
@@ -0,0 +1,71 @@
package org.opencv.test.dnn;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Net;
import org.opencv.test.OpenCVTestCase;
public class DnnForwardAndRetrieve extends OpenCVTestCase {
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
private String modelFileName = "";
@Override
protected void setUp() throws Exception {
super.setUp();
String dnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
String generalTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
File model = null;
if (generalTestDataPath != null) {
model = new File(generalTestDataPath, "dnn/onnx/models/split_0.onnx");
}
if ((model == null || !model.isFile()) && dnnTestDataPath != null) {
model = new File(dnnTestDataPath, "dnn/onnx/models/split_0.onnx");
}
if (model == null || !model.isFile()) {
isTestCaseEnabled = false;
return;
}
modelFileName = model.getAbsolutePath();
}
public void testForwardAndRetrieve()
{
// Verifies forwardAndRetrieve nested list marshalling using a small ONNX model instead of the removed Caffe importer.
Net net = Dnn.readNetFromONNX(modelFileName, Dnn.ENGINE_CLASSIC);
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
// split_0.onnx declares a single 4D input named "image" of shape [1, 3, 2, 2].
Mat inp = new Mat(new int[]{1, 3, 2, 2}, CvType.CV_32F);
Core.randu(inp, -1, 1);
net.setInput(inp);
List<String> outNames = net.getUnconnectedOutLayersNames();
assertFalse("Model has no output layers", outNames.isEmpty());
// Forward and retrieve every output blob of the requested layers.
List<List<Mat>> outBlobs = new ArrayList<>();
net.forwardAndRetrieve(outBlobs, outNames);
// One entry per requested layer name, each holding at least one valid blob.
assertEquals(outNames.size(), outBlobs.size());
for (List<Mat> blobs : outBlobs) {
assertFalse(blobs.isEmpty());
for (Mat blob : blobs)
assertFalse(blob.empty());
}
}
}
@@ -0,0 +1,117 @@
package org.opencv.test.dnn;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.DictValue;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Layer;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.test.OpenCVTestCase;
/*
* regression test for #12324,
* testing various java.util.List invocations,
* which use the LIST_GET macro
*/
public class DnnListRegressionTest extends OpenCVTestCase {
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
String modelFileName = "";
String sourceImageFile = "";
Net net;
@Override
protected void setUp() throws Exception {
super.setUp();
String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
if(envDnnTestDataPath == null){
isTestCaseEnabled = false;
return;
}
File dnnTestDataPath = new File(envDnnTestDataPath);
modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();
String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
File testDataPath = new File(envTestDataPath);
File f = new File(testDataPath, "dnn/grace_hopper_227.png");
sourceImageFile = f.toString();
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
net = Dnn.readNetFromTensorflow(modelFileName);
Mat image = Imgcodecs.imread(sourceImageFile);
assertNotNull("Loading image from file failed!", image);
Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
assertNotNull("Converting image to blob failed!", inputBlob);
net.setInput(inputBlob, "");
}
/*public void testSetInputsNames() {
List<String> inputs = new ArrayList();
inputs.add("input");
try {
net.setInputsNames(inputs);
} catch(Exception e) {
fail("Net setInputsNames failed: " + e.getMessage());
}
}*/
public void testForward() {
Mat out;
try {
out = net.forward();
} catch(Exception e) {
fail("Net forward failed: " + e.getMessage());
}
}
public void testGetMemoryConsumption() {
List<MatOfInt> netInputShapes = new ArrayList();
netInputShapes.add(new MatOfInt(1, 3, 224, 224));
MatOfInt netInputTypes = new MatOfInt(5);
long[] weights=null;
long[] blobs=null;
try {
net.getMemoryConsumption(netInputShapes, netInputTypes, weights, blobs);
} catch(Exception e) {
fail("Net getMemoryConsumption failed: " + e.getMessage());
}
}
public void testGetFLOPS() {
List<MatOfInt> netInputShapes = new ArrayList();
netInputShapes.add(new MatOfInt(1, 3, 224, 224));
MatOfInt netInputTypes = new MatOfInt(5);
try {
net.getFLOPS(netInputShapes, netInputTypes);
} catch(Exception e) {
fail("Net getFLOPS failed: " + e.getMessage());
}
}
}
@@ -0,0 +1,144 @@
package org.opencv.test.dnn;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.dnn.DictValue;
import org.opencv.dnn.Dnn;
import org.opencv.dnn.Layer;
import org.opencv.dnn.Net;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.test.OpenCVTestCase;
public class DnnTensorFlowTest extends OpenCVTestCase {
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH";
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH";
String modelFileName = "";
String sourceImageFile = "";
Net net;
private static void normAssert(Mat ref, Mat test) {
final double l1 = 1e-5;
final double lInf = 1e-4;
double normL1 = Core.norm(ref, test, Core.NORM_L1) / ref.total();
double normLInf = Core.norm(ref, test, Core.NORM_INF) / ref.total();
assertTrue(normL1 < l1);
assertTrue(normLInf < lInf);
}
@Override
protected void setUp() throws Exception {
super.setUp();
String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH);
if(envDnnTestDataPath == null){
isTestCaseEnabled = false;
return;
}
File dnnTestDataPath = new File(envDnnTestDataPath);
modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString();
String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH);
if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!");
File testDataPath = new File(envTestDataPath);
File f = new File(testDataPath, "dnn/grace_hopper_227.png");
sourceImageFile = f.toString();
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile);
net = Dnn.readNetFromTensorflow(modelFileName);
}
public void testGetLayerTypes() {
List<String> layertypes = new ArrayList();
net.getLayerTypes(layertypes);
assertFalse("No layer types returned!", layertypes.isEmpty());
}
public void testGetLayer() {
List<String> layerNames = net.getLayerNames();
assertFalse("Test net returned no layers!", layerNames.isEmpty());
int layerId = 0;
for (String layerName: layerNames) {
Layer layer = net.getLayer(layerId);
assertEquals("Layer name does not match the expected value!", layerName, layer.get_name());
layerId++;
}
}
public void checkInceptionNet(Net net)
{
Mat image = Imgcodecs.imread(sourceImageFile);
assertNotNull("Loading image from file failed!", image);
Mat inputBlob = Dnn.blobFromImage(image, 1.0, new Size(224, 224), new Scalar(0), true, true);
assertNotNull("Converting image to blob failed!", inputBlob);
net.setInput(inputBlob, "");
Mat result = new Mat();
try {
net.setPreferableBackend(Dnn.DNN_BACKEND_OPENCV);
result = net.forward("");
}
catch (Exception e) {
fail("DNN forward failed: " + e.getMessage());
}
assertNotNull("Net returned no result!", result);
result = result.reshape(1, 1);
Core.MinMaxLocResult minmax = Core.minMaxLoc(result);
assertEquals("Wrong prediction", (int)minmax.maxLoc.x, 866);
Mat top5RefScores = new MatOfFloat(new float[] {
0.63032645f, 0.2561979f, 0.032181446f, 0.015721032f, 0.014785315f
}).reshape(1, 1);
Core.sort(result, result, Core.SORT_DESCENDING);
normAssert(result.colRange(0, 5), top5RefScores);
}
public void testTestNetForward() {
checkInceptionNet(net);
}
public void testReadFromBuffer() {
File modelFile = new File(modelFileName);
byte[] modelBuffer = new byte[ (int)modelFile.length() ];
try {
FileInputStream fis = new FileInputStream(modelFile);
fis.read(modelBuffer);
fis.close();
} catch (IOException e) {
fail("Failed to read a model: " + e.getMessage());
}
net = Dnn.readNetFromTensorflow(new MatOfByte(modelBuffer));
checkInceptionNet(net);
}
public void testGetAvailableTargets() {
List<Integer> targets = Dnn.getAvailableTargets(Dnn.DNN_BACKEND_OPENCV);
assertTrue(targets.contains(Dnn.DNN_TARGET_CPU));
}
}