我在本地使用 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);
我在做什么?