Welcome to DOFBOT SE repository
1.Gesture Recognition
Gesture Recognition
1. Gesture Recognition Overview
x1. This gesture recognition routine is based on Baidu Intelligent Cloud platform services, which offers 50,000 free uses per day for learning purposes only. Do not use for commercial purposes. If long-term use is needed, please purchase the relevant service. Our company is not responsible for any issues.2. API Application Instructions. Official API address: https://cloud.baidu.com/doc/BODY/s/ajwvxyyn0. Log in with your Baidu account after opening the official API address in a browser. If you don't have a Baidu account, please register first. Overseas users need to contact technical support.
- Supported gestures and example images:



- API Function. If recognition fails, apply for your own secret key and replace APP_ID, API_KEY, SECRET_KEY.
xxxxxxxxxxfrom aip import AipBodyAnalysis""" Your APPID AK SK """APP_ID = 'your App ID'API_KEY = 'your Api Key'SECRET_KEY = 'your Secret Key'client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)""" Read image """def get_file_content(filePath):with open(filePath, 'rb') as fp:return fp.read()image = get_file_content('example.jpg')""" Call gesture recognition """Res = client.gesture(image);Gesture Recognition Return Example:
xxxxxxxxxx{"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}}
2. Main Code
Code path: /home/yahboom/Dofbot/6.AI_Visual/4.Gesture recognition.ipynb
Import Baidu API, important: change the secret key to your own.
xxxxxxxxxximport cv2 import timeimport demjsonimport pygame from aip import AipBodyAnalysisfrom aip import AipSpeechfrom PIL import Image, ImageDraw, ImageFontimport numpyimport ipywidgets.widgets as widgets# For specific gestures, please refer to official information https://ai.baidu.com/ai-doc/BODY/4k3cpywrv#Please refer to the official information for specific gestures https://ai.baidu.com/ai-doc/BODY/4k3cpywrvhand={'One':'Number 1','Two':'Number 2','Three':'Number 3','Four':'Number 4', 'Five':'Number 5', 'Six':'Number 6','Seven':'Number 7', 'Eight':'Number 8','Nine':'Number 9','Fist':'Fist','Ok':'OK', 'Prayer':'Prayer','Congratulation':'Congregation','Honour':'Farewell', 'Heart_single':'Heart','Thumb_up':'Like','Thumb_down':'Dislike', 'ILY':'I Love You','Palm_up':'Palm Up','Heart_1':'Double Heart 1', 'Heart_2':'Double Heart 2','Heart_3':'Double Heart 3','Rock':'Rock', 'Insult':'Middle Finger','Face':'Face'}# The key below needs to be replaced with one's own #The key below needs to be replaced with one's own""" Body Analysis APPID AK SK """APP_ID = '31069241'API_KEY = 'pxVueLwdAGX4dafYeLsLdZa1'SECRET_KEY = 'VsDmfGRlWGqzGhcWowoCT5km4TG4Gylq'client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)g_camera = cv2.VideoCapture(0)g_camera.set(3, 640)g_camera.set(4, 480)g_camera.set(5, 30) #Set frame rateg_camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))g_camera.set(cv2.CAP_PROP_BRIGHTNESS, 40) #Set brightness -64 - 64 0.0g_camera.set(cv2.CAP_PROP_CONTRAST, 50) #Set contrast -64 - 64 2.0g_camera.set(cv2.CAP_PROP_EXPOSURE, 156) #Set exposure 1.0 - 5000 156.0ret, frame = g_camera.read()Camera display component
xxxxxxxxxximage_widget = widgets.Image(format='jpeg', width=600, height=500) #Set camera display componentdisplay(image_widget) image_widget.value = bgr8_to_jpeg(frame)Main display and result program.
xxxxxxxxxxtry: while True: """1. Capture """ ret, frame = g_camera.read() #image = get_file_content('./image.jpg') """ 2. Call gesture recognition """ raw = str(client.gesture(image_widget.value)) text = demjson.decode(raw) try: res = text['result'][0]['classname'] except: # print('Recognition result: Nothing detected~' ) # img = cv2ImgAddText(frame, "Not recognized", 250, 30, (0, 0 , 255), 30) img = frame else: # print('Recognition result: ' + hand[res]) # img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) if res == 'Prayer': # 1 Prayer print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == 'Thumb_up':# 2 Like print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == 'Ok': # 3 OK print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == 'Heart_single': # 4 Single hand heart print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == 'Five': # 5 Number 5 print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == "Eight": # Number 8 print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == "Rock": # rock print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == "Congratulation": # Congregation print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == "Seven": # Number 7 print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) elif res == "Thumb_down": # Thumb down print('Recognition result: ' + hand[res]) img = cv2ImgAddText(frame, hand[res], 250, 30, (0, 255 , 0), 30) else: img = frame image_widget.value = bgr8_to_jpeg(img)except KeyboardInterrupt: print(" Program closed! ") passAfter running the code blocks, you can see the recognized gestures.