manager.computerVisionManager.java Source code

Java tutorial

Introduction

Here is the source code for manager.computerVisionManager.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import java.io.IOException;
import org.json.simple.*;
import org.json.simple.parser.*;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.StringReader;
import java.net.URISyntaxException;
import org.apache.http.ParseException;

public class computerVisionManager {

    static HttpClient httpclient = HttpClients.createDefault();
    static JSONObject json = null;

    private static void getJson(String path) {
        System.out.println("Get Description from https://westus.api.cognitive.microsoft.com/vision/v1.0/describe");
        try {
            URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/vision/v1.0/describe");
            builder.setParameter("maxCandidates", "1");
            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Content-Type", "application/json");
            request.setHeader("Ocp-Apim-Subscription-Key", "d7f6ef12e41c4f8c8e72d12a890fa703");
            // Request body
            StringEntity reqEntity = new StringEntity("{\"url\":\"" + path + "\"}");
            System.out.println("Request String: " + reqEntity.toString());
            request.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String respuesta = EntityUtils.toString(entity);
                JSONParser lector = new JSONParser();
                StringReader SR = new StringReader(respuesta);
                try {
                    JSONObject temp = (JSONObject) lector.parse(SR);
                    json = (JSONObject) temp.get("description");
                } catch (org.json.simple.parser.ParseException e) {
                    System.err.println(e.getMessage());
                }
            }
        } catch (IOException | URISyntaxException | ParseException e) {
            System.err.println(e.getMessage());
        }
    }

    public static String getImageDescription(String path) {
        if (json == null) {
            getJson(path);
        }
        try {
            System.out.println(json.toString());
            JSONArray captions = (JSONArray) json.get("captions");
            JSONObject text = (JSONObject) captions.get(0);
            return text.get("text").toString();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return "error";
    }

    public static JSONArray getImageTags(String path) {
        if (json == null) {
            getJson(path);
        }
        JSONArray tags = (JSONArray) json.get("tags");
        return tags;
    }
}