/
main.go
147 lines (133 loc) · 4.72 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
"log"
"golang.org/x/oauth2/clientcredentials"
"google.golang.org/api/option"
gapic "github.com/animeapis/api-go-client/vision/v1alpha1"
vision "github.com/animeapis/go-genproto/vision/v1alpha1"
)
var (
ImageAnnotationParent = "[USER]"
Resource = "[RESOURCE]"
ClientID = "[CLIENT-ID]"
ClientSecret = "[CLIENT-SECRET]"
)
var (
TokenURL = "https://accounts.animeshon.com/o/oauth2/token"
Endpoint = "vision.animeapis.com:443"
)
func main() {
ctx := context.Background()
config := &clientcredentials.Config{
ClientID: ClientID,
ClientSecret: ClientSecret,
TokenURL: TokenURL,
}
options := []option.ClientOption{
option.WithEndpoint(Endpoint),
option.WithTokenSource(config.TokenSource(ctx)),
}
client, err := gapic.NewImageAnnotatorClient(ctx, options...)
if err != nil {
log.Fatalf("NewImageAnnotatorClient: %s", err)
}
request := &vision.CreateImageAnnotationRequest{
Parent: ImageAnnotationParent,
Annotation: &vision.ImageAnnotation{
Resource: Resource,
Annotations: &vision.ImageAnnotations{
// ---------------------------------------------------------------------
// Annotations related to text in the image.
// ---------------------------------------------------------------------
TextAnnotations: []*vision.TextAnnotation{
{
Property: &vision.TextAnnotation_TextProperty{
Languages: []*vision.TextAnnotation_Language{
{LanguageCode: "eng", Confidence: 0.9},
},
},
BoundingBox: &vision.BoundingPoly{
Vertices: []*vision.Vertex{
{X: 0, Y: 0},
{X: 0, Y: 10},
{X: 10, Y: 0},
{X: 10, Y: 10},
},
},
Text: "Test",
Confidence: 0.8,
},
},
// ---------------------------------------------------------------------
// Annotations related to labels such as actions, sentiment, and so on.
// ---------------------------------------------------------------------
LabelAnnotations: []*vision.LabelAnnotation{
{
Name: "nsfw",
Score: 0.8,
Topicality: 0.9,
},
},
// ---------------------------------------------------------------------
// Annotations related to entities that can be located in the image.
// ---------------------------------------------------------------------
EntityAnnotations: []*vision.EntityAnnotation{
{
Name: "human-eye",
Score: 0.9,
BoundingBox: &vision.BoundingPoly{
Vertices: []*vision.Vertex{
{X: 0, Y: 0},
{X: 0, Y: 10},
{X: 10, Y: 0},
{X: 10, Y: 10},
},
},
},
},
// ---------------------------------------------------------------------
// Annotations related to known animes, characters, and so on.
// ---------------------------------------------------------------------
KnowledgeGraphAnnotations: []*vision.KnowledgeGraphAnnotation{
{
Resource: "animes/12345",
Score: 0.9,
BoundingBox: nil,
},
},
// ---------------------------------------------------------------------
// Annotations related to known links associated to this image.
// ---------------------------------------------------------------------
WebSearchAnnotations: []*vision.WebSearchAnnotation{
{
Url: "https://www.pixiv.net/my-link",
Score: 1.0,
},
},
// ---------------------------------------------------------------------
// Annotations related to safe search parameters.
// ---------------------------------------------------------------------
SafeSearchAnnotation: &vision.SafeSearchAnnotation{
Adult: vision.Likelihood_POSSIBLE,
Racy: vision.Likelihood_POSSIBLE,
Violence: vision.Likelihood_POSSIBLE,
Medical: vision.Likelihood_POSSIBLE,
Juvenile: vision.Likelihood_POSSIBLE,
},
},
},
}
response, err := client.CreateImageAnnotation(ctx, request)
if err != nil {
log.Fatalf("CreateImageAnnotation: %s", err)
}
log.Printf("name : %s", response.GetName())
log.Printf("resource : %s", response.GetResource())
log.Printf("[annotations] TEXT : %+v", response.GetAnnotations().GetTextAnnotations())
log.Printf("[annotations] LABEL : %+v", response.GetAnnotations().GetLabelAnnotations())
log.Printf("[annotations] ENTITY : %+v", response.GetAnnotations().GetEntityAnnotations())
log.Printf("[annotations] KNOWLEDGE GRAPH: %+v", response.GetAnnotations().GetKnowledgeGraphAnnotations())
log.Printf("[annotations] WEB SEARCH : %+v", response.GetAnnotations().GetWebSearchAnnotations())
log.Printf("[annotations] SAFE SEARCH : %+v", response.GetAnnotations().GetSafeSearchAnnotation())
}