Exemplo n.º 1
0
// start to detect a photo
func (self FaceDetection) Do() (detectResult DetectResult, err error) {
	apiKey := oxford.Config.OcpApimSubscriptionKey
	if len(self.OcpApimSubscriptionKey) > 0 {
		apiKey = self.OcpApimSubscriptionKey
	}
	var resp *http.Response
	if len(self.URL) == 0 {
		if nil == self.Reader {
			err = errors.New("no content or url to detect")
			return
		}

		resp, err = postFile(oxford.Config.DetectionsURL, self.Reader, apiKey)

	} else {
		resp, err = postURL(oxford.Config.DetectionsURL, self.URL, apiKey)
	}

	if err != nil {
		return
	}

	respBody, err := util.HandleResponse(resp)
	if err != nil {
		return
	}

	defer resp.Body.Close()

	detectResult.StatusCode = resp.StatusCode

	if resp.StatusCode != 200 {
		var bodyMap map[string]interface{}
		if err = json.Unmarshal(respBody, &bodyMap); err != nil {
			return
		}

		detectResult.Message = util.Convert2String(bodyMap["message"])
		detectResult.Code = util.Convert2String(bodyMap["message"])
		return
	}

	var faceResult []Face
	if err = json.Unmarshal(respBody, &faceResult); err != nil {
		return
	}

	detectResult.Success = true
	detectResult.Message = "success"
	detectResult.Code = CodeOK
	detectResult.FaceResults = faceResult

	return
}
Exemplo n.º 2
0
func (self FaceVerification) Do() (result VerificationResult, err error) {
	apiKey := oxford.Config.OcpApimSubscriptionKey
	if len(self.OcpApimSubscriptionKey) > 0 {
		apiKey = self.OcpApimSubscriptionKey
	}

	req, err := http.NewRequest("POST", VerificationRUL, strings.NewReader(fmt.Sprintf(`{"faceId1":"%v", "faceId2":"%v"}`, self.FaceIdOne, self.FaceIdTwo)))
	if err != nil {
		return
	}

	req.Header.Set("Content-Type", oxford.ContentTypeJson)
	req.Header.Set("Ocp-Apim-Subscription-Key", apiKey)

	var resp *http.Response
	client := &http.Client{}
	resp, err = client.Do(req)

	respBody, err := util.HandleResponse(resp)
	if err != nil {
		return
	}

	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		var bodyMap map[string]interface{}
		if err = json.Unmarshal(respBody, &bodyMap); err != nil {
			return
		}

		result.Message = util.Convert2String(bodyMap["message"])
		result.Code = util.Convert2String(bodyMap["message"])
		return
	}

	if err = json.Unmarshal(respBody, &result); err != nil {
		return
	}

	result.Success = true
	result.Message = "success"
	result.Code = CodeOK

	return
}