想象一下,您正在为一个挚爱的人举办生日派对。每个人都玩得很开心,音乐在演奏,聚会很吵。突然,该吃生日蛋糕了!如果只要举手张开,您的智能家居设备会识别该手势并关闭音乐,使用相同的手势,您可以调暗灯光。那不是太神奇了吗?先看看下面的效果。
好了,我们来用python来实现它吧。
▊操作步骤
▶实现思路
我们使用开源计算机视觉库OpenCV,,该库可以从屏幕上抓取图像,然后调整图像大小并将其转换为模型可以理解的NumPy数组。我用来转换数据的方法如下:
X_data = np.array(X_data, dtype = 'float32') X_data = np.stack((X_data,) * 3, axis=-1)简而言之,一旦相机启动并运行,您就可以抓取图片,对其进行变换并从模型中进行预测:
#starts the webcam, uses it as video source camera = cv2.VideoCapture(0) #uses webcam for video while camera.isOpened(): #ret returns True if camera is running, frame grabs each frame of the video feed ret, frame = camera.read() k = cv2.waitKey(10) if k == 32: # if spacebar pressed frame = np.stack((frame,)*3, axis=-1) frame = cv2.resize(frame, (224, 224)) frame = frame.reshape(1, 224, 224, 3) prediction, score = predict_image(frame)
# Philips Hue Settings bridge_ip = '192.168.0.103' b = Bridge(bridge_ip) on_command = {'transitiontime' : 0, 'on' : True, 'bri' : 254} off_command = {'transitiontime' : 0, 'on' : False, 'bri' : 254} # Turn lights on b.set_light(6, on_command) #Turn lights off b.set_light(6, off_command) Using SoCo to control Sonos via the web API was arguably even easier: sonos_ip = '192.168.0.104' sonos = SoCo(sonos_ip) # Play sonos.play() #Pause sonos.pause()
if smart_home: if prediction == 'Palm': try: action = "Lights on, music on" sonos.play() # turn off smart home actions if devices are not responding except ConnectionError: smart_home = False # etc. etc.
网友评论0