一个面向webrtc机器人控制的异步库
rtcbot的Python项目详细描述
rtcbot
rtcbot的目的是提供一组简单的模块,帮助在python中开发远程控制机器人,重点是覆盆子pi。
本文档包括开发机器人的指南,从树莓pi和浏览器之间的基本连接开始,包括 创建一个完全由4G移动连接控制的视频流机器人,一直到一个强大的系统,该系统将复杂的计算实时卸载到桌面PC。
所有的交流都是通过WebRTC, 使用python 3的异步和美妙的aiortc库, 这意味着你的机器人可以通过浏览器和python以低延迟进行控制, 即使它没有连接到您的本地网络。
图书馆是在the documentation中逐件解释的。
See Documentation & Tutorials
安装
pip install rtcbot
示例
本例使用rtcbot将网络摄像头实时传输到浏览器。详情请看the tutorials。
将视频流到浏览器的python代码:
fromaiohttpimportwebroutes=web.RouteTableDef()fromrtcbotimportRTCConnection,getRTCBotJS,CVCameracamera=CVCamera()# For this example, we use just one global connectionconn=RTCConnection()conn.video.putSubscription(camera)# Serve the RTCBot javascript library at /rtcbot.js@routes.get("/rtcbot.js")asyncdefrtcbotjs(request):returnweb.Response(content_type="application/javascript",text=getRTCBotJS())# This sets up the connection@routes.post("/connect")asyncdefconnect(request):clientOffer=awaitrequest.json()serverResponse=awaitconn.getLocalDescription(clientOffer)returnweb.json_response(serverResponse)@routes.get("/")asyncdefindex(request):withopen("index.html","r")asf:returnweb.Response(content_type="text/html",text=f.read())asyncdefcleanup(app):awaitconn.close()camera.close()app=web.Application()app.add_routes(routes)app.on_shutdown.append(cleanup)web.run_app(app)
显示视频流的浏览器代码(index.html):
<html><head><title>RTCBot: Video</title><scriptsrc="/rtcbot.js"></script></head><bodystyle="text-align: center;padding-top: 30px;"><videoautoplayplaysinline></video><audioautoplay></audio><p> Open the browser's developer tools to see console messages (CTRL+SHIFT+C) </p><script>varconn=newrtcbot.RTCConnection();conn.video.subscribe(function(stream){document.querySelector("video").srcObject=stream;});asyncfunctionconnect(){letoffer=awaitconn.getLocalDescription();// POST the information to /connectletresponse=awaitfetch("/connect",{method:"POST",cache:"no-cache",body:JSON.stringify(offer)});awaitconn.setRemoteDescription(awaitresponse.json());console.log("Ready!");}connect();</script></body></html>
开发
要直接使用rtcbot
代码,请克隆存储库并安装需求:
pip install -r requirements.txt
然后,您需要执行javascript构建步骤,以准备浏览器代码。此步骤要求在计算机上同时安装make
和npm
。要构建javascript,请键入:
make js
这将创建由rtcbot.getRTCBotJS()
返回的rtcbot/rtcbot.js
。
在这两个步骤之后,您应该能够成功地导入和使用库。