将face API与Python和本地图像文件一起使用 [英] Using face API with Python and local image files

查看:98
本文介绍了将face API与Python和本地图像文件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用API​​进行面部检测,使用本地图像文件。我已经遵循API文档中的Python示例代码,但仍然获取InvalidImageSize响应。这似乎发生在任何图像上。通过网络演示,相同的图片可以获得
罚款。我认为问题在于没有实际发送数据 - 示例代码向您展示如何初始化身份验证和请求参数以及如何将内容类型设置为application / octet-stream,而不是如何设置请求
身体到图像文件。有没有人设法让这个工作?最小的工作示例会很棒。


谢谢!

解决方案

< span lang ="EN-US">请尝试以下代码。没有什么特别的东西。


params = urllib.urlencode( {


   'subscription-key': " ****",


   'analyzeFaceLandmarks':'true ',


   'analyzeAge':'true',


   'analyzeGender':'true',


   'analyzeHeadPose':'true',


}}  


< p style ="">     #specify来自档案的图片


    #对于本地图像,Content-Type应为application / octet-stream或multipart / form-data且JSON应该是空的


&NBSP;&NBSP;&NBSP; headers = {


        'Content-type':'application / octet-stream',


    }


    body =""  


    #load image


    filename ='C:/testPicutre.JPG'


    f =打开(文件名,"rb")


    body = f.read()


    f.close()


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


    conn.request(" POST"," / face / v0 / detections?%s"%params,body,headers)


&NBSP;&NBSP;&NBSP; response = conn.getresponse("")


    data = response.read()


   打印(数据)


    conn.close()


Hi,

I'm trying to use the API for face detection from Python, with a local image file. I've followed the Python example code in the API documentation but keeping getting the InvalidImageSize response. This seems to happen with any image. The same images work fine with the web demo. I think the problem is that no data is actually being sent - the example code shows you how to initialise the authentication and request parameters and to set the content type to application/octet-stream, but not how to set the request body to an image file. Has anyone managed to get this working? A minimal working example would be great.

Thanks!

解决方案

Please try below code. There is no special things.

params = urllib.urlencode({

    'subscription-key': "****",

    'analyzesFaceLandmarks': 'true',

    'analyzesAge': 'true',

    'analyzesGender': 'true',

    'analyzesHeadPose': 'true',

}) 

    #specify image from file

    #For a local image, Content-Type should be application/octet-stream or multipart/form-data AND JSON SHOULD BE EMPTY

    headers = {

        'Content-type': 'application/octet-stream',

    }

    body = "" 

    #load image

    filename = 'C:/testPicutre.JPG'

    f = open(filename, "rb")

    body = f.read()

    f.close()

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

    conn.request("POST", "/face/v0/detections?%s" % params, body, headers)

    response = conn.getresponse("")

    data = response.read()

    print(data)

    conn.close()


这篇关于将face API与Python和本地图像文件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