我需要将图像文件发送到 Clarifai 服务器进行图像分类,但我一直收到 "skipped frames, application doing too much work"
I need to send an image file to Clarifai server for image classification, but I keep getting "skipped frames, application doing too much work"
我正在使用新线程发送文件,并使用一些代码片段将位图转换为文件。从位图到文件的转换真的很慢,而且似乎向 clarifai 发送信息没有做任何事情...
//Convert bitmap to byte array
Bitmap bitmap = mResultsBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bitmapdata);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
final File pFIle2 = f;
//TODO: clarifai stuff
//TODO: clarifai stuff
Log.e("this:"," this is running 0");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.e("this:", " this is running 1");
client = new ClarifaiBuilder("mykeyhere1234}").buildSync();
Log.e("this:", " this is running 2");
Thread th = new Thread(new Runnable() {
@Override
public void run() {
Log.e("this:", " this is running 3");
Log.e("this", client.getDefaultModels().generalModel().predict()
.withInputs(
ClarifaiInput.forImage(ClarifaiImage.of(pFIle2))
)
.executeSync().rawBody());
Log.e("this:", " this is running 4");
}
});
}
});
这段代码片段在 onActivityResult 方法中。 None 的日志消息正在打印,但“0”除外
首先要把位图转换也放到Thread里面,这样运行是异步的。这将防止出现卡顿和跳帧消息。
Clarifai 服务似乎无法正常工作的原因是您没有 start()
Thread。此外,没有理由将一个线程放在另一个线程中。
总而言之,这里是固定代码:
final Bitmap bitmap = mResultsBitmap;
final File pFile2 = f;
ClarifaiClient client = new ClarifaiBuilder("mykeyhere1234}").buildSync();
DefaultModels defaultModels = client.getDefaultModels();
Log.e("this:"," this is running 0");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pFile2);
fos.write(bitmapdata);
} catch (FileNotFoundException e | IOException e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//TODO: clarifai stuff
//TODO: clarifai stuff
Log.e("this:", " this is running 1");
client =
Log.e("this:", " this is running 2");
Log.e("this:", " this is running 3");
Log.e("this", models.generalModel().predict()
.withInputs(
ClarifaiInput.forImage(ClarifaiImage.of(pFile2))
)
.executeSync().rawBody());
Log.e("this:", " this is running 4");
}
});
thread.start();
当 API 客户端在多次重复尝试后无法从服务器获取完整的默认模型(即通用模型和其他模型)时,将抛出日志消息中的错误。请确保:
- 您的 API 密钥有效并且有权进行预测,
- 您的应用可以连接到 Clarifai 服务器。
另外两点:
- 此代码 运行 是否在某处循环?新的
ClarifaiBuilder
/ClarifaiClient
实例应该只构造一次(可能在应用程序开始时)。
- 您或许可以简化对本地文件的预测。请参阅 this example。
将位图相关操作保留在后台线程始终是最佳做法,试试这个你不会得到与跳过帧相关的日志,在主线程上做了太多工作.
使用异步任务执行位图操作。
我正在使用新线程发送文件,并使用一些代码片段将位图转换为文件。从位图到文件的转换真的很慢,而且似乎向 clarifai 发送信息没有做任何事情...
//Convert bitmap to byte array
Bitmap bitmap = mResultsBitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bitmapdata);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
final File pFIle2 = f;
//TODO: clarifai stuff
//TODO: clarifai stuff
Log.e("this:"," this is running 0");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Log.e("this:", " this is running 1");
client = new ClarifaiBuilder("mykeyhere1234}").buildSync();
Log.e("this:", " this is running 2");
Thread th = new Thread(new Runnable() {
@Override
public void run() {
Log.e("this:", " this is running 3");
Log.e("this", client.getDefaultModels().generalModel().predict()
.withInputs(
ClarifaiInput.forImage(ClarifaiImage.of(pFIle2))
)
.executeSync().rawBody());
Log.e("this:", " this is running 4");
}
});
}
});
这段代码片段在 onActivityResult 方法中。 None 的日志消息正在打印,但“0”除外
首先要把位图转换也放到Thread里面,这样运行是异步的。这将防止出现卡顿和跳帧消息。
Clarifai 服务似乎无法正常工作的原因是您没有 start()
Thread。此外,没有理由将一个线程放在另一个线程中。
总而言之,这里是固定代码:
final Bitmap bitmap = mResultsBitmap;
final File pFile2 = f;
ClarifaiClient client = new ClarifaiBuilder("mykeyhere1234}").buildSync();
DefaultModels defaultModels = client.getDefaultModels();
Log.e("this:"," this is running 0");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pFile2);
fos.write(bitmapdata);
} catch (FileNotFoundException e | IOException e) {
e.printStackTrace();
} finally {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//TODO: clarifai stuff
//TODO: clarifai stuff
Log.e("this:", " this is running 1");
client =
Log.e("this:", " this is running 2");
Log.e("this:", " this is running 3");
Log.e("this", models.generalModel().predict()
.withInputs(
ClarifaiInput.forImage(ClarifaiImage.of(pFile2))
)
.executeSync().rawBody());
Log.e("this:", " this is running 4");
}
});
thread.start();
当 API 客户端在多次重复尝试后无法从服务器获取完整的默认模型(即通用模型和其他模型)时,将抛出日志消息中的错误。请确保:
- 您的 API 密钥有效并且有权进行预测,
- 您的应用可以连接到 Clarifai 服务器。
另外两点:
- 此代码 运行 是否在某处循环?新的
ClarifaiBuilder
/ClarifaiClient
实例应该只构造一次(可能在应用程序开始时)。 - 您或许可以简化对本地文件的预测。请参阅 this example。
将位图相关操作保留在后台线程始终是最佳做法,试试这个你不会得到与跳过帧相关的日志,在主线程上做了太多工作. 使用异步任务执行位图操作。