1.功能描述:python
對於輸入的一張圖片(可正常解碼,且長寬比適宜),檢測圖片中的全部人手,輸出每隻手的座標框、21個骨節點座標信息。json
2.平臺接入app
具體接入方式比較簡單,能夠參考個人另外一個帖子,這裏就不重複了:
http://ai.baidu.com/forum/topic/show/943327測試
3.調用攻略(Python3)及評測編碼
3.1首先認證受權:url
在開始調用任何API以前須要先進行認證受權,具體的說明請參考:rest
http://ai.baidu.com/docs#/Auth/topcode
具體Python3代碼以下:orm
# -*- coding: utf-8 -*-
#!/usr/bin/env pythontoken
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.測試結論和建議
測試下來,總體識別效果不錯。對於手部關鍵點有較強的識別能力,效果很好,速度也很快。
不過對於比較複雜的圖片,如多個手或者背景比較複雜的狀況,識別率還有提升的空間,但願後續進一步提升。
做者:才能我浪費99