Clarifai This API call was not sucessful - java

I was trying some concepts with Clarifai locally using java, from some images I have on my computer. The idea was to get 30 concepts for each of the images which would then be saved as tags in the application I am developing. Initially the code was working and I printed the result for each one of the images in the folder I'm reading from, but a little more than an hour ago I left the following error:
Below I leave the java code I was using to get the concepts of my images:
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);
What am I doing roung?

This (the illegal reflective access Warning) can happen in older versions of protobuf. What version are you using? Are you able to update?
The API should be using 3.12.0, but I've seen instances where an older installed version was used unexpectedly.
Just saw that the error message you're getting is actually NoSuchElementException so the Clarifai network error is probably not related to an outage or network issues like I originally suspected.
Would be helpful to know what line 134 is in your getConcepts function to see if it is passing something odd to the Clarifai API which might be choking on it.

Related

System.out.format not working in java for loop

Below is the concerned code. Basically what the code is supposed to do is output the URL, name and version of each GitHub release defined by GetUpdateInfo.getInfo().
GetUpdateInfo.getInfo (NOTE Strings login, oauth and password omitted for security reasons.):
import java.util.List;
import org.kohsuke.github.*;
import org.apache.commons.lang3.ArrayUtils;
public class GetUpdateInfo {
public static getInfo() {
String version = "";
String url = "";
String[] urls = {};
String[] names = {};
String[] versions = {};
String[] releases = {};
GitHub github = GitHubBuilder.fromEnvironment(login, password, oauth).build();
//Get the repo name from the organization
GHOrganization gho = github.getOrganization("NuovoLauncher-Mods");
PagedIterable<GHRepository> repos = gho.listRepositories();
List<GHRepository> repos_list = repos.asList();
for(int i=0; i < repos_list.size(); i++) {
GHRepository repo_test = repos_list.get(i);
GHRelease latest = repo_test.getLatestRelease();
ArrayUtils.add(releases, latest.toString());
ArrayUtils.add(names, latest.getName());
ui.setName(names);
ui.setRelease(releases);
List<GHAsset> assets = latest.getAssets();
for( int x = 0; x < assets.size(); x++ ) {
GHAsset asset = assets.get(x);
url = asset.getBrowserDownloadUrl();
version = url.split("/")[7];
System.out.format("URL: %s, Name: %s, Latest Release: %s. Version %s\n", url, latest.getName(), latest, version);
ArrayUtils.add(urls, url);
ArrayUtils.add(versions, version);
ui.setURL(urls);
ui.setVersion(versions);
}
}
return ui;
}
public static void main(String[] args) throws Exception {
GetUpdateInfo.getInfo();
}
}
DownloadUpdate.runner:
public static void runner() throws Exception {
String system_type = System.getProperty("os.name");
File fpath = new File("");
UpdateInfo ui = GetUpdateInfo.getInfo();
for(int i = 0; i < ui.getName().length; i++) {
System.out.format("URL: %s, Name %s, Version, %s", ui.getURL()[i], ui.getName()[i], ui.getVersion()[i]);
System.out.format("Downloading %s-%s", ui.getName()[i], ui.getVersion()[i]);
System.out.print("\n");
if(system_type.equals("Linux")) {
fpath = new File(System.getProperty("user.home") + "/.minecraft/mods/" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
} else if(system_type.equals("Windows")) {
fpath = new File(System.getProperty("user.home") + "/AppData/Roaming/.minecraft/mods" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
} else {
fpath = new File(System.getProperty("user.home") + "/.minecraft/mods/" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
}
String url = ui.getURL()[i];
FileUtils.copyURLToFile(new URL(url), fpath);
}
}
public static void main(String[] args) throws Exception {
System.out.println("DEBUG START");
DownloadUpdate.runner();
}
}
Looking at the code, I cannot see a reason why the code is not outputting like expected; I am getting zero output on console, simply the line stating that the code is being executed. No exceptions either.
EDIT: variable ui is not being returned properly. For example, ui.getName[0] throws an ArrayIndexOutOfBoundsException, due to the length being zero. Seeing this, I now understand why the for loop isn't behaving as expected. Is this a scope issue? What am I missing here?
An obvious problem of your code is the use of ArrayUtils.add: you have to reassign its result to the input array, as you cannot modify arrays like lists in Java.
Use it like this:
releases = ArrayUtils.add(releases, latest.toString());
names = ArrayUtils.add(names, latest.getName());
and later in the for-loop:
urls = ArrayUtils.add(urls, url);
versions = ArrayUtils.add(versions, version);
Also you don't need to set the elements in each loop cycle to the result:
ui.setURL(urls);
ui.setVersion(versions);
Those would be sufficient once the for-loop has completed.
An alternative would be to first use List<String> instead of the arrays. If you have control over the UpdateInfo class, change it there to be lists too, otherwise create an array from the lists before you set it in UpdateInfo.
As a general advice I would recommend that you get rid of your static methods. Create instances and use your credentials (login, password, oauth) as member fields OR pass in even the whole GitHub instance. This way you would have an easier time writing proper tests.

I have problems when submitting data from an RSS in my App

First of all I apologize for the translation, I speak Spanish.
My problem is the following:
I use the RSS of my own website to show data in my App, the bad thing is that they do not show up immediately, I need you to do it as it is necessary.
I think the problem may be in the XMLParser class:
public class XMLParser {
private URL url;
private Context contextor;
public XMLParser(String url) {
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
#SuppressLint("SimpleDateFormat")
public void parse() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.url.openConnection().getInputStream());
Element root = dom.getDocumentElement();
NodeList itema = root.getElementsByTagName("item");
for (int i = 0; i < 1; i++) {
Node item = itema.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("content:encoded")) {
Log.i("XMLParser", "GUARDO " + property.getFirstChild().getNodeValue());
int star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg0\">"));
int fiinal = (property.getFirstChild().getNodeValue().indexOf("</table>"));
String cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
MyBus.setDomincal(cadena);
star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg1\">"));
String parteCadena = (property.getFirstChild().getNodeValue().substring(star));
fiinal = parteCadena.indexOf("</table>");
cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
Log.i("XMLParser", "GUARDO " + url);
Log.i("XMLParser", "GUARDO " + cadena);
//Log.i("XMLParser", "GUARDO " + MyBus.getIntermedio());
MyBus.setIntermedio(cadena);
star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg2\">"));
parteCadena = (property.getFirstChild().getNodeValue().substring(star));
fiinal = parteCadena.indexOf("</table>");
cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
MyBus.setZodiacal(cadena);
star = (property.getFirstChild().getNodeValue().indexOf("<table class=\"tg3\">"));
parteCadena = (property.getFirstChild().getNodeValue().substring(star));
fiinal = parteCadena.indexOf("</table>");
cadena = (property.getFirstChild().getNodeValue().substring(star, star+fiinal+8));
MyBus.setExtraordinario(cadena);
//MyBus.setSeguro(1);
}
}
}
} catch (Exception e) {
//MyBus.setSeguro(1);
//Esto solo es para lanzar un error y para que la app haga un cierre forsozo
//throw new RuntimeException(e);
}
}}
I do not know what I'm doing wrong, because when I check the link in the browser it is updated. Please, if someone can help me, I would really appreciate it.
An example would be: on the website I create a post or simply edit one, I change certain data that I have or there, let's say it said 'red' but now I edited it and put it 'blue'. In the link that I see of the RSS in the browser appears updated, but if I go to my app keeps saying red or does not appear the new post that I created until several hours later or the next day, and that is wrong because the information given my application must be updated every so often and it does not do me any good to do it many hours later.
At first I thought it could be the webview cache but the problem comes before it was shown in that webview.
Thanks in advance.

