Loading the SED model in android
Hi everyone ,
I used this code to implement an Structured Edge Detection model in android but I ran to an error.
Code Snippet :
private Bitmap StructuredDetectEdges (Bitmap bitmap){ Mat rgba = new Mat(); Utils.bitmapToMat(bitmap,rgba); Mat edges = new Mat(rgba.size(), CvType.CV_8UC1); Imgproc.cvtColor(rgba,edges,Imgproc.COLOR_RGB2GRAY,4); StructuredEdgeDetection pDollar = createStructuredEdgeDetection("file:///android_asset/SEDmodel.yml"); pDollar.detectEdges(edges,edges); Bitmap resultBitmap = Bitmap.createBitmap(edges.cols(), edges.rows(), Bitmap.Config.ARGB_8888); Utils.matToBitmap(edges, resultBitmap); return resultBitmap; }
Error that I got :
E/cv::error(): OpenCV(3.4.0-dev) Error: Assertion failed (modelFile.isOpened()) in cv::ximgproc::StructuredEdgeDetectionImpl::StructuredEdgeDetectionImpl(const cv::String&, cv::Ptr<const cv::ximgproc::rffeaturegetter="">), file /Users/Chao/opencv_contrib/modules/ximgproc/src/structured_edge_detection.cpp, line 432
Any help on how can I solve this problem is much appreciated.
you can't read from the assets folder directly (those are inside a zip).
you'll have to copy it to your sdcard first, and read from there.
@berak thank you for the tip !! I managed to call the model correctly , but now I'm having a new error :
I'm using the same code above. It seems that there is a problem with the type of the source matrix. Any idea how can this be fixed ?
you have to convert your img to float (also scale to [0..1]) before using it, see docs, please
Thank you again for the information, the documentation you linked is for c++ ! since I'm working with java I should consult the javadoc link . The detectEdges method takes Mat type for both source and destination. And that's what I did in the code above ! I converted my bitmap (img) to a Mat (matrix) and fed it to the detectEdges method. Correct me please if I'm wrong or missing something. Much appreciated
Hi there @charara, I have exactly same problem as you has originally with modelFile.isOpened(), but I haven't been able to fix the problem even though I've put model.yml.gz into the downloads folder on my android device. Would you mind sharing what you did? ie: Where you put the model file and whether you opened it in any way before passing the file name to Ximgproc.createStructuredEdgeDetection. and whether you just put in "model.yml.gz" or whether you included the path? (I assume you did?) Thanks in advance.
@kayjay , try with an absolute path to the file.
@berak if I look on my device, the path to the model file is /sdcard/Download/model.yml. But unfortunately my code (below) still crashes with the same model.isOpened() error. String path = "/sdcard/Download/model.yml"; StructuredEdgeDetection edge_detection = Ximgproc.createStructuredEdgeDetection(path);
Do you perhaps have any other ideas? My read external storage permission is in my manifest file.
I just solved the problem! Although I'd enabled the permission in my manifest file, I hadn't clicked that I wasn't asked to allow permissions at runtime...so I went to Settings -> Apps -> {Your App} -> Permissions and granted storage access there. Solved. Thanks for your help!
you also probably should leave it as
model.yml.gz
, like it was, originally