Skip to content

Latest commit

 

History

History
108 lines (81 loc) · 4.38 KB

File metadata and controls

108 lines (81 loc) · 4.38 KB

Quickstart: Generate a thumbnail using the Computer Vision REST API with Go

In this quickstart, you'll generate a thumbnail from an image using the Computer Vision REST API. You specify the height and width, which can differ in aspect ratio from the input image. Computer Vision uses smart cropping to intelligently identify the area of interest and generate cropping coordinates based on that region.

Prerequisites

  • An Azure subscription - Create one for free
  • Go
  • Once you have your Azure subscription, create a Computer Vision resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
    • You will need the key and endpoint from the resource you create to connect your application to the Computer Vision service. You'll paste your key and endpoint into the code below later in the quickstart.
    • You can use the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production.

Create and run the sample

To create and run the sample, do the following steps:

  1. Copy the following code into a text editor.
  2. Replace the values of key and endpoint with your Computer Vision key and endpoint.
  3. Optionally, replace the value of imageUrl with the URL of a different image from which you want to generate a thumbnail.
  4. Save the code as a file with a .go extension. For example, get-thumbnail.go.
  5. Open a command prompt window.
  6. At the prompt, run the go build command to compile the package from the file. For example, go build get-thumbnail.go.
  7. At the prompt, run the compiled package. For example, get-thumbnail.
package main

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "io"
    "log"
    "net/http"
    "os"
    "strings"
    "time"
)

func main() {
    // Add your Computer Vision key and endpoint to your environment variables.
    key := "PASTE_YOUR_COMPUTER_VISION_KEY_HERE"
    endpoint := "PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE"

    uriBase := endpoint + "vision/v3.1/generateThumbnail"
    const imageUrl = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"

    const params = "?width=100&height=100&smartCropping=true"
    uri := uriBase + params
    const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"

    reader := strings.NewReader(imageUrlEnc)

    // Create the HTTP client
    client := &http.Client{
        Timeout: time.Second * 2,
    }

    // Create the POST request, passing the image URL in the request body
    req, err := http.NewRequest("POST", uri, reader)
    if err != nil {
        panic(err)
    }

    // Add headers
    req.Header.Add("Content-Type", "application/json")
    req.Header.Add("Ocp-Apim-Subscription-Key", key)

    // Send the request and retrieve the response
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()

    // Read the response body.
    // Note, data is a byte array
    data, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }
    
    // Convert byte[] to io.Reader type
    readerThumb := bytes.NewReader(data)

    // Write the image binary to file
    file, err := os.Create("thumb_local.png")
    if err != nil { log.Fatal(err) }
    defer file.Close()
    _, err = io.Copy(file, readerThumb)
    if err != nil { log.Fatal(err) }

    fmt.Println("The thunbnail from local has been saved to file.")
    fmt.Println()
}

Examine the response

A successful response contains the thumbnail image binary data. If the request fails, the response contains an error code and a message to help determine what went wrong.

Next steps

Explore the Computer Vision API to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text. To rapidly experiment with the Computer Vision API, try the Open API testing console.