Inserting multiple videos to a playlist using multithreading results in videos missing

When I insert the videos serially, all the videos get inserted to the playlist but it takes a long time. When I use multithreading more than half of the videos are missing in the end. How can I insert multiple videos quickly without losing any videos?
// Insert videos. 5 videos per asyncTask
List<List<YTVideo>> chunks = Lists.partition(videos, 5);
for (int i = 0; i < chunks.size(); i++) {
videoAndPlaylistContainer container = new videoAndPlaylistContainer();
container.playlistId = playlistId;
List<YTVideo> chunk = chunks.get(i);
container.videos = chunk;
InsertPlayListItemTask insertPlaylistItemsTask = new InsertPlayListItemTask();
runningTasks.add(insertPlaylistItemsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,container));
}
.
private class InsertPlayListItemTask extends AsyncTask<videoAndPlaylistContainer, Void, Void>{
#Override
protected Void doInBackground(videoAndPlaylistContainer... params) {
videoAndPlaylistContainer container = params[0];
List<YTVideo> videosChunk = container.videos;
String playlistId = container.playlistId;
for (int i = 0; i < videosChunk.size(); i++) {
YTVideo video = videosChunk.get(i);
String videoId = video.getId();
long pos = video.getPosition();
try {
ResourceId resourceId = new ResourceId();
resourceId.setKind("youtube#video");
resourceId.setVideoId(videoId);
PlaylistItemSnippet playlistItemSnippet = new PlaylistItemSnippet();
// playlistItemSnippet.setTitle("First video in the test playlist");
playlistItemSnippet.setPlaylistId(playlistId);
playlistItemSnippet.setResourceId(resourceId);
playlistItemSnippet.setPosition(pos);
PlaylistItem playlistItem = new PlaylistItem();
playlistItem.setSnippet(playlistItemSnippet);
YouTube.PlaylistItems.Insert playlistItemsInsertCommand =
youtube.playlistItems().insert("snippet", playlistItem);
playlistItemsInsertCommand.execute();
System.out.println("Inserted video: " + video);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
edit: videos.size() to chunks.size(). Removed insertedCount++;
This line doesn't look right...
for (int i = 0; i < videos.size(); i++) {
The i variable will grow based on the size of the videos collection, but you only ever use the i variable to access an element within the chunks collection here...
List<YTVideo> chunk = chunks.get(i);
I can't imagine why you don't (eventually) get an ArrayIndexOutOfBounds exception, except maybe you're testing your code (so far) with fewer than 5 videos.

FreeMarker get variable value by concatenating another variable value

public class Main {
public static void main(String[] args) throws IOException, TemplateException{
Configuration freemarkerConfig = new Configuration();
freemarkerConfig.setClassForTemplateLoading(Main.class, "");
Template template = freemarkerConfig.getTemplate("template.ftl");
Map<String, String> data = new HashMap<String, String>();
for(int i=1;i<=10;i++){
data.put("map_"+i, "value"+i);
}
Writer out = new StringWriter();
template.process(data, out);
System.out.println(out.toString());
}
}
Here is my FTL code to access the variable:
<#assign containerIndex=1>
${map_containerIndex}
This gives error
I want to evaluate ${map_1}
You can read variables with such runtime generated names like .vars['map_' + i]. It's the same trick than tom has used in his answer, but applied for reading a top-level variable.
Instead of trying to create a variable (which I don't think is possible that way) I'd suggest providing an array to the template.
Like so
String[] stringArray = new String[11];
for (int i = 1; i<= 10; i++) {
stringArray[i] = "value"+i;
}
data.put("map", stringArray);
Access in Freemarker should look like
<#assign containerIndex=1>
${map[containerIndex]}
or something like that, can't try it out atm
Also note, that by starting the for-loop with 1 (as in your example) the first array-slot won't be used.
I'd suggest
String[] stringArray = new String[10];
for (int i = 0; i < 10; i++) {
stringArray[i] = "value"+i;
}

Jacob Export Outlook Contact Pictures (Java, Jacob)

Good Morning,
I’am using Jacob 1.17 o read all my Outlook Contact Pictures and save them to an File. The Procedure works pretty fine for the first 199 Contatcs. After that the Dispatch.call fails and terminates with the following Exception:
Exception in thread "main" com.jacob.com.ComFailException: Invoke of: SaveAsFile
Source: Microsoft Outlook
Description: Cannot save the attachment. Cannot create file: ContactPicture.jpg.
Right-click the folder you want to create the file in, and then click Properties on
the shortcut menu to check your permissions for the folder.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.callN(Dispatch.java:453)
at com.jacob.com.Dispatch.call(Dispatch.java:541)
at outlookStuff.ManageContactsOutlook.tmpTest(ManageContactsOutlook.java:217)
at mainPackage.Main.main(Main.java:32)
I’m really not sure way. I tested a different set of Contacts – same Error. Set all Objects to null to make shore that the Garbage Collector is involved but it doesn’t help.
The piece of Code which makes the trouble:
public void tmpTest(int intOutlookFolder, String strWorkingDir) {
Dispatch dipNamespace = this.axc.getProperty("Session").toDispatch();
Dispatch dipContactsFolder = Dispatch.call(dipNamespace, "GetDefaultFolder", (Object) new Integer(intOutlookFolder)).toDispatch();
Dispatch dipContactItems = Dispatch.get(dipContactsFolder, "items").toDispatch();
#SuppressWarnings("deprecation")
int count = Dispatch.call(dipContactItems, "Count").toInt();
for (int i=1; i<=count; i++) {
Dispatch dipContact;
dipContact = Dispatch.call(dipContactItems, "Item", new Integer(i)).toDispatch();
String strEntryID = Dispatch.get(dipContact, "EntryID").toString().trim();
//For Testing
Status.printStatusToConsole("Outlook Contact "+strEntryID+" loaded");
byte[] byteContactPicture = null;
String strPathToTmpPicture = null;
Dispatch dipAttachments = Dispatch.get(dipContact, "Attachments").toDispatch();
#SuppressWarnings("deprecation")
int countAttachements = Dispatch.call((Dispatch) dipAttachments, "Count").toInt();
for (int j=1; j<=countAttachements; j++) {
Dispatch currentAttachement;
currentAttachement = Dispatch.call(dipAttachments, "Item", new Integer(j)).toDispatch();
if (Dispatch.get(currentAttachement, "FileName").toString().equals("ContactPicture.jpg")) {
strPathToTmpPicture = strWorkingDir+strEntryID+".jpg";
//The Crashing Part
Dispatch.call(currentAttachement, "SaveAsFile", strPathToTmpPicture);
File tmpFile = new File(strPathToTmpPicture);
if (tmpFile.exists()) {
try {
byteContactPicture = org.apache.commons.io.FileUtils.readFileToByteArray(tmpFile);
} catch (IOException e) {
e.printStackTrace();
}
}
currentAttachement = null;
tmpFile = null;
}
currentAttachement = null;
}
dipAttachments = null;
}
dipContactItems = null;
dipContactsFolder = null;
dipNamespace = null;
}
May someone has an idea?
Thanks
Aviation

Categories

Resources