685
|
1 #!camtest/bin/python3
|
|
2 import binascii
|
|
3 import logging
|
|
4 import time
|
|
5 import io
|
|
6 import os
|
|
7 import json
|
773
|
8 from docopt import docopt
|
|
9 from standardservice.logsetup import log, verboseLogging
|
|
10
|
685
|
11 logging.basicConfig(level=logging.INFO)
|
|
12 from aiohttp import web
|
|
13 from aiohttp.web import Response
|
|
14 from aiohttp_sse import sse_response
|
|
15
|
|
16 import asyncio
|
|
17
|
|
18 from aioesphomeapi import APIClient
|
|
19 from aioesphomeapi.model import CameraState
|
|
20 import apriltag
|
|
21 import cv2
|
|
22 import numpy
|
|
23
|
|
24 class CameraReceiver:
|
773
|
25 def __init__(self, loop, host):
|
685
|
26 self.lastFrameTime = None
|
|
27 self.loop = loop
|
773
|
28 self.host = host
|
685
|
29 self.lastFrame = b"", ''
|
|
30 self.recent = []
|
|
31
|
|
32 async def start(self):
|
773
|
33 try:
|
|
34 self.c = c = APIClient(self.loop,
|
|
35 self.host,
|
|
36 6053, 'MyPassword')
|
|
37 await c.connect(login=True)
|
|
38 await c.subscribe_states(on_state=self.on_state)
|
|
39 except OSError:
|
|
40 loop.stop()
|
|
41 return
|
|
42 self.loop.create_task(self.start_requesting_image_stream_forever())
|
|
43
|
|
44 async def start_requesting_image_stream_forever(self):
|
|
45 while True:
|
|
46 try:
|
|
47 await self.c.request_image_stream()
|
|
48 except AttributeError:
|
|
49 self.loop.stop()
|
|
50 return
|
|
51 # https://github.com/esphome/esphome/blob/dev/esphome/components/esp32_camera/esp32_camera.cpp#L265 says a 'stream' is 5 sec long
|
|
52 await asyncio.sleep(4)
|
685
|
53
|
|
54 def on_state(self, s):
|
|
55 if isinstance(s, CameraState):
|
|
56 jpg = s.image
|
|
57 if len(self.recent) > 10:
|
|
58 self.recent = self.recent[-10:]
|
|
59
|
|
60 self.recent.append(jpg)
|
773
|
61 #print('recent lens: %s' % (','.join(str(len(x))
|
|
62 # for x in self.recent)))
|
685
|
63 else:
|
|
64 print('other on_state', s)
|
|
65
|
|
66 def analyze(self, jpg):
|
|
67 img = cv2.imdecode(numpy.asarray(bytearray(jpg)),
|
|
68 cv2.IMREAD_GRAYSCALE)
|
|
69 result = detector.detect(img)
|
|
70 msg = {}
|
|
71 if result:
|
|
72 center = result[0].center
|
|
73 msg['center'] = [round(center[0], 2), round(center[1], 2)]
|
|
74 return msg
|
|
75
|
|
76 async def frames(self):
|
|
77 while True:
|
|
78 if self.recent:
|
|
79 if self.lastFrameTime and time.time() - self.lastFrameTime > 15:
|
|
80 print('no recent frames')
|
|
81 os.abort()
|
|
82
|
|
83 jpg = self.recent.pop(0)
|
|
84 msg = self.analyze(jpg)
|
|
85 yield jpg, msg
|
|
86 self.lastFrame = jpg, msg
|
|
87 self.lastFrameTime = time.time()
|
|
88 else:
|
773
|
89 await asyncio.sleep(.05)
|
685
|
90
|
|
91
|
|
92 def imageUri(jpg):
|
|
93 return 'data:image/jpeg;base64,' + binascii.b2a_base64(jpg).decode('ascii')
|
|
94
|
|
95 async def stream(request):
|
|
96 async with sse_response(request) as resp:
|
|
97 await resp.send(imageUri(recv.lastFrame[0]))
|
|
98 await resp.send(json.dumps(recv.lastFrame[1]))
|
|
99 async for frame, msg in recv.frames():
|
|
100 await resp.send(json.dumps(msg))
|
|
101 await resp.send(imageUri(frame))
|
|
102 return resp
|
|
103
|
|
104 async def index(request):
|
|
105 d = r"""
|
|
106 <html>
|
|
107 <body>
|
|
108 <style>
|
|
109 #center {
|
|
110 position: absolute;
|
|
111 font-size: 35px;
|
|
112 color: orange;
|
|
113 text-shadow: black 0 1px 1px;
|
|
114 margin-left: -14px;
|
|
115 margin-top: -23px;
|
|
116 }
|
|
117 </style>
|
|
118 <script>
|
|
119 var evtSource = new EventSource("/stream");
|
|
120 evtSource.onmessage = function(e) {
|
|
121 if (e.data[0] == '{') {
|
|
122 const msg = JSON.parse(e.data);
|
|
123 const st = document.querySelector('#center').style;
|
|
124 if (msg.center) {
|
|
125 st.left = msg.center[0];
|
|
126 st.top = msg.center[1];
|
|
127 } else {
|
|
128 st.left = -999;
|
|
129 }
|
|
130 } else {
|
|
131 document.getElementById('response').src = e.data;
|
|
132 }
|
|
133 }
|
|
134 </script>
|
|
135 <h1>Response from server:</h1>
|
|
136 <div style="position: relative">
|
|
137 <img id="response"></img>
|
|
138 <span id="center" style="position: absolute">◎</span>
|
|
139 </div>
|
|
140 </body>
|
|
141 </html>
|
|
142 """
|
|
143 return Response(text=d, content_type='text/html')
|
|
144
|
773
|
145 arguments = docopt('''
|
|
146 this
|
685
|
147
|
773
|
148 Usage:
|
|
149 this [-v] [--cam host] [--port to_serve]
|
|
150
|
|
151 Options:
|
|
152 -v --verbose more log
|
|
153 --port n http server [default: 10020]
|
|
154 --cam host hostname of esphome server
|
|
155 ''')
|
|
156
|
|
157 verboseLogging(arguments['--verbose'])
|
|
158 logging.getLogger('aioesphomeapi.connection').setLevel(logging.INFO)
|
|
159
|
|
160 loop = asyncio.get_event_loop()
|
|
161
|
|
162 recv = CameraReceiver(loop, arguments['--cam'])
|
|
163 detector = apriltag.Detector()
|
|
164
|
|
165 f = recv.start()
|
|
166 loop.create_task(f)
|
|
167
|
|
168 start_time = time.time()
|
685
|
169 app = web.Application()
|
|
170 app.router.add_route('GET', '/stream', stream)
|
|
171 app.router.add_route('GET', '/', index)
|
773
|
172 try:
|
|
173 web.run_app(app, host='0.0.0.0', port=int(arguments['--port']))
|
|
174 except RuntimeError as e:
|
|
175 log.error(e)
|
|
176 log.info(f'run_app stopped after {time.time() - start_time} sec') |