智能家居手勢識別,只需百度AI即可搞定

上次我嘗試做了一個給眼鏡加特效,針對的是靜態(tài)圖像,具體文章參考 https://ai.baidu.com/forum/topic/show/942890 。

這次我嘗試在視頻中加眼鏡特效,并且加上手勢識別,不同的手勢佩戴不同的眼鏡。接下來將介紹手勢識別接口,并介紹如何接入。

手勢識別接口

接口描述

識別圖片中的手勢類型,返回手勢名稱、手勢矩形框、概率分數(shù),可識別24種常見手勢,適用于手勢特效、智能家居手勢交互等場景。

支持的24類手勢列表:拳頭、OK、祈禱、作揖、作別、單手比心、點贊、Diss、我愛你、掌心向上、雙手比心(3種)、數(shù)字(9種)、Rock、豎中指。

注:

•上述24類以外的其他手勢會劃分到other類。

•除識別手勢外,若圖像中檢測到人臉,會同時返回人臉框位置。

人體分析的請求方式和人臉識別的請求方式有所不同,具體的使用說明參見文檔 https://ai.baidu.com/docs#/Body-API/27495b11

請求格式

POST 方式調(diào)用,請求 URL 為 https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture ,Content-Type 為 application/x-www-form-urlencoded,然后通過 urlencode 格式化請求體。

請求參數(shù)


智能家居手勢識別,只需百度AI即可搞定

返回說明

智能家居手勢識別,只需百度AI即可搞定

返回示例


{"log_id":4466502370458351471,"result_num":2,"result":[{"probability":0.9844077229499817,"top":20,"height":156,"classname":"Face","width":116,"left":173},{"probability":0.4679304957389832,"top":157,"height":106,"classname":"Heart_2","width":177,"left":183}]}

實例

1. 創(chuàng)建應用

由于戴眼鏡是使用的人臉識別的接口,手勢識別是人體分析的接口,因此為了將手勢識別應用到戴眼鏡特效中,需要在創(chuàng)建人臉識別應用時勾選人體分析的手勢識別。

首先進入“控制臺”的“人臉識別”,然后“創(chuàng)建應用”。

智能家居手勢識別,只需百度AI即可搞定

然后填上“應用名稱”和“應用描述”,并且接口勾選上“人體分析”下的“手勢識別”。

智能家居手勢識別,只需百度AI即可搞定

之后點擊“立即創(chuàng)建”,創(chuàng)建好之后我們就能夠獲取到應用的 “API key” 和 “Secret key”,用于后面獲取 “token key”。

智能家居手勢識別,只需百度AI即可搞定

2.獲取 token key

通過 API Key 和 Secret Key 獲取的 access_token。更多關于 access_token 的獲取方法參考 http://ai.baidu.com/docs#/Auth/top。

下面代碼是 python3 獲取 access_token 的代碼

defget_token_key():#client_id為官網(wǎng)獲取的AK,client_secret為官網(wǎng)獲取的SKclient_id='【百度云應用的AK】'#APIkeyclient_secret='【百度云應用的SK】'#Secretkeyurl=f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials'\f'&client_id={client_id}&client_secret={client_secret}'headers={'Content-Type':'application/json;charset=UTF-8'}res=requests.post(url,headers=headers)token_content=res.json()assert"error"notintoken_content,f"{token_content['error_description']}"token_key=token_content['access_token']returntoken_key3.調(diào)用手勢識別接口調(diào)用手勢識別接口的python3代碼實現(xiàn)如下:defget_hand_info(image_base64,token_key):request_url="https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture"params_d=dict()params_d['image']=str(image_base64,encoding='utf-8')access_token=token_keyrequest_url=request_url+"?access_token="+access_tokenres=requests.post(url=request_url,data=params_d,headers={'Content-Type':'application/x-www-form-urlencoded'})data=res.json()assert'error_code'notindata,f'Error:{data["error_msg"]}'returndata正確調(diào)用接口獲取到數(shù)據(jù)之后,我們可以得到一些想要的信息。例如:獲取檢測的類別的數(shù)量、各個類別的類別名以及邊框。defget_hand_num(data):returndata['result_num']defget_hand_cls_and_bbox(data):result=list()cls_list=list()hand_num=get_hand_num(data)foriinrange(hand_num):res_dict=data['result'][i]cls=res_dict['classname']cls_list.append(cls)bbox=[res_dict['left'],res_dict['top'],res_dict['width'],res_dict['height']]res=[cls]+bboxresult.append(res)returnresult,cls_list

案例代碼與說明

整個案例的核心代碼如下:(由于人臉識別的 QPS 為 2,因此在顯示圖像時使用了 cv2.waitKey(500),所以這個應用看起來不是很流暢)

