Created
May 19, 2016 08:43
-
-
Save IndianGuru/1be0d2319590ee7506094a507715d815 to your computer and use it in GitHub Desktop.
To access Microsoft's Face API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
) | |
type FaceResp []struct { | |
FaceID string `json:"faceId"` | |
FaceRectangle struct { | |
Top int `json:"top"` | |
Left int `json:"left"` | |
Width int `json:"width"` | |
Height int `json:"height"` | |
} `json:"faceRectangle"` | |
FaceAttributes struct { | |
Gender string `json:"gender"` | |
Age float64 `json:"age"` | |
Glasses string `json:"glasses"` | |
} `json:"faceAttributes"` | |
} | |
func main() { | |
var jsonStr = []byte(`{"url":"https://pbs.twimg.com/profile_images/717290681232977920/Jl5EKtIn.jpg"}`) | |
url := fmt.Sprintf("https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,glasses") | |
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | |
if err != nil { | |
log.Fatal("NewRequest: ", err) | |
return | |
} | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Ocp-Apim-Subscription-Key", "Your Key") | |
httpClient := &http.Client{} | |
resp, err := httpClient.Do(req) | |
if err != nil { | |
log.Fatal("Do: ", err) | |
return | |
} | |
defer resp.Body.Close() | |
var record FaceResp | |
if err := json.NewDecoder(resp.Body).Decode(&record); err != nil { | |
log.Fatal("Decode: ", err) | |
return | |
} | |
fmt.Printf("Your gender is: %v\n", record[0].FaceAttributes.Gender) | |
fmt.Printf("and age is: %v years.\n", record[0].FaceAttributes.Age) | |
fmt.Printf("Glasses or No Glasses? %v", record[0].FaceAttributes.Glasses) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment