def gesture_recognition(): gesture_client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) frame = telloGetFrame(Drone) cv2.imshow('frame', frame) cv2.imwrite("{}.png".format(img_count), frame) image = get_file_content("{}.png".format(img_count)) gesture = gesture_client.gesture(image) #AipBodyAnalysis内部函数 print(gesture)
def pose_codelab(content, pose): #百度API通行证(获取方法见https://ai.baidu.com) APP_ID = '21219492' API_KEY = 'jC6Z4Fa9jTs6wtpUTkoyGVMV' SECRET_KEY = 'pbGBVFu41ZZRWHhtPEqUbGTaEekaKfqc' client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) #输入ID和密钥建立连接 video_capture = cv2.VideoCapture(0) while True: ret, frame = video_capture.read() #读取摄像头数据 #处理图片格式并发送给云端API frame_b = cv2.imencode(".jpg", frame)[1].tobytes() gesture_info = client.gesture(frame_b) #处理识别结果(API会将识别到的人脸和手势全部返回) if gesture_info['result']: result = gesture_info['result'] num = gesture_info['result_num'] for i in range(num): classname = result[i]['classname'] #识别的手势/人脸类别 #识别框坐标 left = result[i]['left'] top = result[i]['top'] width = result[i]['width'] height = result[i]['height'] #绘制识别框 cv2.rectangle(frame, (left, top), (left + width, top + height), (0, 0, 255), 2) if (classname == 'Face'): cv2.putText(frame, 'Face', (left, top + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1) else: cv2.putText(frame, '{}'.format(classname), (left, top + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 0), 1) pose.send_message_to_scratch(classname) #发送识别结果到scratch cv2.imshow('Video', frame) time.sleep(0.15) #防止QPS溢出 # Hit 'q' on the keyboard to quit! if cv2.waitKey(100) & 0xFF == ord('q'): break # time.sleep(5) # # Release handle to the webcam video_capture.release() cv2.destroyAllWindows()
def index(fileName): APP_ID = '' API_KEY = '' SECRET_KEY = '' client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) image = get_file_content(fileName) """ 调用手势识别 """ res = client.gesture(image) if 'error_code' in res: print(res['error_msg']) try: classname = res['result'][0]['classname'] print(classname) return classname except Exception as err: print('手势识别失败')
def parseimage(self, image): filename = 'images/gesture.png' cv2.imwrite(filename, image) myimage = self.get_file_content(filename) client = AipBodyAnalysis(self.APP_ID, self.API_KEY, self.SECRET_KEY) result = client.gesture(myimage) print(result) if result["result_num"] > 0: data = result['result'] print(data[0]["classname"]) if data[0]['classname'] == ('Heart_single' or 'Heart_1' or 'Heart_2' or 'Heart_3'): self.robotpi_movement.move_left() if data[0]['classname'] == 'Fist': self.robotpi_movement.wave_hands() if data[0]['classname'] == 'Rock': self.robotpi_movement.play_sound(2, 16) else: print("暂无结果") self.robotpi_movement.play_sound(2, 4)
def gestureRecog(self): APP_ID = '16534881' API_KEY = 'DSG6hUBgBVt61Ms1XXnYuEPm' SECRET_KEY = 'W4nkl6xYmOnRjntg53fDMuxQXvqemNI0' a = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) cap=cv2.VideoCapture(0) while 1: ret,image=cap.read() cv2.imshow("capture", image) if cv2.waitKey(100)&0xff==ord("q"): cv2.imwrite("test.jpg", image) break cap.release() cv2.destroyAllWindows() filePath = "test.jpg" options = {} options["top_num"] = 5 result=a.gesture(self.get_file_content(filePath),options) print(result) k=result["result"][0]["classname"] if k=="Insult": self.port_reset() if k=="ILY": self.Slider_3.setValue(500) time.sleep(0.5) self.Slider_3.setValue(1500) else: return
""" 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() #mixer.init() lastRes=0 while True: """1.拍照 """ camera.start_preview() time.sleep(1) #mixer.music.stop() camera.capture('./image.jpg') camera.stop_preview() image = get_file_content('./image.jpg') """ 2.调用手势识别 """ raw = str(client.gesture(image)) text = demjson.decode(raw) # decode 返回的json try: res = text['result'][0]['classname'] except: print('Nothing detected!' ) else: if res==lastRes: print('Nothing detected!' ) else: lastRes=res print('Detect' + hand[res])
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW) ret,frame = cap.read() # cv2.imshow('capture',frame) cv2.imwrite('photo.jpg',frame) cap.release() cv2.destroyAllWindows() def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('photo.jpg') """ 调用手势识别 """ answer = client.gesture(image) try: result = answer['result'][0]['classname'] except: result = 'xxx' hand = sign_dic[result] print('\r'+hand+' ',end='') if hand == '5': kb.press_keys([kb.menu_key,'q']) kb.release_key('q') kb.release_key(kb.space_key) elif hand == '2': kb.press_keys([kb.menu_key,kb.right_key]) kb.release_key(kb.right_key) kb.release_key(kb.menu_key)
class myGesture: """ 实现手势识别,缺点是只能用open函数读取本地照片 """ # APPID AK SK __APP_ID = '23524317' __API_KEY = 'gezpEkWBPsa63SE7gRf4Lzlv' __SECRET_KEY = '7urulBhtACXBsnq5YWWNwt4m15mLrj4V' def __init__(self): self.__client = AipBodyAnalysis(self.__APP_ID, self.__API_KEY, self.__SECRET_KEY) def __recognition(self, image): img_str = imencode('.jpg', image)[1].tobytes() imagesResult = self.__client.gesture(img_str) return imagesResult def __getResult(self, images): result = '' #空代表无效命令 #用try解决报错问题 try: if len(images['result']) == 0: return result else: if images['result'][0]['classname'] == 'Prayer': result = "开始" elif images['result'][0]['classname'] == 'Thumb_up': result = "起飞" elif images['result'][0]['classname'] == 'Thumb_down': result = "降落" elif images['result'][0]['classname'] == 'One': result = "向上" elif images['result'][0]['classname'] == 'Two': result = "向下" elif images['result'][0]['classname'] == 'Three': result = "向左" elif images['result'][0]['classname'] == 'Four': result = "向右" elif images['result'][0]['classname'] == 'Five': result = "向前" elif images['result'][0]['classname'] == 'Six': result = "向后" elif images['result'][0]['classname'] == 'Seven': result = "顺时针旋转90度" elif images['result'][0]['classname'] == 'Eight': result = "逆时针旋转90度" elif images['result'][0]['classname'] == 'Nine': result = "悬停" elif images['result'][0]['classname'] == 'Fist': result = "在手上降落" else: result = '' return result except: return '' def getOrder(self, images): """ 返回指令含义以及发送给无人机的指令,假如result或command为空字符串的话,代表无效指令 :param images: 原始图像帧 :return: (result,command) """ imagesResult = self.__recognition(images) result = self.__getResult(imagesResult) command = self.__action(result) return (result, command) def __action(self, result): """如果返回为空字符串的话代表无效指令""" command = '' if result == "开始": command = "command" elif result == "起飞": command = "takeoff" elif result == "降落": command = "land" elif result == "向上": command = "up" elif result == "向下": command = "down" elif result == "向左": command = "left" elif result == "向右": command = "right" elif result == "向前": command = "forward" elif result == "向后": command = "back" elif result == "顺时针旋转90度": command = "cw 90" elif result == "逆时针旋转90度": command = "ccw 90" elif result == "悬停": command = "stop" elif result == '': command = '' elif result == '在手上降落': command = 'landhand' return command
from aip import AipBodyAnalysis """ 你的 APPID AK SK """ APP_ID = '22952720' API_KEY = 'inMk4SrpYUxPmSUGmqGw89Ow' SECRET_KEY = '7ecoXWQBLwwEjwp83Od0jV5EZM166oG7' client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) """ 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('ok_img.jpg') """ 调用手势识别 """ resp = client.gesture(image) if resp['result'] and resp['result'][0]['classname']: print('识别成功,手势:{}'.format(resp['result'][0]['classname'])) else: print('识别失败')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sun Feb 2 17:05:31 2020 @author: hawk """ from aip import AipBodyAnalysis APP_ID = "18332846" API_KEY = "qKEHoRG0SRL08aUoWnadSNN5" SECRET_KEY = "s4Iu3wPEf2o7vtWulHiNn0Iuxps22tkf" client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) """ 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('image/image2.jpg') """ 调用手势识别 """ result_dict = client.gesture(image) print(result_dict['result']) for item in result_dict['result']: print(item['classname'])
class ai(): def __init__(self): """ 你的 APPID AK SK """ APP_ID = '16947698' API_KEY = 'fN9yStlTEv1zkWoMDMQU7YKT' SECRET_KEY = 'fqk4FfDRZp7fjk1ngcTeeaKtgWZxFh6H' self.gesture_client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) self.gesture_contrast = { "Ok": "1", "Thumb_up": "4", "Thumb_down": "5", "Insult": "2", "Six": "3", "Five": "6", "Fist": "7", "Honour": "8", } # 0指的是摄像头的编号。如果你电脑上有两个摄像头的话,访问第2个摄像头就可以传入1。 self.capture = cv2.VideoCapture(0) self.music_names = os.listdir('music') # 列表歌 self.music_source = [] # 音频播放路劲 self.current_music_no = 0 # 音乐 self.volume = 0.2 # 设置播放频道的音量 def camera(self): while True: ret, frame = self.capture.read() # cv2.imshow(窗口名称, 窗口显示的图像) cv2.imshow('frame', frame) if cv2.waitKey(1) == ord('q'): break def gesture_recognition(self): ret, frame = self.capture.read() image = cv2.imencode(".png", frame)[1].tobytes() result = self.gesture_client.gesture(image) gesture = result['result'] if gesture: gesture = gesture[0]['classname'] else: print('未获取到手势指令') gesture = 'None' return self.gesture_contrast[ gesture] if gesture in self.gesture_contrast else "指令未收录" def bofang(self): while True: print('* 1.播放\n' '* 2.暂停\n' '* 3.继续播放\n' '* 4.上一首\n' '* 5.下一首\n' '* 6.音量增加\n' '* 7.音量减少\n' '* 8.退出\n') while True: command = self.gesture_recognition() # 根据手势获取数字,结果为数字 if command in ['1', '2', '3', '4', '5', '6', '7', '8']: print('接受的指令为:', command) break if command == '1': mixer.music.load(self.music_source[ self.current_music_no]) # Load a music file mixer.music.play() elif command == '2': mixer.music.pause() elif command == '3': mixer.music.unpause() # 4.上一首 elif command == '4': if self.current_music_no > 0: self.current_music_no -= 1 else: # len(music_source)=4 current_music_no=3 0,1,2,3 self.current_music_no = len(self.music_source) - 1 mixer.music.load(self.music_source[self.current_music_no]) mixer.music.play() # 5.下一首 elif command == '5': # len(music_source)=4 0<3 if self.current_music_no < len(self.music_source) - 1: self.current_music_no += 1 else: self.current_music_no = 0 # 0,1,2,3 3-->0 mixer.music.load(self.music_source[self.current_music_no]) mixer.music.play() elif command == '6': if self.volume < 1: self.volume += 0.1 mixer.music.set_volume(self.volume) else: print('音量已经是最大了') elif command == '7': if self.volume > 0: self.volume -= 0.1 mixer.music.set_volume(self.volume) else: print('音量已经是最小了') elif command == '8': print('退出播放器') # kill主进程 getpid得到 进程 id 9绝杀,不管是否死机等,强制关闭 os.kill(os.getpid(), 9) print('正在播放歌曲:', self.music_names[self.current_music_no]) print('当前音量', self.volume) def shezhi(self): print(self.music_names) for name in self.music_names: path = 'music/' + name self.music_source.append(path) mixer.init() # 初始化 mixer.music.set_volume(self.volume) # 设置播放频道的音量 def run(self): self.shezhi() self.bofang()