0

I am trying to use Face API Python 2.7. I wanted to make gender recognition by photo but always getting the error. This is my code:

from six.moves import urllib
import httplib
import json
params = urllib.urlencode({

    'returnFaceId': 'true',
    'returnFaceAttributes': 'true',
    'returnFaceAttributes': '{string}',
})
 headers = {
    'ocp-apim-subscription-key': "ee6b8785e7504dfe91efb96d37fc7f51",
    'content-type': "application/octet-stream"
    }
img = open("d:/Taylor.jpg", "rb")

conn = httplib.HTTPSConnection("api.projectoxford.ai") 

conn.request("POST",  "/vision/v1.0/tag?%s" % params, img, headers)

res = conn.getresponse()
data = res.read()
conn.close()

I've got this error :

  Traceback (most recent call last):

  File "<ipython-input-314-df31294bc16f>", line 3, in <module>
    res = conn.getresponse()

  File "d:\Anaconda2\lib\httplib.py", line 1136, in getresponse
    response.begin()

  File "d:\Anaconda2\lib\httplib.py", line 453, in begin
    version, status, reason = self._read_status()

  File "d:\Anaconda2\lib\httplib.py", line 409, in _read_status
    line = self.fp.readline(_MAXLINE + 1)

  File "d:\Anaconda2\lib\socket.py", line 480, in readline
    data = self._sock.recv(self._rbufsize)

  File "d:\Anaconda2\lib\ssl.py", line 756, in recv
    return self.read(buflen)

  File "d:\Anaconda2\lib\ssl.py", line 643, in read
    v = self._sslobj.read(len)

error: [Errno 10054] 

If I use link instead photo:

img_url='https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'

conn = httplib.HTTPSConnection("api.projectoxford.ai")
conn.request("POST",  "/vision/v1.0/tag?%s" % params, img_url, headers)
res = conn.getresponse()
data = res.read()
conn.close()

I get:

 data
Out[330]: '{ "statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription." }'

If I use:

KEY = 'ee6b8785e7504dfe91efb96d37fc7f51'  
CF.Key.set(KEY)

img_url = 'https://raw.githubusercontent.com/Microsoft/Cognitive-Face-Windows/master/Data/detection1.jpg'
result = CF.face.detect(img_url)

all works fine:

result
[{u'faceId': u'52c0d1ac-f041-49cd-a587-e81ef67be2fb',
  u'faceRectangle': {u'height': 213,
   u'left': 154,
   u'top': 207,
   u'width': 213}}]

But in this case I don't know how to use method returnFaceAttribute (for gender detection) and also if I use img in result = CF.face.detect(img_url) instead img_url I get an error: status_code: 400

response: {"error":{"code":"InvalidImageSize","message":"Image size is too small or too big."}}
Traceback (most recent call last):

  File "<ipython-input-332-3fe2623ccadc>", line 1, in <module>
    result = CF.face.detect(img)

  File "d:\Anaconda2\lib\site-packages\cognitive_face\face.py", line 41, in detect
    data=data)

  File "d:\Anaconda2\lib\site-packages\cognitive_face\util.py", line 84, in request
    error_msg.get('message'))

CognitiveFaceException: Image size is too small or too big. 

This happends with all sorts of img sizes.

Could anyone explain how to solve these problems?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
egorkh
  • 478
  • 8
  • 24

2 Answers2

0

It seems like you need to use your API key when connecting to Face API. That's why your second example works and the others don't.

Code 401 means that you're unauthorised, i.e. at that point you didn't log in with your key.

Maybe it'd be easier for you to try and use requests instead of urllib.

Dawid Laszuk
  • 1,773
  • 21
  • 39
0

The first and error is caused by sending file object instead of file content. You should send image.read() instead of image.

The second error is caused by lacking of subscription key in the request header.

For the last error, I suspect you may have read the image file object once and thus reading it again will return empty result. You can try to send file path instead and it should work with the helper in Face API Python SDK.

Xuan Hu
  • 856
  • 8
  • 22