/** * Classifies the images against the label groups and labels. The response includes a score for a * label if the score meets the minimum threshold of 0.5. If no score meets the threshold for an * image, no labels are returned. * * @param image the file image * @param labelSet the labels to classify against * @return the visual recognition images */ public RecognizedImage recognize(final File image, final LabelSet labelSet) { if (image == null) throw new IllegalArgumentException("image can not be null"); try { Request request = Request.Post("/v1/tag/recognize"); MultipartEntity reqEntity = new MultipartEntity(); // Set the image_file FileBody bin = new FileBody(image); reqEntity.addPart(IMG_FILE, bin); if (labelSet != null) { StringBody labels = new StringBody(GsonSingleton.getGson().toJson(labelSet), Charset.forName("UTF-8")); // Set the labels_to_check reqEntity.addPart(LABELS_TO_CHECK, labels); } request.withEntity(reqEntity); HttpResponse response = execute(request.build()); String resultJson = ResponseUtil.getString(response); VisualRecognitionImages recognizedImages = GsonSingleton.getGson().fromJson(resultJson, VisualRecognitionImages.class); return recognizedImages.getImages().get(0); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Returns a CSV profile. * * @param options the {@link ProfileOptions} * @param includeHeaders if true returns the CSV headers * @return the CSV profile */ public String getProfileAsCSV(final ProfileOptions options, final boolean includeHeaders) { final RequestBuilder requestBuilder = buildProfileRequest(options); requestBuilder.withHeader(HttpHeaders.ACCEPT, HttpMediaType.TEXT_CSV); requestBuilder.withQuery(HEADERS, includeHeaders); final Response response = execute(requestBuilder.build()); return ResponseUtil.getString(response); }
/** * Gets the labels and label groups. * * @return the labels and label groups */ public LabelSet getLabelSet() { HttpRequestBase request = Request.Get("/v1/tag/labels").build(); try { HttpResponse response = execute(request); String jsonLabelSet = ResponseUtil.getString(response); return GsonSingleton.getGson().fromJson(jsonLabelSet, LabelSet.class); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Identifies concepts in a piece of text. * * @param parameters The parameters to be used in the service call, account_id, graph and text are * required. * <ul> * <li>String account_id - The account identifier.<br> * <li>String graph - The graph name.<br> * <li>String text - The text to annotate.<br> * </ul> * * @return {@link Annotations} */ public Annotations annotateText(Map<String, Object> parameters) { Validate.notNull(parameters.get(ACCOUNT_ID), "account_id can't be null"); Validate.notNull(parameters.get(GRAPH), "graph can't be null"); Validate.notNull(parameters.get(TEXT), "text can't be null"); String graphId = createGraphIdPath((String) parameters.get(ACCOUNT_ID), (String) parameters.get(GRAPH)); HttpRequestBase request = Request.Post(graphId + ANNOTATE_TEXT_PATH) .withContent((String) parameters.get(TEXT), MediaType.TEXT_PLAIN) .withHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON) .build(); try { HttpResponse response = execute(request); Annotations annotations = GsonSingleton.getGson().fromJson(ResponseUtil.getString(response), Annotations.class); return annotations; } catch (IOException e) { throw new RuntimeException(e); } }