Skip to content

Commit 9df39fe

Browse files
authored
Merge pull request #115038 from v-jaswel/patch-12
[DO NOT MERGE UNTIL BUILD] Update CSharp-thumb.md, curl-thumb.md, go-thumb.md, javascript-thumb.md, java-thumb.md, node-thumb.md, python-thumb.md
2 parents 4ac5677 + 34c25ec commit 9df39fe

File tree

7 files changed

+164
-160
lines changed

7 files changed

+164
-160
lines changed

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,20 @@ namespace CSHttpClientSample
4848
{
4949
static class Program
5050
{
51-
// Add your Computer Vision subscription key and endpoint to your environment variables.
51+
// Add your Computer Vision subscription key and base endpoint to your environment variables.
5252
static string subscriptionKey = Environment.GetEnvironmentVariable("COMPUTER_VISION_SUBSCRIPTION_KEY");
53-
5453
static string endpoint = Environment.GetEnvironmentVariable("COMPUTER_VISION_ENDPOINT");
5554

56-
// the GenerateThumbnail method endpoint
57-
static string uriBase = endpoint + "vision/v2.1/generateThumbnail";
55+
// The GenerateThumbnail method endpoint
56+
static string uriBase = endpoint + "vision/v3.0/generateThumbnail";
57+
// Add an image to your bin/debug/netcoreappX.X folder, then add the image name (with extension), here
58+
static string imageFilePath = @"my-image-name";
5859

59-
static async Task Main()
60+
public static void Main()
6061
{
61-
// Get the path and filename to process from the user.
62-
Console.WriteLine("Thumbnail:");
63-
Console.Write(
64-
"Enter the path to the image you wish to use to create a thumbnail image: ");
65-
string imageFilePath = Console.ReadLine();
6662

67-
if (File.Exists(imageFilePath))
68-
{
69-
// Call the REST API method.
70-
Console.WriteLine("\nWait a moment for the results to appear.\n");
71-
await MakeThumbNailRequest(imageFilePath);
72-
}
73-
else
74-
{
75-
Console.WriteLine("\nInvalid file path");
76-
}
63+
MakeThumbNailRequest(imageFilePath).Wait();
64+
7765
Console.WriteLine("\nPress Enter to exit...");
7866
Console.ReadLine();
7967
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ To create and run the sample, do the following steps:
3636
[!INCLUDE [Custom subdomains notice](../../../../includes/cognitive-services-custom-subdomains-note.md)]
3737
1. Optionally, change the image URL in the request body (`https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\`) to the URL of a different image from which to generate a thumbnail.
3838
1. Open a command prompt window.
39-
1. Paste the command from the text editor into the command prompt window, and then run the command.
39+
1. Paste the command from the text editor into the command prompt window.
40+
1. Press enter to run the program.
4041

4142
```bash
42-
curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -o <thumbnailFile> -H "Content-Type: application/json" "https://westcentralus.api.cognitive.microsoft.com/vision/v2.1/generateThumbnail?width=100&height=100&smartCropping=true" -d "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\"}"
43+
curl -H "Ocp-Apim-Subscription-Key: <subscriptionKey>" -o <thumbnailFile> -H "Content-Type: application/json" "https://westus.api.cognitive.microsoft.com/vision/v2.1/generateThumbnail?width=100&height=100&smartCropping=true" -d "{\"url\":\"https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/Shorkie_Poo_Puppy.jpg/1280px-Shorkie_Poo_Puppy.jpg\"}"
4344
```
4445

4546
## Examine the response

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

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

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,61 @@ To create and run the sample, do the following steps:
3030

3131
1. Create a new Java project in your favorite IDE or editor. If the option is available, create the Java project from a command line application template.
3232
1. Import the following libraries into your Java project. If you're using Maven, the Maven coordinates are provided for each library.
33-
- [Apache HTTP client](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpclient:4.5.5)
34-
- [Apache HTTP core](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpcore:4.4.9)
33+
- [Apache HTTP client](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpclient:4.5.X)
34+
- [Apache HTTP core](https://hc.apache.org/downloads.cgi) (org.apache.httpcomponents:httpcore:4.4.X)
3535
- [JSON library](https://github.com/stleary/JSON-java) (org.json:json:20180130)
36-
1. Add the following `import` statements to the file that contains the `Main` public class for your project.
36+
1. Add the following `import` statements to your main class:
3737

3838
```java
39-
import java.awt.*;
40-
import javax.swing.*;
41-
import java.net.URI;
42-
import java.io.InputStream;
43-
import javax.imageio.ImageIO;
44-
import java.awt.image.BufferedImage;
45-
import org.apache.http.HttpEntity;
46-
import org.apache.http.HttpResponse;
47-
import org.apache.http.client.methods.HttpPost;
48-
import org.apache.http.entity.StringEntity;
49-
import org.apache.http.client.utils.URIBuilder;
50-
import org.apache.http.impl.client.CloseableHttpClient;
51-
import org.apache.http.impl.client.HttpClientBuilder;
52-
import org.apache.http.util.EntityUtils;
53-
import org.json.JSONObject;
39+
import java.awt.*;
40+
import javax.swing.*;
41+
import java.net.URI;
42+
import java.io.InputStream;
43+
import javax.imageio.ImageIO;
44+
import java.awt.image.BufferedImage;
45+
import org.apache.http.HttpEntity;
46+
import org.apache.http.HttpResponse;
47+
import org.apache.http.client.methods.HttpPost;
48+
import org.apache.http.entity.StringEntity;
49+
import org.apache.http.client.utils.URIBuilder;
50+
import org.apache.http.impl.client.CloseableHttpClient;
51+
import org.apache.http.impl.client.HttpClientBuilder;
52+
import org.apache.http.util.EntityUtils;
53+
import org.json.JSONObject;
5454
```
5555

56-
1. Replace the `Main` public class with the following code.
57-
1. Optionally, replace the value of `imageToAnalyze` with the URL of a different image for which you want to generate a thumbnail.
56+
1. Add the rest of the sample code below, beneath the imports (change to your class name if needed).
57+
1. Add your Computer Vision subscription key and endpoint to your environment variables.
58+
1. Optionally, replace the value of `imageToAnalyze` with the URL of your own image.
5859
1. Save, then build the Java project.
59-
1. If you're using an IDE, run `Main`. Otherwise, open a command prompt window and then use the `java` command to run the compiled class. For example, `java Main`.
60+
1. If you're using an IDE, run `GenerateThumbnail`. Otherwise, run from the command line (commands below).
6061

6162
```java
62-
// This sample uses the following libraries:
63-
// - Apache HTTP client (org.apache.httpcomponents:httpclient:4.5.5)
64-
// - Apache HTTP core (org.apache.httpcomponents:httpccore:4.4.9)
65-
// - JSON library (org.json:json:20180130).
66-
67-
68-
public class Main {
69-
// **********************************************
70-
// *** Update or verify the following values. ***
71-
// **********************************************
72-
73-
// Add your Computer Vision subscription key and endpoint to your environment variables.
74-
// After setting, close and then re-open your command shell or project for the changes to take effect.
75-
String subscriptionKey = System.getenv("COMPUTER_VISION_SUBSCRIPTION_KEY");
76-
String endpoint = ("COMPUTER_VISION_ENDPOINT");
77-
78-
private static final String uriBase = endpoint +
79-
"vision/v2.1/generateThumbnail";
80-
81-
private static final String imageToAnalyze =
82-
"https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg";
63+
/**
64+
* This sample uses the following libraries (create a "lib" folder to place them in):
65+
* Apache HTTP client:
66+
* org.apache.httpcomponents:httpclient:4.5.X
67+
* Apache HTTP core:
68+
* org.apache.httpcomponents:httpccore:4.4.X
69+
* JSON library:
70+
* org.json:json:20180130
71+
*
72+
* To build/run from the command line:
73+
* javac GenerateThumbnail.java -cp .;lib\*
74+
* java -cp .;lib\* GenerateThumbnail
75+
*/
76+
77+
public class GenerateThumbnail {
78+
79+
// Add your Computer Vision subscription key and endpoint to your environment
80+
// variables. Then, close and then re-open your command shell or project for the
81+
// changes to take effect.
82+
private static String subscriptionKey = System.getenv("COMPUTER_VISION_SUBSCRIPTION_KEY");
83+
private static String endpoint = System.getenv("COMPUTER_VISION_ENDPOINT");
84+
// The endpoint path
85+
private static final String uriBase = endpoint + "vision/v3.0/generateThumbnail";
86+
// It's optional if you'd like to use your own image instead of this one.
87+
private static final String imageToAnalyze = "https://upload.wikimedia.org/wikipedia/commons/9/94/Bloodhound_Puppy.jpg";
8388

8489
public static void main(String[] args) {
8590
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
@@ -101,14 +106,15 @@ public class Main {
101106
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
102107

103108
// Request body.
104-
StringEntity requestEntity =
105-
new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
109+
StringEntity requestEntity = new StringEntity("{\"url\":\"" + imageToAnalyze + "\"}");
106110
request.setEntity(requestEntity);
107111

108112
// Call the REST API method and get the response entity.
109113
HttpResponse response = httpClient.execute(request);
110114
HttpEntity entity = response.getEntity();
111115

116+
System.out.println("status" + response.getStatusLine().getStatusCode());
117+
112118
// Check for success.
113119
if (response.getStatusLine().getStatusCode() == 200) {
114120
// Display the thumbnail.
@@ -123,11 +129,12 @@ public class Main {
123129
}
124130
} catch (Exception e) {
125131
System.out.println(e.getMessage());
132+
e.printStackTrace();
126133
}
127134
}
128135

129136
// Displays the given input stream as an image.
130-
private static void displayImage(InputStream inputStream) {
137+
public static void displayImage(InputStream inputStream) {
131138
try {
132139
BufferedImage bufferedImage = ImageIO.read(inputStream);
133140

@@ -161,4 +168,4 @@ Explore a Java Swing application that uses Computer Vision to perform optical ch
161168
> [!div class="nextstepaction"]
162169
> [Computer Vision API Java Tutorial](../Tutorials/java-tutorial.md)
163170
164-
* To rapidly experiment with the Computer Vision API, try the [Open API testing console](https://westcentralus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa/console).
171+
* To rapidly experiment with the Computer Vision API, try the [Open API testing console](https://westcentralus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa/console).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ To create and run the sample, do the following steps:
5151
var subscriptionKey = document.getElementById("subscriptionKey").value;
5252
var endpoint = document.getElementById("endpointUrl").value;
5353
54-
var uriBase = endpoint + "vision/v2.1/generateThumbnail";
54+
var uriBase = endpoint + "vision/v3.0/generateThumbnail";
5555
5656
// Request parameters.
5757
var params = "?width=100&height=150&smartCropping=true";

0 commit comments

Comments
 (0)