importcv2fromutilimportpic_base64,get_face_info,get_face_location,get_face_num,frame2base64,get_hand_infofrompprintimportpprintimportutilimportface_utilimportgesture_utilimportosimportrandomtoken_key='【獲取的tokenkey】'glasses_img=['images/glasses/'+imgforimginos.listdir('images/glasses')]glasses=cv2.imread('images/glasses/glasses6.png',cv2.IMREAD_UNCHANGED)cap=cv2.VideoCapture(0)whileTrue:_,image=cap.read()detect_img=image.copy()gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)image_base64=frame2base64(image)face_data=get_face_info(image_base64,token_key)hand_data=get_hand_info(image_base64,token_key)_,cls_list=util.get_hand_cls_and_bbox(hand_data)ifface_data:location=get_face_location(face_data)face_num=util.get_face_num(face_data)landmark4=util.get_landmark4(face_data)ifutil.compare_hand(cls_list,'Heart_single'):detect_img=gesture_util.draw_heart_single(detect_img)ifutil.compare_hand(cls_list,'Ok'):detect_img=gesture_util.draw_firework(detect_img)ifutil.compare_hand(cls_list,'One'):glasses=cv2.imread(glasses_img[1],cv2.IMREAD_UNCHANGED)detect_img=gesture_util.draw_one(detect_img)ifutil.compare_hand(cls_list,'Two'):glasses=cv2.imread(glasses_img[2],cv2.IMREAD_UNCHANGED)detect_img=gesture_util.draw_two(detect_img)ifutil.compare_hand(cls_list,'Three'):glasses=cv2.imread(glasses_img[3],cv2.IMREAD_UNCHANGED)detect_img=gesture_util.draw_three(detect_img)ifutil.compare_hand(cls_list,'Four'):glasses=cv2.imread(glasses_img[4],cv2.IMREAD_UNCHANGED)detect_img=gesture_util.draw_four(detect_img)ifutil.compare_hand(cls_list,'Five'):glasses=cv2.imread(glasses_img[5],cv2.IMREAD_UNCHANGED)detect_img=gesture_util.draw_five(detect_img)ifutil.compare_hand(cls_list,'Fist'):glasses=cv2.imread(glasses_img[random.randint(0,len(glasses_img)-1)],cv2.IMREAD_UNCHANGED)ifutil.compare_hand(cls_list,'ILY'):detect_img=gesture_util.draw_love(detect_img)detect_img=face_util.wear_glasses(detect_img,glasses,face_num,landmark4)detect_img=cv2.flip(detect_img,1)else:detect_img=cv2.flip(detect_img,1)#fori,clsinenumerate(cls_list):#ifcls!='Face':#cv2.putText(detect_img,cls,(50,50+100*i),cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,255,255),2)cv2.imshow('pic',detect_img)key=cv2.waitKey(500)&0xFFifkey==ord('q'):breakcap.release()cv2.destroyAllWindows()

該代碼主要識別數(shù)字1-5、比心、OK、單手我愛你和拳頭手勢,數(shù)字1-5對應不同類型的眼鏡,拳頭代表隨機更換眼鏡,比心會在界面上畫出心?,OK會在界面上展示一些煙花,單手我愛你展示愛你的表情。

下面是一些截圖展示:

one:

智能家居手勢識別,只需百度AI即可搞定

two:

智能家居手勢識別,只需百度AI即可搞定

three:

智能家居手勢識別,只需百度AI即可搞定

four:

智能家居手勢識別,只需百度AI即可搞定

five:

智能家居手勢識別,只需百度AI即可搞定

OK:

智能家居手勢識別,只需百度AI即可搞定

比心:

智能家居手勢識別,只需百度AI即可搞定

我愛你:

智能家居手勢識別,只需百度AI即可搞定

項目代碼地址: https://github.com/busyboxs/baiduAIFace ,修改好自己的 API key 和 Secret Key 之后直接執(zhí)行 camera_face 即可。(作者:busyboxs

極客網(wǎng)企業(yè)會員

免責聲明:本網(wǎng)站內(nèi)容主要來自原創(chuàng)、合作伙伴供稿和第三方自媒體作者投稿,凡在本網(wǎng)站出現(xiàn)的信息,均僅供參考。本網(wǎng)站將盡力確保所提供信息的準確性及可靠性,但不保證有關資料的準確性及可靠性,讀者在使用前請進一步核實,并對任何自主決定的行為負責。本網(wǎng)站對有關資料所引致的錯誤、不確或遺漏,概不負任何法律責任。任何單位或個人認為本網(wǎng)站中的網(wǎng)頁或鏈接內(nèi)容可能涉嫌侵犯其知識產(chǎn)權或存在不實內(nèi)容時,應及時向本網(wǎng)站提出書面權利通知或不實情況說明,并提供身份證明、權屬證明及詳細侵權或不實情況證明。本網(wǎng)站在收到上述法律文件后,將會依法盡快聯(lián)系相關文章源頭核實,溝通刪除相關內(nèi)容或斷開相關鏈接。

2019-07-16
智能家居手勢識別,只需百度AI即可搞定
上次我嘗試做了一個給眼鏡加特效,針對的是靜態(tài)圖像,具體文章參考 https://ai.baidu.com/forum/topic/show/942890 。

長按掃碼 閱讀全文