Skip to content

Commit

Permalink
qnn-sample: workaround to fix a minior bug in qnn-sample code of Qual…
Browse files Browse the repository at this point in the history
…comm QNN(Qualcomm Neural Network, aka Qualcomm AI Engine Direct) SDK

add the qnn binary files to facilitate other developers to reproduce the qnn sample on Qualcomm SoC based android phone(Xiaomi 14 is preferred)
  • Loading branch information
zhouwg committed Mar 31, 2024
1 parent eb02b54 commit 2c740c7
Show file tree
Hide file tree
Showing 21 changed files with 1,205 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,124 +15,171 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cdeos.media.player;

import android.annotation.SuppressLint;
import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CDEAssetLoader {
private static final String TAG = CDEAssetLoader.class.getName();

public static String getDataPath(Context context) {
return new StringBuilder("/data/data/").append(context.getPackageName()).append("/").toString();
}

public static void copyAssetFile(Context context, String srcFilePath, String destFilePath) {
CDELog.j(TAG, "src path: " + srcFilePath);
CDELog.j(TAG, "dst path: " + destFilePath);
InputStream inStream = null;
FileOutputStream outStream = null;
try {
File destFile = new File(destFilePath);

if (destFile.exists()) {
// 03-30-2024 17:09:35.152 24446 24797 D KANTV : [LogUtils.cpp, logStdoutCallback, 48]:
// 0.3ms [ ERROR ] Unable to load backend. pal::dynamicloading::dlError():
// dlopen failed: file offset for the library "/data/data/com.cdeos.kantv/libQnnCpu.so" >= file size: 0 >= 0


//03-30-2024 17:15:23.949 25507 25796 D KANTV : [LogUtils.cpp, logStdoutCallback, 48]:
// 6.5ms [ ERROR ] Unable to load model. pal::dynamicloading::dlError():
// dlopen failed: library "/sdcard/kantv/libInception_v3.so" needed or dlopened by
// "/data/app/~~70peMvcNIhRmzhm-PhmfRg==/com.cdeos.kantv-bUwy7gbMeCP0JFLe1J058g==/base.apk!/lib/arm64-v8a/libggml-jni.so"
// is not accessible for the namespace "clns-4"

CDELog.j(TAG, "dst file already exist");
if (destFile.getAbsolutePath().contains("libQnnCpu")) {
CDELog.j(TAG, "remove libQnnCpu.so");
//destFile.delete();
}
if (destFile.getAbsolutePath().contains("libInception_v3")) {
CDELog.j(TAG, "remove libInception_v3.so");
//destFile.delete();
}
return;
}

if (!destFile.exists()) {
destFile.createNewFile();
}

inStream = context.getAssets().open(srcFilePath);
outStream = new FileOutputStream(destFilePath);

int bytesRead = 0;
byte[] buf = new byte[2048];
while ((bytesRead = inStream.read(buf)) != -1) {
outStream.write(buf, 0, bytesRead);
}
} catch (Exception e) {
CDELog.j(TAG, "error: " + e.toString());
e.printStackTrace();
} finally {
close(inStream);
close(outStream);
}
}

public static void close(InputStream is) {
if (is == null) return;
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void close(OutputStream os) {
if (os == null) return;
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
CDELog.j(TAG, "error: " + e.toString());
}
}

public static String readTextFromFile(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
CDELog.j(TAG, "error: " + e.toString());
}
}
}
return sbf.toString();
}

public static native int kantv_anti_remove_rename_this_file();
}
package cdeos.media.player;

import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import android.annotation.SuppressLint;
import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


