def _start_fury_client(self, use_asyncio=False): """Start the fury image buffer client and the interaction client Parameters ---------- use_asyncio : bool, optional If should use asyncio to start the server. Default is False. """ if self._server_started: self.stop() self.stream = FuryStreamClient(self.showm, max_window_size=self.max_window_size, use_raw_array=False, whithout_iren_start=True) self.stream_interaction = FuryStreamInteraction( self.showm, max_queue_size=self.queue_size, whithout_iren_start=True, use_raw_array=False) self.stream_interaction.start(ms=self.ms_interaction, use_asyncio=use_asyncio) self.stream.start(self.ms_stream, use_asyncio=use_asyncio) self._server_started = True self.pserver = None
def test(use_raw_array, ms_stream=16): width_0 = 100 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) showm.initialize() stream = FuryStreamClient(showm, use_raw_array=use_raw_array, whithout_iren_start=False) if use_raw_array: img_buffer_manager = tools.RawArrayImageBufferManager( info_buffer=stream.img_manager.info_buffer, image_buffers=stream.img_manager.image_buffers) else: img_buffer_manager = tools.SharedMemImageBufferManager( info_buffer_name=stream.img_manager.info_buffer_name, image_buffer_names=stream.img_manager.image_buffer_names) showm.render() stream.start(ms_stream) showm.render() # test jpeg method img_buffer_manager.get_jpeg() width, height, frame = img_buffer_manager.get_current_frame() # assert width == showm.size[0] and height == showm.size[1] image = np.frombuffer(frame, 'uint8')[0:width * height * 3].reshape( (height, width, 3)) # image = np.flipud(image) # image = image[:, :, ::-1] # import matplotlib.pyplot as plt # plt.imshow(image) # plt.show() # npt.assert_allclose(arr, image) report = window.analyze_snapshot(image, find_objects=True) npt.assert_equal(report.objects, 3) img_buffer_manager.cleanup() stream.stop() stream.cleanup()
def test_rtc_video_stream_whitout_cython(loop: asyncio.AbstractEventLoop): if not WEBRTC_AVAILABLE: print('\n aiortc not available -> skipping test\n') return use_raw_array = True ms_stream = 0 # creates a context whithout cython with mock.patch.dict(sys.modules, {'pyximport': None}): reload(sys.modules["fury.stream.server.main"]) width_0 = 100 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) showm.initialize() stream = FuryStreamClient(showm, use_raw_array=use_raw_array, whithout_iren_start=False) if use_raw_array: img_buffer_manager = tools.RawArrayImageBufferManager( info_buffer=stream.img_manager.info_buffer, image_buffers=stream.img_manager.image_buffers) else: img_buffer_manager = tools.SharedMemImageBufferManager( info_buffer_name=stream.img_manager.info_buffer_name, image_buffer_names=stream.img_manager.image_buffer_names) rtc_server = RTCServer(img_buffer_manager) showm.render() stream.start(ms_stream) showm.render() loop.run_until_complete(rtc_server.recv()) # assert frame.width == showm.size[0] and frame.height == showm.size[1] rtc_server.release() img_buffer_manager.cleanup() stream.stop() stream.cleanup() reload(sys.modules["fury.stream.server.main"])
def test_stream_client_resize(): width_0 = 100 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) with npt.assert_raises(ValueError): FuryStreamClient(showm, use_raw_array=True, max_window_size=(width_0 - 10, height_0), whithout_iren_start=False) stream = FuryStreamClient(showm, use_raw_array=True, max_window_size=(width_0, height_0), whithout_iren_start=False) showm.window.SetSize((width_0 + 210, height_0 + 210)) showm.initialize() npt.assert_equal(0, stream.img_manager.buffer_index) stream.start() showm.render() showm.scene.azimuth(1) showm.render() showm.render() stream.stop() stream.cleanup()
def test(use_raw_array, ms_stream=16): width_0 = 100 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) showm.initialize() stream = FuryStreamClient(showm, use_raw_array=use_raw_array, whithout_iren_start=False) if use_raw_array: img_buffer_manager = tools.RawArrayImageBufferManager( info_buffer=stream.img_manager.info_buffer, image_buffers=stream.img_manager.image_buffers) else: img_buffer_manager = tools.SharedMemImageBufferManager( info_buffer_name=stream.img_manager.info_buffer_name, image_buffer_names=stream.img_manager.image_buffer_names) rtc_server = RTCServer(img_buffer_manager) showm.render() stream.start(ms_stream) showm.render() loop.run_until_complete(rtc_server.recv()) # sassert frame.width == width_0 and frame.height == height_0 rtc_server.release() img_buffer_manager.cleanup() stream.stop() stream.cleanup()
def test(use_raw_array): width_0 = 100 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) stream = FuryStreamClient(showm, use_raw_array=use_raw_array) stream_interaction = FuryStreamInteraction(showm, use_raw_array=use_raw_array) showm.initialize() if use_raw_array: web_server_raw_array( stream.img_manager.image_buffers, stream.img_manager.info_buffer, stream_interaction.circular_queue.head_tail_buffer, stream_interaction.circular_queue.buffer._buffer, 8000, 'localhost', True, True, run_app=False) else: web_server(stream.img_manager.image_buffer_names, stream.img_manager.info_buffer_name, stream_interaction.circular_queue.head_tail_buffer_name, stream_interaction.circular_queue.buffer.buffer_name, 8000, 'localhost', True, True, True, run_app=False) stream.stop() stream_interaction.stop() stream.cleanup()
def test(use_raw_array, ms_stream, whitouth_iren_start): width_0 = 300 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager(scene, size=(width_0, height_0)) showm.initialize() stream = FuryStreamClient(showm, use_raw_array=use_raw_array, whithout_iren_start=whitouth_iren_start) stream_interaction = FuryStreamInteraction( max_queue_size=500, showm=showm, use_raw_array=use_raw_array, whithout_iren_start=whitouth_iren_start) showm.render() # ms should always be greather than 0 with npt.assert_raises(ValueError): stream_interaction.start(-1) stream_interaction.start(ms_stream) # test double start stream_interaction.start(ms_stream) while stream_interaction.circular_queue.head != -1: showm.render() time.sleep(.01) stream_interaction.stop() # double stop test npt.assert_equal(stream_interaction.stop(), False) stream.stop() stream.cleanup() stream_interaction.cleanup()
async def test(use_raw_array, ms_stream=16): width_0 = 300 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) showm.initialize() stream = FuryStreamClient(showm, use_raw_array=use_raw_array, whithout_iren_start=True) stream_interaction = FuryStreamInteraction(max_queue_size=500, showm=showm, use_raw_array=use_raw_array, whithout_iren_start=True) showm.render() # test jpeg method for _ in range(10): stream_interaction.circular_queue.enqueue( np.array([_CQUEUE.event_ids.mouse_weel, 1, 0, 0, 0, 0, .1, 0], dtype='d')) for _ in range(10): stream_interaction.circular_queue.enqueue( np.array([_CQUEUE.event_ids.mouse_weel, -1, 0, 0, 0, 0, .1, 0], dtype='d')) dxs = [] for shift, ctrl in ((0, 1), (1, 0), (0, 0)): x = width_0 / 2 y = height_0 / 2 stream_interaction.circular_queue.enqueue( np.array([ _CQUEUE.event_ids.left_btn_press, 0, x, y, ctrl, shift, .1, 0 ], dtype='d')) for i in range(50): if ctrl == 1: x = x + i / 50 * width_0 / 4 else: if i < 25: dx = +i / 50 dxs.append(dx) x = x - dx else: x = x + dxs[::-1][i - 25] stream_interaction.circular_queue.enqueue( np.array([ _CQUEUE.event_ids.mouse_move, 0, x, y, ctrl, shift, .1, 0 ], dtype='d')) stream_interaction.circular_queue.enqueue( np.array([ _CQUEUE.event_ids.left_btn_release, 0, x, y, ctrl, shift, .1, 0 ], dtype='d')) stream_interaction.start(ms_stream, use_asyncio=True) while stream_interaction.circular_queue.head != -1: showm.render() await asyncio.sleep(.01) stream_interaction.stop() stream.stop() stream.cleanup() stream_interaction.cleanup()
def test(use_raw_array, ms_stream=16, whithout_iren_start=False): width_0 = 100 height_0 = 200 centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, colors=colors, radii=.1) scene = window.Scene() scene.add(actors) showm = window.ShowManager( scene, reset_camera=False, size=(width_0, height_0), order_transparent=False, ) showm.initialize() stream = FuryStreamClient(showm, use_raw_array=use_raw_array, whithout_iren_start=whithout_iren_start) showm.render() stream.start(ms_stream) npt.assert_equal(stream._started, True) # test if stop method has been called stream.start(ms_stream) npt.assert_equal(stream._started, True) showm.render() stream.stop() # double stop test npt.assert_equal(stream.stop(), False) stream.cleanup()
class Widget: """Thi Obj it's able execute the fury streaming system using the SharedMemory object from Python multiprocessing. """ def __init__(self, showm, ms_stream=33, ms_interaction=33, host='localhost', port=None, encoding='mjpeg', ms_jpeg=33, queue_size=20): """ Parameters ---------- showm : ShowmManager ms_stream : float, optional time in mileseconds between each frame buffer update. ms_interaction : float, optional time in mileseconds between each user interaction update. host : str, optional port : int, optional encoding : str, optional If should use MJPEG streaming or WebRTC. ms_jpeg : float, optional This it's used only if the MJPEG will be used. The ms_jpeg represents the amount of miliseconds between to consecutive calls of the jpeg enconding. queue_size : int, optional maximum number of user interactions to be stored """ if not PY_VERSION_8: raise ImportError('Python 3.8 or greater is required to use the\ widget class') self.showm = showm self.window_size = self.showm.size max_window_size = (int(self.window_size[0] * (1 + 0.1)), int(self.window_size[1] * (1 + 0.1))) self.max_window_size = max_window_size self.ms_stream = ms_stream self.ms_interaction = ms_interaction self.ms_jpeg = ms_jpeg self._host = host if port is None: port = np.random.randint(7000, 8888) self._port = port self.queue_size = queue_size self._server_started = False self.pserver = None self.encoding = encoding self.showm.window.SetOffScreenRendering(1) self.showm.iren.EnableRenderOff() @property def command_string(self): """Return the command string to start the server Returns ------- command_string : str """ s = 'from fury.stream.server import web_server;' s += "web_server(image_buffer_names=" s += f"{self.stream.img_manager.image_buffer_names}" s += f",info_buffer_name='{self.stream.img_manager.info_buffer_name}'," s += "queue_head_tail_buffer_name='" s += f"{self.stream_interaction.circular_queue.head_tail_buffer_name}'" s += ",queue_buffer_name='" s += f"{self.stream_interaction.circular_queue.buffer.buffer_name}'" if self.encoding == 'mjpeg': s += ",provides_mjpeg=True" s += f",ms_jpeg={self.ms_jpeg}" s += ",provides_webrtc=False" s += f",port={self._port},host='{self._host}'," s += "avoid_unlink_shared_mem=True" s += ")" return s def _start_fury_client(self, use_asyncio=False): """Start the fury image buffer client and the interaction client Parameters ---------- use_asyncio : bool, optional If should use asyncio to start the server. Default is False. """ if self._server_started: self.stop() self.stream = FuryStreamClient(self.showm, max_window_size=self.max_window_size, use_raw_array=False, whithout_iren_start=True) self.stream_interaction = FuryStreamInteraction( self.showm, max_queue_size=self.queue_size, whithout_iren_start=True, use_raw_array=False) self.stream_interaction.start(ms=self.ms_interaction, use_asyncio=use_asyncio) self.stream.start(self.ms_stream, use_asyncio=use_asyncio) self._server_started = True self.pserver = None def run_command(self): """Evaluate the command string to start the server """ if self.pserver is not None: self._kill_server() i = 0 available = check_port_is_available(self._host, self._port) while not available and i < 50: self._port = np.random.randint(7000, 8888) available = check_port_is_available(self._host, self._port) i += 1 if not available: return False if self._server_started: args = [sys.executable, '-c', self.command_string] self.pserver = subprocess.Popen( args, # f'python -c "{self.command_string}"', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) return True @property def url(self): """Return the url to access the server """ url = f'http://{self._host}:{self._port}' url += f'?iframe=1&encoding={self.encoding}' return url def return_iframe(self, height=200): """Return the jupyter div iframe used to show the stream """ if IPYTHON_AVAILABLE: display(IFrame(self.url, '100%', f'{int(height)}px')) def start(self, use_asyncio=False): """Start the fury client and the interaction client and return the url Parameters ---------- use_asyncio : bool, optional If should use the asyncio version of the server. Default is False. """ self._start_fury_client(use_asyncio) ok = self.run_command() if not ok: self.stop() return False print(f'url: {self.url}') def display(self, height=150): """Start the server and display the url in an iframe """ self._start_fury_client() ok = self.run_command() if not ok: self.stop() return False time.sleep(2) self.return_iframe(height) def stop(self): """Stop the streaming server and release the shared memory """ if self._server_started: self.stream.stop() self.stream_interaction.stop() if self.pserver is not None: self._kill_server() self.cleanup() self._server_started = False def _kill_server(self): """Kill the server process """ self.pserver.kill() self.pserver.wait() self.pserver = None def cleanup(self): """Release the shared memory """ if self.stream is not None: self.stream.cleanup() if self.stream_interaction is not None: self.stream_interaction.cleanup() def __del__(self): self.stop()
focal_point=(0.0, 0.0, 0.0), view_up=(0.0, 0.0, 0.0)) showm = window.ShowManager( scene, reset_camera=False, size=(window_size[0], window_size[1]), order_transparent=False, ) ########################################################################### # ms define the amount of mileseconds that will be used in the timer event. ms = 0 showm.initialize() stream = FuryStreamClient(showm, use_raw_array=True) p = multiprocessing.Process(target=web_server_raw_array, args=( stream.img_manager.image_buffers, stream.img_manager.info_buffer, )) p.start() stream.start(ms, ) if interactive: showm.start() stream.stop() stream.cleanup() window.record(showm.scene, size=window_size,
max_queue_size = 17 ###################################################################### centers = np.array([[0, 0, 0], [-1, 0, 0], [1, 0, 0]]) colors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) actors = actor.sphere(centers, opacity=.5, radii=.4, colors=colors) scene = window.Scene() scene.add(actors) showm = window.ShowManager(scene, size=(window_size[0], window_size[1])) showm.initialize() stream = FuryStreamClient(showm, max_window_size=max_window_size, use_raw_array=use_raw_array) stream_interaction = FuryStreamInteraction(showm, max_queue_size=max_queue_size, use_raw_array=use_raw_array) if use_raw_array: p = multiprocessing.Process( target=web_server_raw_array, args=( stream.img_manager.image_buffers, stream.img_manager.info_buffer, stream_interaction.circular_queue.head_tail_buffer, stream_interaction.circular_queue.buffer._buffer, 8000, 'localhost',