0

私はアンドロイド用のアプリを書いており、カフェライブラリを使用しています。私の問題は、開始時に caffe を初期化する必要があることです。これは、2 つのファイル (ネットワークの構造) を caffe に渡すことによって行われます。問題は、余分なファイルをデバイスに保存する方法がわからないことです。モデル ファイルをアセットに追加しましたが、ファイル パスを使用してそれを読み取る方法がわかりません。ファイル パスを使用してアクセスできるこれらのファイルの保存場所を教えてください。

アイデアをありがとう。

4

3 に答える 3

2

これでうまくいくはずです。これらのファイルをアセット フォルダーからデータ ディレクトリにコピーするだけです。それらのファイルが既にある場合は、それらをロードするだけです。

String toPath = "/data/data/" + getPackageName();  // Your application path




private static boolean copyAssetFolder(AssetManager assetManager,
            String fromAssetPath, String toPath) {
        try {
            String[] files = assetManager.list(fromAssetPath);
            new File(toPath).mkdirs();
            boolean res = true;
            for (String file : files)
                if (file.contains("."))
                    res &= copyAsset(assetManager, 
                            fromAssetPath + "/" + file,
                            toPath + "/" + file);
                else 
                    res &= copyAssetFolder(assetManager, 
                            fromAssetPath + "/" + file,
                            toPath + "/" + file);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

private static boolean copyAsset(AssetManager assetManager,
        String fromAssetPath, String toPath) {
    InputStream in = null;
    OutputStream out = null;
    try {
      in = assetManager.open(fromAssetPath);
      new File(toPath).createNewFile();
      out = new FileOutputStream(toPath);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;
      return true;
    } catch(Exception e) {
        e.printStackTrace();
        return false;
    }
}

private static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}
于 2015-01-10T16:33:24.010 に答える
0

それらをアセットとしてプロジェクトに配置すると、アプリの起動時にアセットから読み取って、アプリのプライベート ストレージ領域にコピーできます。このディレクトリは、 を使用して見つけることができますContext.getFilesDir()

そこから、ファイルを Caffe に渡すことができます。

于 2015-01-10T16:34:14.953 に答える