public class CDEAssetLoader {
private static final String TAG = CDEAssetLoader.class.getName();

public static String getDataPath(Context context) {
return new StringBuilder("/data/data/").append(context.getPackageName()).append("/").toString();
}

public static void copyAssetFile(Context context, String srcFilePath, String destFilePath) {
CDELog.j(TAG, "src path: " + srcFilePath);
CDELog.j(TAG, "dst path: " + destFilePath);
InputStream inStream = null;
FileOutputStream outStream = null;
try {
File destFile = new File(destFilePath);

if (destFile.exists()) {
// 03-30-2024 17:09:35.152 24446 24797 D KANTV : [LogUtils.cpp, logStdoutCallback, 48]:
// 0.3ms [ ERROR ] Unable to load backend. pal::dynamicloading::dlError():
// dlopen failed: file offset for the library "/data/data/com.cdeos.kantv/libQnnCpu.so" >= file size: 0 >= 0


//03-30-2024 17:15:23.949 25507 25796 D KANTV : [LogUtils.cpp, logStdoutCallback, 48]:
// 6.5ms [ ERROR ] Unable to load model. pal::dynamicloading::dlError():
// dlopen failed: library "/sdcard/kantv/libInception_v3.so" needed or dlopened by
// "/data/app/~~70peMvcNIhRmzhm-PhmfRg==/com.cdeos.kantv-bUwy7gbMeCP0JFLe1J058g==/base.apk!/lib/arm64-v8a/libggml-jni.so"
// is not accessible for the namespace "clns-4"

CDELog.j(TAG, "dst file already exist");
if (destFile.getAbsolutePath().contains("libQnnCpu")) {
CDELog.j(TAG, "remove libQnnCpu.so");
//destFile.delete();
}
if (destFile.getAbsolutePath().contains("libInception_v3")) {
CDELog.j(TAG, "remove libInception_v3.so");
//destFile.delete();
}
return;
}

if (!destFile.exists()) {
destFile.createNewFile();
}

inStream = context.getAssets().open(srcFilePath);
outStream = new FileOutputStream(destFilePath);

int bytesRead = 0;
byte[] buf = new byte[2048];
while ((bytesRead = inStream.read(buf)) != -1) {
outStream.write(buf, 0, bytesRead);
}
} catch (Exception e) {
CDELog.j(TAG, "error: " + e.toString());
e.printStackTrace();
} finally {
close(inStream);
close(outStream);
}
}

public static void copyAssetDir(Context context, String srcDirPath, String destDirPath) {
CDELog.j(TAG, "src path: " + srcDirPath);
CDELog.j(TAG, "dst path: " + destDirPath);
try {
File destFile = new File(destDirPath);
if (destFile.exists()) {
CDELog.j(TAG, "dir: " + destDirPath + " already exist");
return;
} else {
CDELog.j(TAG, "dir: " + destDirPath + " not exist");
}

String fileNames[] = context.getAssets().list(srcDirPath);
if (fileNames.length > 0) {
File file = new File(destDirPath);
file.mkdirs();
for (String fileName : fileNames) {
copyAssetDir(context, srcDirPath + File.separator + fileName, destDirPath + File.separator + fileName);
}
} else {
InputStream is = context.getAssets().open(srcDirPath);
FileOutputStream fos = new FileOutputStream(new File(destDirPath));
byte[] buffer = new byte[2048];
int byteCount = 0;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close();
}
//Path copyFrom = Paths.get(srcDirPath);
//Path copyTo = Paths.get(destDirPath);
//Files.copy(copyFrom, copyTo, REPLACE_EXISTING, COPY_ATTRIBUTES);
} catch (Exception e) {
CDELog.j(TAG, "error: " + e.toString());
e.printStackTrace();
}
}

public static void close(InputStream is) {
if (is == null) return;
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void close(OutputStream os) {
if (os == null) return;
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
CDELog.j(TAG, "error: " + e.toString());
}
}

public static String readTextFromFile(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
sbf.append(tempStr);
}
reader.close();
return sbf.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
CDELog.j(TAG, "error: " + e.toString());
}
}
}
return sbf.toString();
}

public static native int kantv_anti_remove_rename_this_file();
}
Binary file added cdeosplayer/kantv/src/main/assets/data/chairs.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
4 changes: 4 additions & 0 deletions cdeosplayer/kantv/src/main/assets/data/cropped/raw_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/opt/qcom/aistack/qnn/2.20.0.240223/examples/Models/InceptionV3/data/cropped/notice_sign.raw
/opt/qcom/aistack/qnn/2.20.0.240223/examples/Models/InceptionV3/data/cropped/plastic_cup.raw
/opt/qcom/aistack/qnn/2.20.0.240223/examples/Models/InceptionV3/data/cropped/trash_bin.raw
/opt/qcom/aistack/qnn/2.20.0.240223/examples/Models/InceptionV3/data/cropped/chairs.raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading

0 comments on commit 2c740c7

Please sign in to comment.