Environment: Windows 10 Pro
OpenCV: 4.5.1, I downloaded from https://opencv.org/releases/
yolov3.cfg: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3.cfg
yolov3.weights: https://pjreddie.com/media/files/yolov3.weights
yolov3-tiny.cfg: https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov3-tiny.cfg
yolov3-tiny.weights: https://pjreddie.com/media/files/yolov3-tiny.weights
coco.names: https://github.com/AlexeyAB/darknet/raw/master/data/coco.names
My code:
static final Scalar scalar = new Scalar(0); static final Size sz = new Size(13*16*4,13*9*4); // new Size(416,416); static final Size cfgSize = new Size(608, 608); static final float scale = 1F/255F; static final boolean swapRB = false; private static List<String> getOutputNames(Net net) { List<String> names = new ArrayList<>(); List<Integer> outLayers = net.getUnconnectedOutLayers().toList(); List<String> layersNames = net.getLayerNames(); outLayers.forEach((item) -> names.add(layersNames.get(item - 1))); return names; } public static void test1() { System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME); System.loadLibrary("opencv_videoio_ffmpeg451_64"); List<Mat> result = new ArrayList<>(); String cfgFile = "E:/yolo/yolo-coco/yolov3.cfg"; String weightsFile = "E:/yolo/yolo-coco/yolov3.weights"; String labelFils = "E:/yolo/yolo-coco/coco.names"; String videoFile = "E:/AV/Vehicles/video_SECTION-1.mp4"; Net net = Dnn.readNetFromDarknet(cfgFile, weightsFile); List<String> outputLayers = getOutputNames(net); log.trace("outputLayers:{}", outputLayers); VideoCapture vs = new VideoCapture(videoFile); log.trace("VideoCapture is opened? {}", vs.isOpened()); Mat frame = new Mat(); boolean readOk = vs.read(frame); log.trace("readOk:{}, frame size:{}", readOk, frame.size()); Mat inputBlob = Dnn.blobFromImage(frame, scale, sz, scalar, swapRB, false); net.setInput(inputBlob); log.debug("CHECK POINT 00001"); net.forward(result, outputLayers); // outBlobNames:[yolo_82, yolo_94, yolo_106] log.trace("Aftert blobFromImage "); }
Output:
43:58.278 INFO OpencvTest: Loading opencv_java451
14:43:58.529 TRACE OpencvTest: outputLayers:[yolo_82, yolo_94, yolo_106]
14:43:58.552 TRACE OpencvTest: VideoCapture is opened? true
14:43:58.574 TRACE OpencvTest: readOk:true, frame size:1280x720
14:43:58.581 DEBUG OpencvTest: CHECK POINT 00001
Errors:
[ERROR:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\dnn.cpp (3498) cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively OPENCV/DNN: [Concat]:(concat_98): getMemoryShapes() throws exception. inputs=2 outputs=1/1 blobs=0
[ERROR:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\dnn.cpp (3501) cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively input[0] = [ 1 128 60 104 ]
[ERROR:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\dnn.cpp (3501) cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively input[1] = [ 1 256 59 104 ]
[ERROR:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\dnn.cpp (3505) cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively output[0] = [ 1 128 60 104 ]
[ERROR:0] global C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\dnn.cpp (3511) cv::dnn::dnn4_v20201117::Net::Impl::getLayerShapesRecursively Exception message: OpenCV(4.5.1) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\layers\concat_layer.cpp:102: error: (-201:Incorrect size of input array) Inconsistent shape for ConcatLayer in function ‘cv::dnn::ConcatLayerImpl::getMemoryShapes’Exception in thread “main” CvException [org.opencv.core.CvException: cv::Exception: OpenCV(4.5.1) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\dnn\src\layers\concat_layer.cpp:102: error: (-201:Incorrect size of input array) Inconsistent shape for ConcatLayer in function ‘cv::dnn::ConcatLayerImpl::getMemoryShapes’
]
at org.opencv.dnn.Net.forward_4(Native Method)
at org.opencv.dnn.Net.forward(Net.java:289)
at com.jcarxmate.test.OpencvTest.test1(OpencvTest.java:105)
at com.jcarxmate.test.OpencvTest.main(OpencvTest.java:120)
However, with same logics, same opencv, same weights, same cfg, and same test video file in python. it works.
And if I test with yolov3-tiny in both Java and Python, everything works.
So, any suggestion?
Many thanks.