【问题标题】:How to read .cfg and .weights file in opencv android app如何在 opencv android 应用程序中读取 .cfg 和 .weights 文件
【发布时间】:2021-04-28 01:35:07
【问题描述】:

我尝试使用 我的文件保存在手机的木材文件夹中

`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(),我应该把我的文件放在哪里?

【问题讨论】:

    标签: android opencv yolo


    【解决方案1】:

    您是否添加了权限? 您可以在 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
            .....
    

    然后在MainActivity onStart()中添加权限请求

    @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);
         
    
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2016-07-18
      • 2013-06-19
      • 2014-03-04
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      相关资源
      最近更新 更多