我在用java在本地用clarifai尝试一些概念,从我电脑上的一些图片中。我们的想法是为每个图像获得30个概念,然后在我正在开发的应用程序中保存为标记。最初代码正常工作,我打印了我正在读取的文件夹中每个图像的结果,但一个多小时前,我留下了以下错误:
下面是我用来获取图像概念的java代码:
protected static String getConcepts(String path, String API_KEY) {
/*Vou obter 30 conceitos para uma determinada imagem*/
String result = "";
ClarifaiClient client = new ClarifaiBuilder(API_KEY).client(new OkHttpClient()).buildSync();
final List<ClarifaiOutput<Concept>> predictionResults = client.getDefaultModels().generalModel().predict().
withInputs(ClarifaiInput.forImage(new File(path))).withMaxConcepts(5).executeSync().get();
Iterator<ClarifaiOutput<Concept>> resultTags = predictionResults.iterator();
TreeMap<String, Float> tags = new TreeMap<>();
int numberOfTags;
if (resultTags.hasNext()) {
ClarifaiOutput<Concept> next = resultTags.next();
numberOfTags = next.data().size();
for (int j = 0; j < numberOfTags; j++) {
Concept concept = next.data().get(j);
String name = concept.name();
tags.put(concept.name(), concept.value());
}
}
for (Map.Entry<String, Float> entry : tags.entrySet()) {
result += entry.getKey() + "," + entry.getValue() + ",";
}
System.out.println( result);
return result;
}
public static void main(String[] args) throws IOException {
/*ClarifaiClient client = new ClarifaiBuilder("b5bc3cb6b7ba4a8cbd6d950c811c18b3").buildSync();
final List<ClarifaiOutput<Concept>> response =
// You can also do client.getModelByID("id") to get your custom model
client.getDefaultModels().generalModel()
.predict()
.withInputs(ClarifaiInput.forImage("https://samples.clarifai.com/metro-north.jpg"))
.executeSync()
.get();
System.out.println(response);*/
//String src = "/home/claudia/Desktop/EmotionROI/images/anger/selected_images/";
//String img = "12.jpg";
//String dest = src+"teste"+img;
//Lista de ficheiros do diretório - obtenção dos ficheiros
String pathDir = "/home/claudia/Desktop/EmotionROI/images/anger/selected_images/";
File imageFile = new File(pathDir);
File[] listFiles = imageFile.listFiles();
//System.out.println("Os meus ficheiros são: ");
String resultF = "";
List<String> listTagImg = new ArrayList<>();
//ciclo que vai permitir aceder ao nome dos ficheiros
for (int i = 0; i < listFiles.length; i++) {
String nameFiles = listFiles[i].getName();
//System.out.println(nameFiles);
//criação de um pah novo com o nome de cada um dos ficheiros
String pahtFiles = pathDir+nameFiles;
listTagImg.add(pahtFiles);
}
List<String> listTags = new ArrayList<>();
String line="";
for (int j = 0; j < listFiles.length; j++) {
//System.out.println("lista"+listTagImg.get(j));
line += getConcepts(listTagImg.get(j), "b5bc3cb6b7ba4a8cbd6d950c811c18b3");
}
//getConcepts(listTagImg.get(), "241b3315671f4baeaec399186c435022");
System.out.println(line);
我在做什么?
1条答案
按热度按时间nfg76nw01#
这(非法反射访问警告)可能发生在protobuf的旧版本中。你用的是什么版本?你能更新吗?
api应该使用3.12.0,但我见过一些示例意外地使用了较旧的安装版本。
刚才看到您收到的错误消息实际上是nosuchelementexception,所以clarifai网络错误可能与我最初怀疑的中断或网络问题无关。
如果你能知道你房间里的第134行是什么,会很有帮助的
getConcepts
函数来查看它是否向clarifai api传递了一些奇怪的东西,这些东西可能会阻塞它。