如何在opencv android应用程序中读取.cfg和.weights文件

如何在opencv android应用程序中读取.cfg和.weights文件,android,opencv,yolo,Android,Opencv,Yolo,我试着用 我把文件保存在手机的文件夹里 `String cfgPath= Environment.getExternalStorageDirectory().getPath()+"/lumber/yolov3_custom.cfg"; String weightsPath=Environment.getExternalStorageDirectory().getPath()+"/lumber/yolov3_custom_final.weights"; yo

我试着用 我把文件保存在手机的文件夹里

`String cfgPath= Environment.getExternalStorageDirectory().getPath()+"/lumber/yolov3_custom.cfg";
String weightsPath=Environment.getExternalStorageDirectory().getPath()+"/lumber/yolov3_custom_final.weights";
yoloModel= Dnn.readNetFromDarknet(cfgPath,weightsPath);`
不起作用 我还尝试使用资产文件夹中的输入流,然后保存文件

AssetManager assetManager= getAssets();
InputStream cfgStream= assetManager.open("yolov3_custom.cfg");
byte[] buffer= new byte[cfgStream.available()];
cfgStream.read(buffer);
File cfgFile = new File(getFilesDir(), "yolov3_customCp.cfg");
OutputStream outCfgStream= new FileOutputStream(cfgFile);
outCfgStream.write(buffer);
String cfgPath= cfgFile.getAbsolutePath();
Log.d("path: ", cfgPath);

InputStream weightsStream= assetManager.open("yolov3_custom_final.weights");
byte[] _buffer= new byte[weightsStream.available()];
weightsStream.read(_buffer);
File weightsFile= new File(getFilesDir(), "yolo_customCp.weights");
OutputStream outWeightsStream=new FileOutputStream(weightsFile);
outWeightsStream.write(_buffer);
String weightsPath= weightsFile.getAbsolutePath();
我仍然无法读取Dnn.ReadFromDarknet()中的文件

有没有办法做到这一点


另外,对于使用
getApplicationContext().getFilesDir().getAbsolutePath()
我应该将文件放在哪里?

您添加了权限吗? 您可以在AndroidManifest.xml中添加权限

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.something">
    
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
        <application
        .....
@Override
protected void onStart() {
        super.onStart();
    
        // Check External Storage Permission
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    
            // Request permission
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION);
     

   }
}