簡體   English   中英

Android 上的 OpenCV:net.forward 產生“215 斷言失敗”

[英]OpenCV on Android: net.forward yields "215 Assertion failed"

遵循openCV 的本教程,它應該是直截了當的。 但是,它因 net.forward 上的斷言失敗而崩潰,我無法在其他任何地方解決/找到該問題。

認為這個問題看起來很相似,並試圖通過修復/問題發現。 然而,重新開始討論和試驗表明情況可能不一樣。 我最初使用的是 3.4.3,它以某種方式不支持相同的 Mat 類型。 現在更新到 3.4.7,可以確認 blob 大小沒問題(從圖像生成)。 還嘗試了各種其他 prototxt 和 caffemodels,但現在懷疑問題出在那里(如果文件沒問題,則工作正常,否則網絡加載失敗)。 關鍵代碼應該是這樣的:

// Load a network.
public void onCameraViewStarted(int width, int height) {
    String proto = getPath("deploy.prototxt", this);
    String weights = getPath("MobileNetSSD_deploy.caffemodel", this);
    net = Dnn.readNetFromCaffe(proto, weights);
    Log.i(TAG, "Network loaded successfully");
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    // Get a new frame
    Mat frame = inputFrame.rgba();
    Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB);
    // Forward image through network.
    Mat blob = Dnn.blobFromImage(frame, 0.007843,
            new Size(300, 300),
            new Scalar(127.5, 127.5, 127.5));

    net.setInput(blob);
    Mat detections = net.forward(); //***215 ASSERTION FAILED occurs***
    int cols = frame.cols();
    int rows = frame.rows();
    detections = detections.reshape(1, (int)detections.total() / 7);
    for (int i = 0; i < detections.rows(); ++i) {
        double confidence = detections.get(i, 2)[0];
        if (confidence > 0.2) {
            int classId = (int)detections.get(i, 1)[0];
            int left   = (int)(detections.get(i, 3)[0] * cols);
            int top    = (int)(detections.get(i, 4)[0] * rows);
            int right  = (int)(detections.get(i, 5)[0] * cols);
            int bottom = (int)(detections.get(i, 6)[0] * rows);
            // Draw rectangle around detected object.
            Imgproc.rectangle(frame, new Point(left, top), new Point(right, bottom),
                    new Scalar(0, 255, 0));
            String label = classNames[classId] + ": " + confidence;
            int[] baseLine = new int[1];
            Size labelSize = Imgproc.getTextSize(label, Core.FONT_HERSHEY_SIMPLEX, 0.5, 1, baseLine);
            // Draw background for label.
            Imgproc.rectangle(frame, new Point(left, top - labelSize.height),
                    new Point(left + labelSize.width, top + baseLine[0]),
                    new Scalar(255, 255, 255), Core.FILLED);
            // Write class name and confidence.
            Imgproc.putText(frame, label, new Point(left, top),
                    Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(0, 0, 0));
        }
    }
    return frame;
}
public void onCameraViewStopped() {}
// Upload file to storage and return a path.
private static String getPath(String file, Context context) {
    AssetManager assetManager = context.getAssets();
    BufferedInputStream inputStream = null;
    try {
        // Read data from assets.
        inputStream = new BufferedInputStream(assetManager.open(file));
        byte[] data = new byte[inputStream.available()];
        inputStream.read(data);
        inputStream.close();
        // Create copy file in storage.
        File outFile = new File(context.getFilesDir(), file);
        FileOutputStream os = new FileOutputStream(outFile);
        os.write(data);
        os.close();
        // Return a path to file which may be read in common way.
        return outFile.getAbsolutePath();
    } catch (IOException ex) {
        Log.i(TAG, "Failed to upload a file");
    }
    return "";
}

完整的錯誤信息是

cv::Exception: OpenCV(3.4.7) /build/3_4_pack-android/opencv/modules/dnn/src/layers/batch_norm_layer.cpp:39: 錯誤: (-215:Assertion failed) blobs.size() >= 2 函數 'cv::dnn::BatchNormLayerImpl::BatchNormLayerImpl(const cv::dnn::experimental_dnn_34_v13::LayerParams&)'

我希望它不會崩潰。 框架應該沒問題(圖像加載),網絡不是空的,網絡中的層也很好(檢查,因為在 Java 中使用 caffe 存在一些差異)。 任何幫助表示贊賞!

經過幾天不同方向的研究,我發現了問題:幀格式應該是BGR,而不是RGB! 這意味着

Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2BGR);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM