Skip to content

Commit 34c25ec

Browse files
authored
Update go-thumb.md
1 parent 18b3592 commit 34c25ec

File tree

1 file changed

+64
-64
lines changed
  • articles/cognitive-services/Computer-vision/QuickStarts

1 file changed

+64
-64
lines changed

articles/cognitive-services/Computer-vision/QuickStarts/go-thumb.md

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -39,73 +39,73 @@ To create and run the sample, do the following steps:
3939
package main
4040

4141
import (
42-
"encoding/json"
43-
"fmt"
44-
"io/ioutil"
45-
"net/http"
46-
"strings"
47-
"time"
42+
"bytes"
43+
"fmt"
44+
"io/ioutil"
45+
"io"
46+
"log"
47+
"net/http"
48+
"os"
49+
"strings"
50+
"time"
4851
)
4952

5053
func main() {
51-
// Add your Computer Vision subscription key and endpoint to your environment variables.
52-
subscriptionKey := os.Getenv("COMPUTER_VISION_SUBSCRIPTION_KEY")
53-
if (subscriptionKey == "") {
54-
log.Fatal("\n\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n" +
55-
"**Restart your shell or IDE for changes to take effect.**\n")
56-
57-
endpoint := os.Getenv("COMPUTER_VISION_ENDPOINT")
58-
if ("" == endpoint) {
59-
log.Fatal("\n\nSet the COMPUTER_VISION_ENDPOINT environment variable.\n" +
60-
"**Restart your shell or IDE for changes to take effect.**")
61-
}
62-
const uriBase = endpoint + "vision/v2.1/generateThumbnail"
63-
const imageUrl =
64-
"https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"
65-
66-
const params = "?width=100&height=100&smartCropping=true"
67-
const uri = uriBase + params
68-
const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"
69-
70-
reader := strings.NewReader(imageUrlEnc)
71-
72-
// Create the HTTP client
73-
client := &http.Client{
74-
Timeout: time.Second * 2,
75-
}
76-
77-
// Create the POST request, passing the image URL in the request body
78-
req, err := http.NewRequest("POST", uri, reader)
79-
if err != nil {
80-
panic(err)
81-
}
82-
83-
// Add headers
84-
req.Header.Add("Content-Type", "application/json")
85-
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
86-
87-
// Send the request and retrieve the response
88-
resp, err := client.Do(req)
89-
if err != nil {
90-
panic(err)
91-
}
92-
93-
defer resp.Body.Close()
94-
95-
// Read the response body.
96-
// Note, data is a byte array
97-
data, err := ioutil.ReadAll(resp.Body)
98-
if err != nil {
99-
panic(err)
100-
}
101-
102-
// Parse the JSON data
103-
var f interface{}
104-
json.Unmarshal(data, &f)
105-
106-
// Format and display the JSON result
107-
jsonFormatted, _ := json.MarshalIndent(f, "", " ")
108-
fmt.Println(string(jsonFormatted))
54+
// Add your Computer Vision subscription key and endpoint to your environment variables.
55+
subscriptionKey := os.Getenv("COMPUTER_VISION_SUBSCRIPTION_KEY")
56+
endpoint := os.Getenv("COMPUTER_VISION_ENDPOINT")
57+
58+
uriBase := endpoint + "vision/v3.0/generateThumbnail"
59+
const imageUrl = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg"
60+
61+
const params = "?width=100&height=100&smartCropping=true"
62+
uri := uriBase + params
63+
const imageUrlEnc = "{\"url\":\"" + imageUrl + "\"}"
64+
65+
reader := strings.NewReader(imageUrlEnc)
66+
67+
// Create the HTTP client
68+
client := &http.Client{
69+
Timeout: time.Second * 2,
70+
}
71+
72+
// Create the POST request, passing the image URL in the request body
73+
req, err := http.NewRequest("POST", uri, reader)
74+
if err != nil {
75+
panic(err)
76+
}
77+
78+
// Add headers
79+
req.Header.Add("Content-Type", "application/json")
80+
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
81+
82+
// Send the request and retrieve the response
83+
resp, err := client.Do(req)
84+
if err != nil {
85+
panic(err)
86+
}
87+
88+
defer resp.Body.Close()
89+
90+
// Read the response body.
91+
// Note, data is a byte array
92+
data, err := ioutil.ReadAll(resp.Body)
93+
if err != nil {
94+
panic(err)
95+
}
96+
97+
// Convert byte[] to io.Reader type
98+
readerThumb := bytes.NewReader(data)
99+
100+
// Write the image binary to file
101+
file, err := os.Create("thumb_local.png")
102+
if err != nil { log.Fatal(err) }
103+
defer file.Close()
104+
_, err = io.Copy(file, readerThumb)
105+
if err != nil { log.Fatal(err) }
106+
107+
fmt.Println("The thunbnail from local has been saved to file.")
108+
fmt.Println()
109109
}
110110
```
111111

0 commit comments

Comments
 (0)