|
1 | 1 | ---
|
2 |
| -title: Computer Vision API Ruby quick start | Microsoft Docs |
3 |
| -description: Get information and code samples to help you quickly get started using Ruby and the Computer Vision API in Microsoft Cognitive Services. |
| 2 | +title: Computer Vision API Ruby quickstart summary | Microsoft Docs |
| 3 | +titleSuffix: "Microsoft Cognitive Services" |
| 4 | +description: In these quickstarts, you analyze an image, create a thumbnail, and extract printed text using Computer Vision with Ruby in Cognitive Services. |
4 | 5 | services: cognitive-services
|
5 |
| -author: easyj2j |
6 |
| - |
| 6 | +author: noellelacharite |
| 7 | +manager: nolachar |
7 | 8 | ms.service: cognitive-services
|
8 | 9 | ms.component: computer-vision
|
9 |
| -ms.topic: article |
10 |
| -ms.date: 5/07/2018 |
11 |
| -ms.author: v-johnma |
| 10 | +ms.topic: quickstart |
| 11 | +ms.date: 05/26/2018 |
| 12 | +ms.author: nolachar |
12 | 13 | ---
|
| 14 | +# Quickstart: Summary |
13 | 15 |
|
14 |
| -# Use the Computer Vision API with Ruby |
15 |
| - |
16 |
| -This article provides information and code samples to help you quickly get started using the Computer Vision API with Ruby to accomplish the following tasks: |
17 |
| -* [Analyze an image](#AnalyzeImage) |
18 |
| -* [Intelligently generate a thumbnail](#GetThumbnail) |
19 |
| -* [Detect and extract text from an Image](#OCR) |
20 |
| - |
21 |
| - |
22 |
| -## Prerequisites |
23 |
| - |
24 |
| -To use the Computer Vision API, you need a subscription key. You can get free subscription keys [here](../Vision-API-How-to-Topics/HowToSubscribe.md). |
25 |
| - |
26 |
| -## Analyze an Image With Computer Vision API Using Ruby <a name="AnalyzeImage"> </a> |
27 |
| - |
28 |
| -With the [Analyze Image method](https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fa), you can extract visual features based on image content. You can upload an image or specify an image URL and choose which features to return, including: |
29 |
| -* The category defined in this [taxonomy](../Category-Taxonomy.md). |
30 |
| -* A detailed list of tags related to the image content. |
31 |
| -* A description of image content in a complete sentence. |
32 |
| -* The coordinates, gender, and age of any faces contained in the image. |
33 |
| -* The ImageType (clipart or a line drawing) |
34 |
| -* The dominant color, the accent color, or whether an image is black & white. |
35 |
| -* Whether the image contains pornographic or sexually suggestive content. |
36 |
| - |
37 |
| -### Analyze an Image Ruby Example Request |
38 |
| - |
39 |
| -Change the REST URL to use the location where you obtained your subscription keys, replace the "Ocp-Apim-Subscription-Key" value with your valid subscription key, and add a URL to a photograph of a celebrity to the `body` variable. |
40 |
| - |
41 |
| -```ruby |
42 |
| -require 'net/http' |
43 |
| - |
44 |
| -# NOTE: You must use the same location in your REST call as you used to obtain your subscription keys. |
45 |
| -# For example, if you obtained your subscription keys from westus, replace "westcentralus" in the |
46 |
| -# URL below with "westus". |
47 |
| -uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze') |
48 |
| -uri.query = URI.encode_www_form({ |
49 |
| - # Request parameters |
50 |
| - 'visualFeatures' => 'Categories', |
51 |
| - 'details' => '{string}', |
52 |
| - 'language' => 'en' |
53 |
| -}) |
54 |
| - |
55 |
| -request = Net::HTTP::Post.new(uri.request_uri) |
56 |
| -# Request headers |
57 |
| -request['Content-Type'] = 'application/json' |
58 |
| -# NOTE: Replace the "Ocp-Apim-Subscription-Key" value with a valid subscription key. |
59 |
| -request['Ocp-Apim-Subscription-Key'] = '{subscription key}' |
60 |
| -# Replace with the body, for example, "{\"url\": \"http://www.example.com/images/image.jpg\"}" |
61 |
| -request.body = "{body}" |
62 |
| - |
63 |
| -response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| |
64 |
| - http.request(request) |
65 |
| -end |
66 |
| - |
67 |
| -puts response.body |
68 |
| - |
69 |
| -``` |
70 |
| -### Analyze an Image Response |
71 |
| - |
72 |
| -A successful response is returned in JSON. Following is an example of a successful response: |
73 |
| - |
74 |
| -```json |
75 |
| -{ |
76 |
| - "categories": [ |
77 |
| - { |
78 |
| - "name": "abstract_", |
79 |
| - "score": 0.00390625 |
80 |
| - }, |
81 |
| - { |
82 |
| - "name": "people_", |
83 |
| - "score": 0.83984375, |
84 |
| - "detail": { |
85 |
| - "celebrities": [ |
86 |
| - { |
87 |
| - "name": "Satya Nadella", |
88 |
| - "faceRectangle": { |
89 |
| - "left": 597, |
90 |
| - "top": 162, |
91 |
| - "width": 248, |
92 |
| - "height": 248 |
93 |
| - }, |
94 |
| - "confidence": 0.999028444 |
95 |
| - } |
96 |
| - ] |
97 |
| - } |
98 |
| - } |
99 |
| - ], |
100 |
| - "adult": { |
101 |
| - "isAdultContent": false, |
102 |
| - "isRacyContent": false, |
103 |
| - "adultScore": 0.0934349000453949, |
104 |
| - "racyScore": 0.068613491952419281 |
105 |
| - }, |
106 |
| - "tags": [ |
107 |
| - { |
108 |
| - "name": "person", |
109 |
| - "confidence": 0.98979085683822632 |
110 |
| - }, |
111 |
| - { |
112 |
| - "name": "man", |
113 |
| - "confidence": 0.94493889808654785 |
114 |
| - }, |
115 |
| - { |
116 |
| - "name": "outdoor", |
117 |
| - "confidence": 0.938492476940155 |
118 |
| - }, |
119 |
| - { |
120 |
| - "name": "window", |
121 |
| - "confidence": 0.89513939619064331 |
122 |
| - } |
123 |
| - ], |
124 |
| - "description": { |
125 |
| - "tags": [ |
126 |
| - "person", |
127 |
| - "man", |
128 |
| - "outdoor", |
129 |
| - "window", |
130 |
| - "glasses" |
131 |
| - ], |
132 |
| - "captions": [ |
133 |
| - { |
134 |
| - "text": "Satya Nadella sitting on a bench", |
135 |
| - "confidence": 0.48293603002174407 |
136 |
| - } |
137 |
| - ] |
138 |
| - }, |
139 |
| - "requestId": "0dbec5ad-a3d3-4f7e-96b4-dfd57efe967d", |
140 |
| - "metadata": { |
141 |
| - "width": 1500, |
142 |
| - "height": 1000, |
143 |
| - "format": "Jpeg" |
144 |
| - }, |
145 |
| - "faces": [ |
146 |
| - { |
147 |
| - "age": 44, |
148 |
| - "gender": "Male", |
149 |
| - "faceRectangle": { |
150 |
| - "left": 593, |
151 |
| - "top": 160, |
152 |
| - "width": 250, |
153 |
| - "height": 250 |
154 |
| - } |
155 |
| - } |
156 |
| - ], |
157 |
| - "color": { |
158 |
| - "dominantColorForeground": "Brown", |
159 |
| - "dominantColorBackground": "Brown", |
160 |
| - "dominantColors": [ |
161 |
| - "Brown", |
162 |
| - "Black" |
163 |
| - ], |
164 |
| - "accentColor": "873B59", |
165 |
| - "isBWImg": false |
166 |
| - }, |
167 |
| - "imageType": { |
168 |
| - "clipArtType": 0, |
169 |
| - "lineDrawingType": 0 |
170 |
| - } |
171 |
| -} |
172 |
| - |
173 |
| -``` |
174 |
| - |
175 |
| -## Get a Thumbnail with Computer Vision API Using Ruby <a name="GetThumbnail"> </a> |
176 |
| - |
177 |
| -Use the [Get Thumbnail method](https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fb) to crop an image based on its region of interest (ROI) to the height and width you desire, even if the aspect ratio differs from the input image. |
178 |
| - |
179 |
| -### Get a Thumbnail Ruby Example Request |
180 |
| - |
181 |
| -Change the REST URL to use the location where you obtained your subscription keys, replace the "Ocp-Apim-Subscription-Key" value with your valid subscription key, and add a URL to a photograph of a celebrity to the `body` variable. |
182 |
| - |
183 |
| -```ruby |
184 |
| -require 'net/http' |
185 |
| - |
186 |
| -# NOTE: You must use the same location in your REST call as you used to obtain your subscription keys. |
187 |
| -# For example, if you obtained your subscription keys from westus, replace "westcentralus" in the |
188 |
| -# URL below with "westus". |
189 |
| -uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/generateThumbnail') |
190 |
| -uri.query = URI.encode_www_form({ |
191 |
| - # Request parameters |
192 |
| - 'width' => '{number}', |
193 |
| - 'height' => '{number}', |
194 |
| - 'smartCropping' => 'true' |
195 |
| -}) |
196 |
| - |
197 |
| -request = Net::HTTP::Post.new(uri.request_uri) |
198 |
| -# Request headers |
199 |
| -request['Content-Type'] = 'application/json' |
200 |
| -# NOTE: Replace the "Ocp-Apim-Subscription-Key" value with a valid subscription key. |
201 |
| -request['Ocp-Apim-Subscription-Key'] = '{subscription key}' |
202 |
| -# Replace with the body, for example, "{\"url\": \"http://www.example.com/images/image.jpg\"}" |
203 |
| -request.body = "{body}" |
204 |
| - |
205 |
| -response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| |
206 |
| - http.request(request) |
207 |
| -end |
208 |
| - |
209 |
| -puts response.body |
210 |
| -``` |
211 |
| - |
212 |
| -### Get a Thumbnail Response |
213 |
| - |
214 |
| -A successful response contains the thumbnail image binary. If the request failed, the response contains an error code and a message to help determine what went wrong. |
215 |
| - |
216 |
| -## Optical Character Recognition (OCR) with Computer Vision API Using Ruby <a name="OCR"> </a> |
217 |
| - |
218 |
| -Use the [Optical Character Recognition (OCR) method](https://westus.dev.cognitive.microsoft.com/docs/services/5adf991815e1060e6355ad44/operations/56f91f2e778daf14a499e1fc) to detect text in an image and extract recognized characters into a machine-usable character stream. |
219 |
| - |
220 |
| -### OCR Ruby Example Request |
221 |
| - |
222 |
| -Change the REST URL to use the location where you obtained your subscription keys, replace the "Ocp-Apim-Subscription-Key" value with your valid subscription key, and add a URL to a photograph of a celebrity to the `body` variable. |
223 |
| - |
224 |
| -```ruby |
225 |
| -require 'net/http' |
226 |
| - |
227 |
| -# NOTE: You must use the same location in your REST call as you used to obtain your subscription keys. |
228 |
| -# For example, if you obtained your subscription keys from westus, replace "westcentralus" in the |
229 |
| -# URL below with "westus". |
230 |
| -uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/ocr') |
231 |
| -uri.query = URI.encode_www_form({ |
232 |
| - # Request parameters |
233 |
| - 'language' => 'unk', |
234 |
| - 'detectOrientation ' => 'true' |
235 |
| -}) |
236 |
| - |
237 |
| -request = Net::HTTP::Post.new(uri.request_uri) |
238 |
| -# Request headers |
239 |
| -request['Content-Type'] = 'application/json' |
240 |
| -# NOTE: Replace the "Ocp-Apim-Subscription-Key" value with a valid subscription key. |
241 |
| -request['Ocp-Apim-Subscription-Key'] = '{subscription key}' |
242 |
| -# Replace with the body, for example, "{\"url\": \"http://www.example.com/images/image.jpg\"}" |
243 |
| -request.body = "{body}" |
244 |
| - |
245 |
| -response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| |
246 |
| - http.request(request) |
247 |
| -end |
248 |
| - |
249 |
| -puts response.body |
250 |
| - |
251 |
| -``` |
| 16 | +These quickstarts provide information and code samples to help you quickly get started using the Computer Vision API with Ruby to accomplish the following tasks: |
252 | 17 |
|
253 |
| -### OCR Example Response |
| 18 | +* [Analyze an image](ruby-analyze.md) |
| 19 | +* [Intelligently generate a thumbnail](ruby-thumb.md) |
| 20 | +* [Detect and extract printed text from an image](ruby-print-text.md) |
254 | 21 |
|
255 |
| -Upon success, the OCR results returned include text, bounding box for regions, lines, and words. |
| 22 | +The code in these samples is similar. However, they highlight different Computer Vision features along with different techniques for exchanging data with the service, as summarized in the following table: |
256 | 23 |
|
257 |
| -```json |
258 |
| -{ |
259 |
| - "language": "en", |
260 |
| - "textAngle": -2.0000000000000338, |
261 |
| - "orientation": "Up", |
262 |
| - "regions": [ |
263 |
| - { |
264 |
| - "boundingBox": "462,379,497,258", |
265 |
| - "lines": [ |
266 |
| - { |
267 |
| - "boundingBox": "462,379,497,74", |
268 |
| - "words": [ |
269 |
| - { |
270 |
| - "boundingBox": "462,379,41,73", |
271 |
| - "text": "A" |
272 |
| - }, |
273 |
| - { |
274 |
| - "boundingBox": "523,379,153,73", |
275 |
| - "text": "GOAL" |
276 |
| - }, |
277 |
| - { |
278 |
| - "boundingBox": "694,379,265,74", |
279 |
| - "text": "WITHOUT" |
280 |
| - } |
281 |
| - ] |
282 |
| - }, |
283 |
| - { |
284 |
| - "boundingBox": "565,471,289,74", |
285 |
| - "words": [ |
286 |
| - { |
287 |
| - "boundingBox": "565,471,41,73", |
288 |
| - "text": "A" |
289 |
| - }, |
290 |
| - { |
291 |
| - "boundingBox": "626,471,150,73", |
292 |
| - "text": "PLAN" |
293 |
| - }, |
294 |
| - { |
295 |
| - "boundingBox": "801,472,53,73", |
296 |
| - "text": "IS" |
297 |
| - } |
298 |
| - ] |
299 |
| - }, |
300 |
| - { |
301 |
| - "boundingBox": "519,563,375,74", |
302 |
| - "words": [ |
303 |
| - { |
304 |
| - "boundingBox": "519,563,149,74", |
305 |
| - "text": "JUST" |
306 |
| - }, |
307 |
| - { |
308 |
| - "boundingBox": "683,564,41,72", |
309 |
| - "text": "A" |
310 |
| - }, |
311 |
| - { |
312 |
| - "boundingBox": "741,564,153,73", |
313 |
| - "text": "WISH" |
314 |
| - } |
315 |
| - ] |
316 |
| - } |
317 |
| - ] |
318 |
| - } |
319 |
| - ] |
320 |
| -} |
321 |
| -``` |
| 24 | +| Quickstart | Request Parameters | Response | |
| 25 | +| ------------------------ | ------------------------------------------- | ---------------- | |
| 26 | +| Analyze an image | visualFeatures=Categories,Description,Color | JSON string | |
| 27 | +| Generate a thumbnail | width=200&height=150&smartCropping=true | byte array | |
| 28 | +| Extract printed text | language=unk&detectOrientation=true | JSON string | |
0 commit comments