Search in sources :

Example 1 with Image

use of com.ibm.dtfj.image.j9.Image in project Douya by DreaminginCodeZH.

the class BroadcastLayout method bindBroadcast.

public void bindBroadcast(final Broadcast broadcast) {
    final Context context = getContext();
    if (broadcast.isInterest) {
        mAvatarImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.recommendation_avatar_icon_grey600_40dp));
        mAvatarImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                UriHandler.open(DoubanUtils.getInterestTypeUrl(broadcast.interestType), context);
            }
        });
    } else {
        ImageUtils.loadAvatar(mAvatarImage, broadcast.author.avatar);
        mAvatarImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                context.startActivity(ProfileActivity.makeIntent(broadcast.author, context));
            }
        });
    }
    mNameText.setText(broadcast.getAuthorName());
    mTimeActionText.setDoubanTimeAndAction(broadcast.createdAt, broadcast.action);
    boolean isRebind = mBoundBroadcastId != null && mBoundBroadcastId == broadcast.id;
    // HACK: Attachment and text should not change on rebind.
    if (!isRebind) {
        Attachment attachment = broadcast.attachment;
        if (attachment != null) {
            mAttachmentLayout.setVisibility(VISIBLE);
            mAttachmentTitleText.setText(attachment.title);
            mAttachmentDescriptionText.setText(attachment.description);
            if (!TextUtils.isEmpty(attachment.image)) {
                mAttachmentImage.setVisibility(VISIBLE);
                ImageUtils.loadImage(mAttachmentImage, attachment.image);
            } else {
                mAttachmentImage.setVisibility(GONE);
            }
            final String attachmentUrl = attachment.href;
            if (!TextUtils.isEmpty(attachmentUrl)) {
                mAttachmentLayout.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        UriHandler.open(attachmentUrl, context);
                    }
                });
            } else {
                mAttachmentLayout.setOnClickListener(null);
            }
        } else {
            mAttachmentLayout.setVisibility(GONE);
        }
        final ArrayList<Image> images = broadcast.images.size() > 0 ? broadcast.images : Photo.toImageList(broadcast.photos);
        int numImages = images.size();
        if (numImages == 1) {
            final Image image = images.get(0);
            mSingleImageLayout.setVisibility(VISIBLE);
            mSingleImageLayout.loadImage(image);
            mSingleImageLayout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    context.startActivity(GalleryActivity.makeIntent(image, context));
                }
            });
        } else {
            mSingleImageLayout.setVisibility(GONE);
        }
        if (numImages > 1) {
            mImageListLayout.setVisibility(VISIBLE);
            mImageListDescriptionText.setText(context.getString(R.string.broadcast_image_list_count_format, numImages));
            mImageListAdapter.replace(images);
            mImageListAdapter.setOnImageClickListener(new HorizontalImageAdapter.OnImageClickListener() {

                @Override
                public void onImageClick(int position) {
                    context.startActivity(GalleryActivity.makeImageListIntent(images, position, context));
                }
            });
        } else {
            mImageListLayout.setVisibility(GONE);
        }
        boolean textSpaceVisible = (attachment != null || numImages > 0) && !TextUtils.isEmpty(broadcast.text);
        ViewUtils.setVisibleOrGone(mTextSpace, textSpaceVisible);
        mTextText.setText(broadcast.getTextWithEntities(context));
    }
    mLikeButton.setText(broadcast.getLikeCountString());
    LikeBroadcastManager likeBroadcastManager = LikeBroadcastManager.getInstance();
    if (likeBroadcastManager.isWriting(broadcast.id)) {
        mLikeButton.setActivated(likeBroadcastManager.isWritingLike(broadcast.id));
        mLikeButton.setEnabled(false);
    } else {
        mLikeButton.setActivated(broadcast.isLiked);
        mLikeButton.setEnabled(true);
    }
    mLikeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onLikeClicked();
            }
        }
    });
    RebroadcastBroadcastManager rebroadcastBroadcastManager = RebroadcastBroadcastManager.getInstance();
    if (rebroadcastBroadcastManager.isWriting(broadcast.id)) {
        mRebroadcastButton.setActivated(rebroadcastBroadcastManager.isWritingRebroadcast(broadcast.id));
        mRebroadcastButton.setEnabled(false);
    } else {
        mRebroadcastButton.setActivated(broadcast.isRebroadcasted());
        mRebroadcastButton.setEnabled(true);
    }
    mRebroadcastButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onRebroadcastClicked();
            }
        }
    });
    mCommentButton.setText(broadcast.getCommentCountString());
    mCommentButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            if (mListener != null) {
                mListener.onCommentClicked();
            }
        }
    });
    mBoundBroadcastId = broadcast.id;
}
Also used : Context(android.content.Context) HorizontalImageAdapter(me.zhanghai.android.douya.ui.HorizontalImageAdapter) Attachment(me.zhanghai.android.douya.network.api.info.apiv2.Attachment) Image(me.zhanghai.android.douya.network.api.info.apiv2.Image) ImageView(android.widget.ImageView) BindView(butterknife.BindView) TimeActionTextView(me.zhanghai.android.douya.ui.TimeActionTextView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) RebroadcastBroadcastManager(me.zhanghai.android.douya.broadcast.content.RebroadcastBroadcastManager) LikeBroadcastManager(me.zhanghai.android.douya.broadcast.content.LikeBroadcastManager)

