Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while obtaining labels #7

Open
lukaszraczylo opened this issue Jan 10, 2019 · 1 comment
Open

Issue while obtaining labels #7

lukaszraczylo opened this issue Jan 10, 2019 · 1 comment

Comments

@lukaszraczylo
Copy link

lukaszraczylo commented Jan 10, 2019

Parts of code responsible for image analysis:

batch, _ := client.NewBatchAnnotateImageRequest([]string{imageURL}, pigeon.NewFeature(pigeon.LabelDetection), pigeon.NewFeature(pigeon.SafeSearchDetection))

Getting annotations for both labels and safe search at the same time.

res, err := client.ImagesService().Annotate(batch).Do()
b, _ := json.Marshal(res.Responses)
json.Unmarshal(b, &GoogleVision)

Parsing the output into struct created automagically from the sample json ( as follows )

JSON:

{"labelAnnotations":[{"description":"anime","mid":"/m/0jxy","score":0.81207085,"topicality":0.81207085},{"description":"art","mid":"/m/0jjw","score":0.59014136,"topicality":0.59014136},{"description":"action figure","mid":"/m/017h86","score":0.5556467,"topicality":0.5556467},{"description":"graphics","mid":"/m/021sdg","score":0.53742254,"topicality":0.53742254},{"description":"video game software","mid":"/m/0987_ns","score":0.52508384,"topicality":0.52508384},{"description":"fiction","mid":"/m/02xlf","score":0.51081264,"topicality":0.51081264},{"description":"screenshot","mid":"/m/01zbnw","score":0.50924677,"topicality":0.50924677},{"description":"recreation","mid":"/m/06bm2","score":0.50229543,"topicality":0.50229543}],"safeSearchAnnotation":{"adult":"VERY_UNLIKELY","medical":"VERY_UNLIKELY","racy":"POSSIBLE","spoof":"VERY_UNLIKELY","violence":"UNLIKELY"}}

STRUCT:


type AutoGenerated struct { 	LabelAnnotations []struct { 		Description string  `json:"description"` 		Mid         string  `json:"mid"` 		Score       float64 `json:"score"` 		Topicality  float64 `json:"topicality"` 	} `json:"labelAnnotations"` 	SafeSearchAnnotation struct { 		Adult    string `json:"adult"` 		Medical  string `json:"medical"` 		Racy     string `json:"racy"` 		Spoof    string `json:"spoof"` 		Violence string `json:"violence"` 	} `json:"safeSearchAnnotation"` }
--

Unfortunately:
GoogleVision.SafeSearchAnnotation.Adult contains correct value
but

for _, label := range GoogleVision.LabelAnnotations {
		Log.Info("Label detected", label) }

is always empty.
Any ideas what's missing?

@kaneshin
Copy link
Owner

kaneshin commented Jan 1, 2020

Output JSON is always inside of an array. You need to parse like the below;

// The AutoGenerated is defined by you from comment.
type AutoGenerated struct {
  LabelAnnotations []struct {
    // ...
  } `json:"labelAnnotations"`
  safeSearchAnnotation struct {
    // ...
  } `json:"safeSearchAnnotation"`
}

// You need to redefine like this for json.Unmarshal
type Parsable []AutoGenerated

Sorry for the late response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants