如何在Android Studio中使用Clarifai Java API
我正在制作一个Android Studio应用程序,我需要帮助将Clarifai集成到其中。我首先按照Clarifai网站上的快速启动指南设置客户机。我的代码如下所示:如何在Android Studio中使用Clarifai Java API,java,android,api,clarifai,Java,Android,Api,Clarifai,我正在制作一个Android Studio应用程序,我需要帮助将Clarifai集成到其中。我首先按照Clarifai网站上的快速启动指南设置客户机。我的代码如下所示: public static void main(String[] arg) { new ClarifaiBuilder("c719daa395fe42f2a1385ace59592496").buildSync(); ClarifaiClient client = new ClarifaiBuilder("
public static void main(String[] arg) {
new ClarifaiBuilder("c719daa395fe42f2a1385ace59592496").buildSync();
ClarifaiClient client = new ClarifaiBuilder("c719daa395fe42f2a1385ace59592496")
.client(new OkHttpClient()) // OPTIONAL. Allows customization of OkHttp by the user
.buildSync();
client.getDefaultModels().generalModel().predict()
.withInputs(ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg"))
.executeSync();
}
据我所知,这是采取样本图像metro north和预测它是什么。我想知道的是,如何在应用程序的屏幕上写入/打印最重要的结果(或结果列表),使其可见
如果有人能帮我解决这个问题,我将不胜感激:D您可以使用以下代码打印顶部结果:
client.getDefaultModels().generalModel().predict()
.withInputs(ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg"))
.executeAsync(
outputs -> System.out.println("First output of this prediction is " + outputs.get(0))
),
code -> System.err.println("Error code: " + code + ". Error msg: " + message),
e -> { throw new ClarifaiException(e); }
);
基本上,您可以这样做:
//Get the results from the api:
new AsyncTask<Void, Void, ClarifaiResponse<List<ClarifaiOutput<Concept>>>>() {
@Override protected ClarifaiResponse<List<ClarifaiOutput<Concept>>> doInBackground(Void... params) {
// The default Clarifai model that identifies concepts in images
final ConceptModel generalModel = App.get().clarifaiClient().getDefaultModels().generalModel();
// Use this model to predict, with the image that the user just selected as the input
return generalModel.predict()
.withInputs(ClarifaiInput.forImage(ClarifaiImage.of(imageBytes)))
.executeSync();
}
@Override protected void onPostExecute(ClarifaiResponse<List<ClarifaiOutput<Concept>>> response) {
setBusy(false);
if (!response.isSuccessful()) {
showErrorSnackbar(R.string.error_while_contacting_api);
return;
}
final List<ClarifaiOutput<Concept>> predictions = response.get();
if (predictions.isEmpty()) {
showErrorSnackbar(R.string.no_results_from_api);
return;
}else{
//Do something with the results or get the first element.
}
}
//从api获取结果:
新建异步任务(){
@覆盖受保护的ClariFairResponse doInBackground(无效…参数){
//识别图像中概念的默认Clarifai模型
final ConceptModel generalModel=App.get().ClarifaicClient().getDefaultModels().generalModel();
//使用此模型预测用户刚刚选择的图像作为输入
返回generalModel.predict()
.带输入(ClarifaiInput.forImage(ClarifaiImage.of(imageBytes)))
.executeSync();
}
@重写受保护的void onPostExecute(ClariFairResponse响应){
挫折(假);
如果(!response.issusccessful()){
showErrorSnackbar(联系api时出现R.string.error);
返回;
}
最终列表预测=response.get();
if(predictions.isEmpty()){
showErrorSnackbar(R.string.no_results_来自_api);
返回;
}否则{
//对结果进行处理或获取第一个元素。
}
}
取自