human/ui/ipc_render.py

73 lines
2.1 KiB
Python
Raw Normal View History

2024-12-02 16:14:00 +00:00
#encoding = utf8
import os
import logging
2024-12-03 18:07:18 +00:00
import time
2024-12-02 16:14:00 +00:00
from queue import Queue
from human import HumanContext
from ipc import IPCUtil
2024-12-03 18:07:18 +00:00
from utils import render_image
2024-12-02 16:14:00 +00:00
logger = logging.getLogger(__name__)
current_file_path = os.path.dirname(os.path.abspath(__file__))
class IpcRender:
def __init__(self):
self._human_context = None
self._queue = None
2024-12-03 18:07:18 +00:00
self._exit = False
self._ipc = None
def _send_image(self, identifier, image):
height, width, channels = image.shape
width_bytes = width.to_bytes(4, byteorder='little')
height_bytes = height.to_bytes(4, byteorder='little')
bit_depth_bytes = channels.to_bytes(4, byteorder='little')
img_bytes = image.tobytes()
data = identifier + width_bytes + height_bytes + bit_depth_bytes + img_bytes
self._ipc.send_binary(data, len(data))
def _on_reader_callback(self, data_str, size):
data_str = data_str.decode('utf-8')
print(f'on_reader_callback: {data_str}, size:{size}')
if 'quit' == data_str:
self._exit = True
elif 'heartbeat' == data_str:
pass
2024-12-02 16:14:00 +00:00
def run(self):
self._queue = Queue()
self._human_context = HumanContext()
self._human_context.build()
2024-12-03 18:07:18 +00:00
self._ipc = IPCUtil('human_product', 'human_render')
self._ipc.set_reader_callback(self._on_reader_callback)
logger.info(f'ipc listen:{self._ipc.listen()}')
2024-12-02 16:14:00 +00:00
render = self._human_context.render_handler
render.set_image_render(self)
2024-12-03 18:07:18 +00:00
while not self._exit:
if not self._queue.empty():
while self._queue.qsize() > 5:
self._queue.get()
print('render queue is slower')
2024-12-02 16:14:00 +00:00
2024-12-03 18:07:18 +00:00
image = self._queue.get()
image = render_image(self._human_context, image)
self._send_image(b'\x01', image)
else:
time.sleep(0.02)
logger.info('ipc render exit')
2024-12-02 16:14:00 +00:00
2024-12-03 18:07:18 +00:00
def stop(self):
2024-12-04 16:47:17 +00:00
if self._human_context is not None:
self._human_context.stop()
2024-12-02 16:14:00 +00:00
2024-12-03 18:07:18 +00:00
def on_render(self, image):
self._queue.put(image)
2024-12-02 16:14:00 +00:00