685
|
1 #!camtest/bin/python3
|
|
2 import binascii
|
|
3 import logging
|
|
4 import time
|
|
5 import io
|
|
6 import os
|
|
7 import json
|
|
8 logging.basicConfig(level=logging.INFO)
|
|
9 from aiohttp import web
|
|
10 from aiohttp.web import Response
|
|
11 from aiohttp_sse import sse_response
|
|
12
|
|
13 import asyncio
|
|
14
|
|
15 from aioesphomeapi import APIClient
|
|
16 from aioesphomeapi.model import CameraState
|
|
17 import apriltag
|
|
18 import cv2
|
|
19 import numpy
|
|
20
|
|
21 class CameraReceiver:
|
|
22 def __init__(self, loop):
|
|
23 self.lastFrameTime = None
|
|
24 self.loop = loop
|
|
25 self.lastFrame = b"", ''
|
|
26 self.recent = []
|
|
27
|
|
28 async def start(self):
|
|
29 self.c = c = APIClient(self.loop, '10.2.0.21', 6053, 'MyPassword')
|
|
30 await c.connect(login=True)
|
|
31 await c.subscribe_states(on_state=self.on_state)
|
|
32 await c.request_image_stream()
|
|
33
|
|
34 def on_state(self, s):
|
|
35 if isinstance(s, CameraState):
|
|
36 jpg = s.image
|
|
37 if len(self.recent) > 10:
|
|
38 self.recent = self.recent[-10:]
|
|
39
|
|
40 self.recent.append(jpg)
|
|
41 print('recent lens: %s' % (','.join(str(len(x))
|
|
42 for x in self.recent)))
|
|
43 else:
|
|
44 print('other on_state', s)
|
|
45
|
|
46 def analyze(self, jpg):
|
|
47 img = cv2.imdecode(numpy.asarray(bytearray(jpg)),
|
|
48 cv2.IMREAD_GRAYSCALE)
|
|
49 result = detector.detect(img)
|
|
50 msg = {}
|
|
51 if result:
|
|
52 center = result[0].center
|
|
53 msg['center'] = [round(center[0], 2), round(center[1], 2)]
|
|
54 return msg
|
|
55
|
|
56 async def frames(self):
|
|
57 while True:
|
|
58 if self.recent:
|
|
59 if self.lastFrameTime and time.time() - self.lastFrameTime > 15:
|
|
60 print('no recent frames')
|
|
61 os.abort()
|
|
62
|
|
63 jpg = self.recent.pop(0)
|
|
64 msg = self.analyze(jpg)
|
|
65 yield jpg, msg
|
|
66 self.lastFrame = jpg, msg
|
|
67 self.lastFrameTime = time.time()
|
|
68 else:
|
|
69 await asyncio.sleep(.5)
|
|
70
|
|
71 loop = asyncio.get_event_loop()
|
|
72
|
|
73 recv = CameraReceiver(loop)
|
|
74 detector = apriltag.Detector()
|
|
75
|
|
76 loop.create_task(recv.start())
|
|
77
|
|
78 def imageUri(jpg):
|
|
79 return 'data:image/jpeg;base64,' + binascii.b2a_base64(jpg).decode('ascii')
|
|
80
|
|
81 async def stream(request):
|
|
82 async with sse_response(request) as resp:
|
|
83 await resp.send(imageUri(recv.lastFrame[0]))
|
|
84 await resp.send(json.dumps(recv.lastFrame[1]))
|
|
85 async for frame, msg in recv.frames():
|
|
86 await resp.send(json.dumps(msg))
|
|
87 await resp.send(imageUri(frame))
|
|
88 return resp
|
|
89
|
|
90 async def index(request):
|
|
91 d = r"""
|
|
92 <html>
|
|
93 <body>
|
|
94 <style>
|
|
95 #center {
|
|
96 position: absolute;
|
|
97 font-size: 35px;
|
|
98 color: orange;
|
|
99 text-shadow: black 0 1px 1px;
|
|
100 margin-left: -14px;
|
|
101 margin-top: -23px;
|
|
102 }
|
|
103 </style>
|
|
104 <script>
|
|
105 var evtSource = new EventSource("/stream");
|
|
106 evtSource.onmessage = function(e) {
|
|
107 if (e.data[0] == '{') {
|
|
108 const msg = JSON.parse(e.data);
|
|
109 const st = document.querySelector('#center').style;
|
|
110 if (msg.center) {
|
|
111 st.left = msg.center[0];
|
|
112 st.top = msg.center[1];
|
|
113 } else {
|
|
114 st.left = -999;
|
|
115 }
|
|
116 } else {
|
|
117 document.getElementById('response').src = e.data;
|
|
118 }
|
|
119 }
|
|
120 </script>
|
|
121 <h1>Response from server:</h1>
|
|
122 <div style="position: relative">
|
|
123 <img id="response"></img>
|
|
124 <span id="center" style="position: absolute">◎</span>
|
|
125 </div>
|
|
126 </body>
|
|
127 </html>
|
|
128 """
|
|
129 return Response(text=d, content_type='text/html')
|
|
130
|
|
131
|
|
132 app = web.Application()
|
|
133 app.router.add_route('GET', '/stream', stream)
|
|
134 app.router.add_route('GET', '/', index)
|
|
135 web.run_app(app, host='0.0.0.0', port=10020)
|