locked
Emotions API with Python RRS feed

  • Question

  • I try to launch the Emotion API with Python but with no success - I get "statusCode": 404.

    How should I parse Request body where the Url of the picture is to conn.request? Substitution "{body}" with the URL apparently does not work.

    Thanks in advance

    Sunday, November 15, 2015 9:15 AM

Answers

  • here's solution that works with Python 2.7


    import httplib, urllib, base64, json

    body = {"URL": "http://www.scientificamerican.com/sciam/cache/file/35391452-5457-431A-A75B859471FAB0B3.jpg" }

    headers = {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '_key_here_',
    }

    try:
        conn = httplib.HTTPSConnection('api.projectoxford.ai')
        conn.request("POST", "/emotion/v1.0/recognize", json.dumps(body) , headers)
        response = conn.getresponse()#
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

    • Marked as answer by MartiGr Wednesday, November 18, 2015 2:19 PM
    Wednesday, November 18, 2015 10:35 AM

All replies

  • Ok, here's Python 2.7 with the following code which yields "statusCode": 404, "message": "Resource not found" What's wrong?

    import httplib, urllib, base64, urllib2



    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '{valid_key_here}',
    }


    params = urllib.urlencode({
        # Request parameters
         "faceRectangle": {
          "left": 68,
          "top": 97,
          "width": 64,
          "height": 97
        },
    })

    try:
        conn = httplib.HTTPSConnection('api.projectoxford.ai')
        conn.request("POST", "/emotion/v1.0/recognize&%s" % params, '"url": "http://www.scientificamerican.com/sciam/cache/file/35391452-5457-431A-A75B859471FAB0B3.jpg"' , headers)
        response = conn.getresponse()
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

    Tuesday, November 17, 2015 1:07 PM
  • I think I have the same issue, when executing the JavaScript example. The console reads:


    OPTIONS https://api.projectoxford.ai/emotion/v1.0/recognize 404 (Resource Not Found)
    XMLHttpRequest cannot load https://api.projectoxford.ai/emotion/v1.0/recognize. 
    Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' 
    header is present on the requested resource. Origin 'http://localhost:7357' is therefore not allowed access. 
    The response had HTTP status code 404.

    Thanks,

    Jim.

    Tuesday, November 17, 2015 8:57 PM
  • here's solution that works with Python 2.7


    import httplib, urllib, base64, json

    body = {"URL": "http://www.scientificamerican.com/sciam/cache/file/35391452-5457-431A-A75B859471FAB0B3.jpg" }

    headers = {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '_key_here_',
    }

    try:
        conn = httplib.HTTPSConnection('api.projectoxford.ai')
        conn.request("POST", "/emotion/v1.0/recognize", json.dumps(body) , headers)
        response = conn.getresponse()#
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

    • Marked as answer by MartiGr Wednesday, November 18, 2015 2:19 PM
    Wednesday, November 18, 2015 10:35 AM