mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-22 15:45:39 +08:00
Clickable viewer deployment (#308)
* first commit for clickable web viewer * pylint ignore * functional clickable viewer with updated docs * fixed logging
This commit is contained in:
1 parent
d6b3eccd9c
commit
2cca012ad8
9 files changed
+181
-68
No files matched your search
@@ -283,6 +283,8 @@ class ViewerConfig(PrintableConfig):
|
||||
|
||||
enable: bool = False
|
||||
zmq_url: str = "tcp://127.0.0.1:6000"
|
||||
launch_bridge_server: bool = True
|
||||
websocket_port: int = 7007
|
||||
min_render_image_height: int = 64
|
||||
max_render_image_height: int = 1024
|
||||
num_rays_per_chunk: int = 4096
|
||||
|
||||
@@ -10,16 +10,23 @@ const WebSocketContext = createContext(null);
|
||||
|
||||
export { WebSocketContext };
|
||||
|
||||
function getWebsocketEndpointFromUrl(url: string) {
|
||||
const splitUrl = url.split('?');
|
||||
if (splitUrl.length !== 2) {
|
||||
window.alert("There should be exactly one '?' in the url.");
|
||||
return '';
|
||||
function getParam(param_name) {
|
||||
// https://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript
|
||||
const params = new RegExp(
|
||||
`[?&]${encodeURIComponent(param_name)}=([^&]*)`,
|
||||
).exec(window.location.href);
|
||||
if (params === null) {
|
||||
return undefined;
|
||||
}
|
||||
const endpoint = splitUrl.pop();
|
||||
if (endpoint === '') {
|
||||
return decodeURIComponent(params[1]);
|
||||
}
|
||||
|
||||
function getWebsocketEndpoint() {
|
||||
const endpoint = getParam('websocket_url');
|
||||
console.log("websocket url: ", endpoint);
|
||||
if (endpoint === undefined) {
|
||||
const message =
|
||||
'Please set the websocket endpoint. E.g., a correct URL may be: http://localhost:4000?localhost:8051';
|
||||
'Please set the websocket endpoint. The format should look like "<viewer_url>?websocket_url=localhost:<port>". E.g., "https://viewer.nerfactory.com/branch/master/?websocket_url=localhost:7007".';
|
||||
window.alert(message);
|
||||
return '';
|
||||
}
|
||||
@@ -32,8 +39,8 @@ export default function WebSocketContextFunction({ children }) {
|
||||
let socket;
|
||||
let ws;
|
||||
|
||||
// should look like ws://localhost:8051
|
||||
const websocketEndpoint = getWebsocketEndpointFromUrl(window.location.href);
|
||||
// should look like e.g., "ws://<localhost:port>"
|
||||
const websocketEndpoint = getWebsocketEndpoint();
|
||||
|
||||
const connect = () => {
|
||||
socket = new WebSocket(`ws://${websocketEndpoint}/`);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
"""Server bridge to faciliate interactions between python backend and javascript front end"""
|
||||
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from typing import Callable, List, Optional, Tuple
|
||||
|
||||
@@ -164,31 +165,39 @@ class ZMQWebSocketBridge:
|
||||
"""ZMQ web socket bridge class
|
||||
|
||||
Args:
|
||||
zmq_url: zmq url to connect to. Defaults to None.
|
||||
zmq_port: zmq port to connect to. Defaults to None.
|
||||
host: host of server. Defaults to "127.0.0.1".
|
||||
websocket_port: websocket port to connect to. Defaults to None.
|
||||
"""
|
||||
|
||||
context = zmq.Context()
|
||||
|
||||
def __init__(self, zmq_url: Optional[str] = None, host: str = "127.0.0.1", websocket_port: Optional[int] = None):
|
||||
def __init__(
|
||||
self,
|
||||
zmq_port: Optional[int] = None,
|
||||
host: str = "127.0.0.1",
|
||||
websocket_address: str = "0.0.0.0",
|
||||
websocket_port: Optional[int] = None,
|
||||
):
|
||||
self.host = host
|
||||
self.websocket_address = websocket_address
|
||||
self.websocket_pool = set()
|
||||
self.app = self.make_app()
|
||||
self.ioloop = tornado.ioloop.IOLoop.current()
|
||||
self.pcs = set()
|
||||
self.video_tracks = set()
|
||||
|
||||
if zmq_url is None:
|
||||
if zmq_port is None:
|
||||
|
||||
def f(port):
|
||||
return self.setup_zmq(f"{DEFAULT_ZMQ_METHOD:s}://{self.host:s}:{port:d}")
|
||||
|
||||
(self.zmq_socket, self.zmq_stream, self.zmq_url), _ = find_available_port(f, DEFAULT_ZMQ_PORT)
|
||||
else:
|
||||
zmq_url = f"{DEFAULT_ZMQ_METHOD:s}://{self.host:s}:{zmq_port:d}"
|
||||
self.zmq_socket, self.zmq_stream, self.zmq_url = self.setup_zmq(zmq_url)
|
||||
|
||||
listen_kwargs = {}
|
||||
listen_kwargs = {"address": self.websocket_address}
|
||||
|
||||
if websocket_port is None:
|
||||
_, self.websocket_port = find_available_port(self.app.listen, DEFAULT_WEBSOCKET_PORT, **listen_kwargs)
|
||||
@@ -196,11 +205,12 @@ class ZMQWebSocketBridge:
|
||||
self.app.listen(websocket_port, **listen_kwargs)
|
||||
self.websocket_port = websocket_port
|
||||
|
||||
self.websocket_url = f"{self.websocket_address}:{self.websocket_port}"
|
||||
self.state_tree = get_tree(StateNode)
|
||||
|
||||
def __str__(self) -> str:
|
||||
class_name = self.__class__.__name__
|
||||
return f"{class_name} using zmq_url={self.zmq_url} and websocket_port={self.websocket_port}"
|
||||
return f'{class_name} using zmq_url="{self.zmq_url}" and websocket_url="{self.websocket_url}"'
|
||||
|
||||
def make_app(self):
|
||||
"""Create a tornado application for the websocket server."""
|
||||
@@ -273,7 +283,7 @@ class ZMQWebSocketBridge:
|
||||
Args:
|
||||
websocket: websocket to send information over
|
||||
"""
|
||||
print("Sending entire scene state on websocket connect (i.e,. a page refresh).")
|
||||
print("Sending entire scene state due to websocket connection established.")
|
||||
for path, node in walk("", self.state_tree):
|
||||
if node.data is not None:
|
||||
command = {"type": "write", "path": path, "data": node.data}
|
||||
@@ -284,6 +294,19 @@ class ZMQWebSocketBridge:
|
||||
self.ioloop.start()
|
||||
|
||||
|
||||
def start_server_as_subprocess(zmq_url=None):
|
||||
"""Starts the ZMQWebSocketBridge server as a subprocess."""
|
||||
raise NotImplementedError()
|
||||
def run_viewer_bridge_server():
|
||||
"""Run the viewer bridge server"""
|
||||
parser = argparse.ArgumentParser(description="Listen for ZeroMQ commands")
|
||||
parser.add_argument("--zmq-port", "-z", type=int, nargs="?", default=6000)
|
||||
parser.add_argument("--websocket-port", "-wp", type=int, nargs="?", default=7007)
|
||||
args = parser.parse_args()
|
||||
bridge = ZMQWebSocketBridge(zmq_port=args.zmq_port, websocket_port=args.websocket_port)
|
||||
print(bridge)
|
||||
try:
|
||||
bridge.run()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_viewer_bridge_server()
|
||||
@@ -0,0 +1,56 @@
|
||||
# Copyright 2022 The Plenoptix Team. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""
|
||||
Code to use the viewer as a subprocess.
|
||||
"""
|
||||
|
||||
import atexit
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from typing import Optional
|
||||
|
||||
from nerfactory.viewer.server import server
|
||||
|
||||
|
||||
def run_viewer_bridge_server_as_subprocess(zmq_port: int, websocket_port: int, log_filename: Optional[str] = None):
|
||||
"""Runs the viewer bridge server as a subprocess.
|
||||
|
||||
Args:
|
||||
zmq_port: Port to use for the ZMQ server.
|
||||
websocket_port: Port to use for the websocket server.
|
||||
log_filename: Filename to use for the log file. Defaults to None. If None, no log file is created.
|
||||
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
args = [sys.executable, "-u", "-m", server.__name__]
|
||||
args.append("--zmq-port")
|
||||
args.append(str(zmq_port))
|
||||
args.append("--websocket-port")
|
||||
args.append(str(websocket_port))
|
||||
# supress output if no log filename is specified
|
||||
logfile = open( # pylint: disable=consider-using-with
|
||||
log_filename if log_filename else os.devnull, "w", encoding="utf8"
|
||||
)
|
||||
process = subprocess.Popen( # pylint: disable=consider-using-with
|
||||
args, stdout=logfile, stderr=logfile, start_new_session=True
|
||||
)
|
||||
|
||||
def cleanup(process):
|
||||
process.kill()
|
||||
process.wait()
|
||||
|
||||
atexit.register(cleanup, process)
|
||||
@@ -34,6 +34,7 @@ from nerfactory.models.base import Model
|
||||
from nerfactory.utils import profiler, visualization
|
||||
from nerfactory.utils.decorators import check_visualizer_enabled, decorate_all
|
||||
from nerfactory.utils.writer import GLOBAL_BUFFER, EventName
|
||||
from nerfactory.viewer.server.subprocess import run_viewer_bridge_server_as_subprocess
|
||||
from nerfactory.viewer.server.utils import get_intrinsics_matrix_and_camera_to_world_h
|
||||
from nerfactory.viewer.server.visualizer import Viewer
|
||||
|
||||
@@ -188,9 +189,29 @@ class VisualizerState:
|
||||
self.config = config
|
||||
|
||||
self.vis = None
|
||||
self.viewer_url = None
|
||||
if self.config.enable:
|
||||
zmq_url = self.config.zmq_url
|
||||
self.vis = Viewer(zmq_url=zmq_url)
|
||||
if self.config.launch_bridge_server:
|
||||
# start the viewer bridge server
|
||||
zmq_port = int(self.config.zmq_url.split(":")[-1])
|
||||
websocket_port = self.config.websocket_port
|
||||
run_viewer_bridge_server_as_subprocess(zmq_port, websocket_port)
|
||||
# TODO(ethan): move this into the writer such that it's at the bottom
|
||||
# of the logging stack and easy to see and click
|
||||
# TODO(ethan): log the output of the viewer bridge server in a file where the training logs go
|
||||
print("\n")
|
||||
self.viewer_url = (
|
||||
f"https://viewer.nerfactory.com/branch/master/?websocket_url=localhost:{websocket_port}"
|
||||
)
|
||||
viewer_url_local = f"http://localhost:4000/?websocket_url=localhost:{websocket_port}"
|
||||
pub_open_viewer_instructions_string = f'[Public] Open the viewer at "{self.viewer_url}"'
|
||||
dev_open_viewer_instructions_string = f'[Local] Open the viewer at "{viewer_url_local}"'
|
||||
print("-" * len(pub_open_viewer_instructions_string))
|
||||
print(pub_open_viewer_instructions_string)
|
||||
print(dev_open_viewer_instructions_string)
|
||||
print("-" * len(pub_open_viewer_instructions_string))
|
||||
print("\n")
|
||||
self.vis = Viewer(zmq_url=self.config.zmq_url)
|
||||
else:
|
||||
logging.info("Continuing without viewer.")
|
||||
|
||||
@@ -485,10 +506,3 @@ class VisualizerState:
|
||||
stuff_colors = graph.stuff_colors if hasattr(graph, "stuff_colors") else None
|
||||
self._send_output_to_viewer(outputs, stuff_colors=stuff_colors)
|
||||
self._update_viewer_stats(render_duration, image_height)
|
||||
|
||||
|
||||
def get_default_vis() -> Viewer:
|
||||
"""Returns the default Visualizer."""
|
||||
zmq_url = "tcp://0.0.0.0:6000"
|
||||
viewer = Viewer(zmq_url=zmq_url)
|
||||
return viewer
|
||||
Reference in new issue
Block a user