Skip to content

Commit b69d3ef

Browse files
davidcavazostbpg
authored andcommitted
vision: add set_endpoint sample (#1113)
1 parent 002578a commit b69d3ef

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

vision/detect/set_endpoint.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
// [START vision_set_endpoint]
18+
import (
19+
"context"
20+
"fmt"
21+
22+
vision "cloud.google.com/go/vision/apiv1"
23+
"google.golang.org/api/option"
24+
)
25+
26+
// setEndpoint changes your endpoint.
27+
func setEndpoint(endpoint string) error {
28+
// endpoint := "eu-vision.googleapis.com:443"
29+
30+
ctx := context.Background()
31+
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
32+
if err != nil {
33+
return fmt.Errorf("NewImageAnnotatorClient: %v", err)
34+
}
35+
defer client.Close()
36+
37+
return nil
38+
}
39+
40+
// [END vision_set_endpoint]

vision/detect/set_endpoint_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package main
16+
17+
import (
18+
"context"
19+
"strings"
20+
"testing"
21+
22+
vision "cloud.google.com/go/vision/apiv1"
23+
"google.golang.org/api/option"
24+
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
25+
)
26+
27+
func TestSetEndpoint(t *testing.T) {
28+
const endpoint = "eu-vision.googleapis.com:443"
29+
30+
// Run the code sample to check for errors.
31+
err := setEndpoint(endpoint)
32+
if err != nil {
33+
t.Fatalf("setEndpoint: %v", err)
34+
}
35+
36+
// Since we're not returning the client from the code sample, we create an equivalent client here.
37+
ctx := context.Background()
38+
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
39+
if err != nil {
40+
t.Fatalf("NewImageAnnotatorClient: %v", err)
41+
}
42+
defer client.Close()
43+
44+
image := &visionpb.Image{
45+
Source: &visionpb.ImageSource{
46+
GcsImageUri: "gs://cloud-samples-data/vision/text/screen.jpg",
47+
},
48+
}
49+
texts, err := client.DetectTexts(ctx, image, nil, 1)
50+
if err != nil {
51+
t.Fatalf("DetectTexts: %v", err)
52+
}
53+
54+
text := texts[0]
55+
if got, want := text.GetDescription(), "System"; !strings.Contains(got, want) {
56+
t.Errorf("text.GetDescription() got:\n----\n%s----\nWant to contain:\n----\n%s\n----", got, want)
57+
}
58+
if len(text.GetBoundingPoly().GetVertices()) == 0 {
59+
t.Errorf("text.GetBoundingPoly().getVertices() must have at least one vertex")
60+
}
61+
}

0 commit comments

Comments
 (0)