-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Pass the mode changed to 0.2 -> 0.5 - Remove all input_output - PUT API in the last (Object Collector take long time to initialize)
- Loading branch information
1 parent
004429d
commit 358a2da
Showing
3 changed files
with
66 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,56 @@ | ||
class CameraServer(): | ||
pass | ||
import asyncio | ||
import websockets | ||
import logging | ||
|
||
from PIL import Image | ||
from io import BytesIO | ||
from vision.camera import Camera | ||
|
||
class CameraServer: | ||
def __init__(self): | ||
self.connected = set() | ||
self.logger = logging.getLogger(__name__) | ||
self.camera = Camera() | ||
|
||
async def send_to_all_clients(self): | ||
while True: | ||
await asyncio.sleep(1/20.0) | ||
|
||
if len(self.connected) == 0: | ||
continue | ||
|
||
img = self.camera.grab_frame_loop() | ||
if img is None: | ||
continue | ||
|
||
with BytesIO() as bytes: | ||
pil_img = Image.fromarray(img) | ||
pil_img.save(bytes, 'jpeg') | ||
message = bytes.getvalue() | ||
|
||
for websocket in list(self.connected): | ||
try: | ||
await websocket.send(message) | ||
except websockets.ConnectionClosed: | ||
self.logger.warning("Client disconnected. Removing from list.") | ||
self.connected.remove(websocket) | ||
|
||
async def handler(self, websocket, path): | ||
self.logger.debug("New connection added.") | ||
self.connected.add(websocket) | ||
|
||
try: | ||
async for message in websocket: | ||
self.logger.debug(f"Receive commands :", message) | ||
except (websockets.ConnectionClosed, Exception) as e: | ||
self.logger.warning(f"Connection closed due to error: {e}") | ||
finally: | ||
self.connected.remove(websocket) | ||
|
||
def run(self): | ||
server_coro = websockets.serve(self.handler, '0.0.0.0', 5678) | ||
|
||
self.loop = asyncio.get_event_loop() | ||
|
||
self.loop.run_until_complete(server_coro) | ||
self.loop.create_task(self.send_to_all_clients()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters