Perform handwritten text detection on a file stored in Cloud Storage (for beta launch).

## Explore further


For detailed documentation that includes this code sample, see the following:

- [Detect handwritten text in a Cloud Storage file (beta)](https://docs.cloud.google.com/vision/docs/samples/vision-handwritten-ocr-gcs-beta)

## Code sample

### Java


To learn how to install and use the client library for Vision API Product Search, see
[Vision API Product Search client libraries](https://docs.cloud.google.com/vision/product-search/docs/libraries).


For more information, see the
[Vision API Product Search Java API
reference documentation](https://docs.cloud.google.com/java/docs/reference/google-cloud-vision/latest/overview).


To authenticate to Vision API Product Search, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    /**
     * Performs handwritten text detection on a remote image on Google Cloud Storage.
     *
     * @param gcsPath The path to the remote file on Google Cloud Storage to detect handwritten 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 detectHandwrittenOcrGcs(String gcsPath, PrintStream out) throws Exception {
      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.DOCUMENT_TEXT_DETECTION).build();
      // Set the parameters for the image
      ImageContext imageContext =
          ImageContext.newBuilder().addLanguageHints("en-t-i0-handwrit").build();

      AnnotateImageRequest request =
          AnnotateImageRequest.newBuilder()
              .addFeatures(feat)
              .setImage(img)
              .setImageContext(imageContext)
              .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());
        }
      }
    }

### Node.js


To learn how to install and use the client library for Vision API Product Search, see
[Vision API Product Search client libraries](https://docs.cloud.google.com/vision/product-search/docs/libraries).


For more information, see the
[Vision API Product Search Node.js API
reference documentation](https://googleapis.dev/nodejs/vision/latest).


To authenticate to Vision API Product Search, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    // Imports the Google Cloud client libraries
    const vision = require('https://docs.cloud.google.com/nodejs/docs/reference/vision/latest/overview.html').v1p3beta1;
    const fs = require('fs');

    // Creates a client
    const client = new vision.https://docs.cloud.google.com/nodejs/docs/reference/vision/latest/overview.html();

    /**
     * TODO(developer): Uncomment the following line before running the sample.
     */
    // const uri = `gs://bucket/bucketImage.png`;

    const request = {
      image: {
        content: fs.readFileSync(uri),
      },
      feature: {
        languageHints: ['en-t-i0-handwrit'],
      },
    };

    const [result] = await client.documentTextDetection(request);
    const fullTextAnnotation = https://docs.cloud.google.com/nodejs/docs/reference/vision/latest/vision/protos.google.longrunning.operation.html.fullTextAnnotation;
    console.log(`Full text: ${fullTextAnnotation.text}`);

### Python


To learn how to install and use the client library for Vision API Product Search, see
[Vision API Product Search client libraries](https://docs.cloud.google.com/vision/product-search/docs/libraries).


For more information, see the
[Vision API Product Search Python API
reference documentation](https://docs.cloud.google.com/python/docs/reference/vision/latest).


To authenticate to Vision API Product Search, set up Application Default Credentials.
For more information, see

[Set up authentication for a local development environment](https://docs.cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).

    def detect_handwritten_ocr_uri(uri):
        """Detects handwritten characters in the file located in Google Cloud
        Storage.

        Args:
        uri: The path to the file in Google Cloud Storage (gs://...)
        """
        from google.cloud import vision_v1p3beta1 as vision

        client = vision.ImageAnnotatorClient()
        image = vision.Image()
        image.source.image_uri = uri

        # Language hint codes for handwritten OCR:
        # en-t-i0-handwrit, mul-Latn-t-i0-handwrit
        # Note: Use only one language hint code per request for handwritten OCR.
        image_context = vision.ImageContext(language_hints=["en-t-i0-handwrit"])

        response = client.document_text_detection(image=image, image_context=image_context)

        print(f"Full Text: {response.full_text_annotation.text}")
        for page in response.full_text_annotation.pages:
            for block in page.blocks:
                print(f"\nBlock confidence: {block.confidence}\n")

                for paragraph in block.paragraphs:
                    print("Paragraph confidence: {}".format(paragraph.confidence))

                    for word in paragraph.words:
                        word_text = "".join([symbol.text for symbol in word.symbols])
                        print(
                            "Word text: {} (confidence: {})".format(
                                word_text, word.confidence
                            )
                        )

                        for symbol in word.symbols:
                            print(
                                "\tSymbol: {} (confidence: {})".format(
                                    symbol.text, symbol.confidence
                                )
                            )

        if response.error.message:
            raise Exception(
                "{}\nFor more info on error messages, check: "
                "https://cloud.google.com/apis/design/errors".format(response.error.message)
            )

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=cloudvision).