百度AI攻略:手部关键点识别

1.功能描述:python

对于输入的一张图片(可正常解码,且长宽比适宜),检测图片中的全部人手,输出每只手的坐标框、21个骨节点坐标信息。json

2.平台接入app

具体接入方式比较简单,能够参考个人另外一个帖子,这里就不重复了:测试

http://ai.baidu.com/forum/topic/show/943327编码

3.调用攻略(Python3)及评测url

3.1首先认证受权:rest

在开始调用任何API以前须要先进行认证受权,具体的说明请参考:code

http://ai.baidu.com/docs#/Auth/toporm

具体Python3代码以下:blog

# -*- coding: utf-8 -*-

#!/usr/bin/env python

import urllib

import base64

import json

#client_id 为官网获取的AK, client_secret 为官网获取的SK

client_id =【百度云应用的AK】

client_secret =【百度云应用的SK】

#获取token

def get_token():

host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret

request = urllib.request.Request(host)

request.add_header('Content-Type', 'application/json; charset=UTF-8')

response = urllib.request.urlopen(request)

token_content = response.read()

if token_content:

token_info = json.loads(token_content)

token_key = token_info['access_token']

return token_key

3.2手部关键点识别分析接口调用:

详细说明请参考: https://ai.baidu.com/docs#/Body-API/2757b503

说明的比较清晰,这里就不重复了。

你们须要注意的是:

API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/hand_analysis

图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M。图片的base64编码是不包含图片头的,如(data:image/jpg;base64,),支持图片格式:jpg、bmp、png,最短边至少50px,最长边最大4096px

Python3调用代码以下:

#画出手部识别结果

def draw_hands_point(originfilename,hands,resultfilename,pointsize,pointcolor):

    from PIL import Image, ImageDraw

    image_origin = Image.open(originfilename)

    draw =ImageDraw.Draw(image_origin)

 

    for hand in hands:

 

        for hand_part in hand['hand_parts'].values():

            #print(hand_part)

            draw.ellipse((hand_part['x']-pointsize,hand_part['y']-pointsize,hand_part['x']+pointsize,hand_part['y']+pointsize),fill = pointcolor)

        gesture = hand['location']

        draw.rectangle((gesture['left'],gesture['top'],gesture['left']+gesture['width'],gesture['top']+gesture['height']),outline = "red")

 

 

    image_origin.save(resultfilename, "JPEG")

#手部识别

#filename:原图片名(本地存储包括路径)

def hand_analysis(filename,resultfilename,size,color,pointsize,pointcolor):

    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/hand_analysis"

    print(filename)

    # 二进制方式打开图片文件

    f = open(filename, 'rb')

    img = base64.b64encode(f.read())

 

    params = dict()

    params['image'] = img

    params = urllib.parse.urlencode(params).encode("utf-8")

    #params = json.dumps(params).encode('utf-8')

 

    access_token = get_token()

    begin = time.perf_counter()

    request_url = request_url + "?access_token=" + access_token

    request = urllib.request.Request(url=request_url, data=params)

    request.add_header('Content-Type', 'application/x-www-form-urlencoded')

    response = urllib.request.urlopen(request)

    content = response.read()

    end = time.perf_counter()

    print('处理时长:'+'%.2f'%(end-begin)+'秒')

    if content:

        #print(content)

        content=content.decode('utf-8')

        #print(content)

        data = json.loads(content)

        print('hand_num:',data['hand_num'])

        #print(data)

        result=data['hand_info']

 

        draw_hands_point(filename,result,resultfilename,pointsize,pointcolor)

 

4.功能评测:

选用不一样的数据对效果进行测试,具体效果以下(如下例子均来自网上):

处理时长:0.44秒

hand_num: 1

处理时长:0.67秒

hand_num: 1

处理时长:0.56秒

hand_num: 1

处理时长:0.86秒

hand_num: 1

能够发现对于单手的状况,速度很快,效果很准确。

 

处理时长:0.61秒

hand_num: 3

 

5.测试结论和建议

测试下来,总体识别效果不错。对于手部关键点有较强的识别能力,效果很好,速度也很快。

不过对于比较复杂的图片,如多个手或者背景比较复杂的状况,识别率还有提升的空间,但愿后续进一步提升。

建议:

1,能够考虑增长对手势的一些识别,好比握拳,张手等。

2,能够考虑与手势识别的功能进行结合,让客户经过选项选择要返回内内容。

相关文章
相关标签/搜索