Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

93 строки
2.3 KiB

  1. // Copyright 2016 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. // http://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. package vision_test
  15. import (
  16. "context"
  17. "fmt"
  18. "os"
  19. vision "cloud.google.com/go/vision/apiv1"
  20. pb "google.golang.org/genproto/googleapis/cloud/vision/v1"
  21. )
  22. func Example_NewImageFromReader() {
  23. f, err := os.Open("path/to/image.jpg")
  24. if err != nil {
  25. // TODO: handle error.
  26. }
  27. img, err := vision.NewImageFromReader(f)
  28. if err != nil {
  29. // TODO: handle error.
  30. }
  31. fmt.Println(img)
  32. }
  33. func Example_NewImageFromURI() {
  34. img := vision.NewImageFromURI("gs://my-bucket/my-image.png")
  35. fmt.Println(img)
  36. }
  37. func ExampleImageAnnotatorClient_AnnotateImage() {
  38. ctx := context.Background()
  39. c, err := vision.NewImageAnnotatorClient(ctx)
  40. if err != nil {
  41. // TODO: Handle error.
  42. }
  43. res, err := c.AnnotateImage(ctx, &pb.AnnotateImageRequest{
  44. Image: vision.NewImageFromURI("gs://my-bucket/my-image.png"),
  45. Features: []*pb.Feature{
  46. {Type: pb.Feature_LANDMARK_DETECTION, MaxResults: 5},
  47. {Type: pb.Feature_LABEL_DETECTION, MaxResults: 3},
  48. },
  49. })
  50. if err != nil {
  51. // TODO: Handle error.
  52. }
  53. // TODO: Use res.
  54. _ = res
  55. }
  56. func Example_FaceFromLandmarks() {
  57. ctx := context.Background()
  58. c, err := vision.NewImageAnnotatorClient(ctx)
  59. if err != nil {
  60. // TODO: Handle error.
  61. }
  62. resp, err := c.BatchAnnotateImages(ctx, &pb.BatchAnnotateImagesRequest{
  63. Requests: []*pb.AnnotateImageRequest{
  64. {
  65. Image: vision.NewImageFromURI("gs://bucket/image.jpg"),
  66. Features: []*pb.Feature{{
  67. Type: pb.Feature_FACE_DETECTION,
  68. MaxResults: 5,
  69. }},
  70. },
  71. },
  72. })
  73. if err != nil {
  74. // TODO: Handle error.
  75. }
  76. res := resp.Responses[0]
  77. if res.Error != nil {
  78. // TODO: Handle error.
  79. }
  80. for _, a := range res.FaceAnnotations {
  81. face := vision.FaceFromLandmarks(a.Landmarks)
  82. fmt.Println(face.Nose.Tip)
  83. fmt.Println(face.Eyes.Left.Pupil)
  84. }
  85. }