Example 2 with Image

use of com.ibm.dtfj.image.j9.Image in project spring-cloud-gcp by spring-cloud.

the class VisionController method uploadImage.

/**
 * This method downloads an image from a URL and sends its contents to the Vision API for label detection.
 *
 * @param imageUrl the URL of the image
 * @return a string with the list of labels and percentage of certainty
 * @throws Exception if the Vision API call produces an error
 */
@GetMapping("/vision")
public String uploadImage(String imageUrl) throws Exception {
    // Copies the content of the image to memory.
    byte[] imageBytes = StreamUtils.copyToByteArray(this.resourceLoader.getResource(imageUrl).getInputStream());
    BatchAnnotateImagesResponse responses;
    Image image = Image.newBuilder().setContent(ByteString.copyFrom(imageBytes)).build();
    // Sets the type of request to label detection, to detect broad sets of categories in an image.
    Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();
    responses = this.imageAnnotatorClient.batchAnnotateImages(Collections.singletonList(request));
    StringBuilder responseBuilder = new StringBuilder("<table border=\"1\">");
    responseBuilder.append("<tr><th>description</th><th>score</th></tr>");
    // We're only expecting one response.
    if (responses.getResponsesCount() == 1) {
        AnnotateImageResponse response = responses.getResponses(0);
        if (response.hasError()) {
            throw new Exception(response.getError().getMessage());
        }
        for (EntityAnnotation annotation : response.getLabelAnnotationsList()) {
            responseBuilder.append("<tr><td>").append(annotation.getDescription()).append("</td><td>").append(annotation.getScore()).append("</td></tr>");
        }
    }
    responseBuilder.append("</table>");
    responseBuilder.append("<p><img src='" + imageUrl + "'/></p>");
    return responseBuilder.toString();
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with Image

use of com.ibm.dtfj.image.j9.Image in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectDocumentText.

// [START vision_detect_document]
/**
 * Performs document text detection on a local image file.
 *
 * @param filePath The path to the local file to detect document text on.
 * @param out A {@link PrintStream} to write the results to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectDocumentText(String filePath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            TextAnnotation annotation = res.getFullTextAnnotation();
            for (Page page : annotation.getPagesList()) {
                String pageText = "";
                for (Block block : page.getBlocksList()) {
                    String blockText = "";
                    for (Paragraph para : block.getParagraphsList()) {
                        String paraText = "";
                        for (Word word : para.getWordsList()) {
                            String wordText = "";
                            for (Symbol symbol : word.getSymbolsList()) {
                                wordText = wordText + symbol.getText();
                                out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), symbol.getConfidence());
                            }
                            out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        out.println("\nParagraph: \n" + paraText);
                        out.format("Paragraph Confidence: %f\n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            out.println("\nComplete annotation:");
            out.println(annotation.getText());
        }
    }
}
Also used : Word(com.google.cloud.vision.v1.Word) ByteString(com.google.protobuf.ByteString) Symbol(com.google.cloud.vision.v1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) Page(com.google.cloud.vision.v1.Page) ByteString(com.google.protobuf.ByteString) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) Paragraph(com.google.cloud.vision.v1.Paragraph) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Block(com.google.cloud.vision.v1.Block) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 4 with Image

use of com.ibm.dtfj.image.j9.Image in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLabelsGcs.

/**
 * Detects labels in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform label detection
 *                on.
 * @param out A {@link PrintStream} to write detected features to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLabelsGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString()));
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 5 with Image

use of com.ibm.dtfj.image.j9.Image in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectFacesGcs.

/**
 * Detects faces in the specified remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to perform face detection
 *                on.
 * @param out A {@link PrintStream} to write detected features to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectFacesGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.FACE_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
                out.printf("anger: %s\njoy: %s\nsurprise: %s\nposition: %s", annotation.getAngerLikelihood(), annotation.getJoyLikelihood(), annotation.getSurpriseLikelihood(), annotation.getBoundingPoly());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) FaceAnnotation(com.google.cloud.vision.v1.FaceAnnotation) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)28 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)28 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)28 Feature (com.google.cloud.vision.v1.Feature)28 Image (com.google.cloud.vision.v1.Image)28 ArrayList (java.util.ArrayList)28 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)27 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)25 ByteString (com.google.protobuf.ByteString)17 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)16 ImageSource (com.google.cloud.vision.v1.ImageSource)15 FileInputStream (java.io.FileInputStream)14 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)8 Block (com.google.cloud.vision.v1.Block)6 ColorInfo (com.google.cloud.vision.v1.ColorInfo)6 CropHint (com.google.cloud.vision.v1.CropHint)6 CropHintsAnnotation (com.google.cloud.vision.v1.CropHintsAnnotation)6 DominantColorsAnnotation (com.google.cloud.vision.v1.DominantColorsAnnotation)6 FaceAnnotation (com.google.cloud.vision.v1.FaceAnnotation)6 LocationInfo (com.google.cloud.vision.v1.LocationInfo)6