|
| 1 | +# Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import print_function |
| 15 | + |
| 16 | +import argparse |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import sys |
| 20 | + |
| 21 | +from flask import Flask, render_template |
| 22 | +from tornado.wsgi import WSGIContainer |
| 23 | +from tornado.web import Application, FallbackHandler |
| 24 | +from tornado.ioloop import IOLoop |
| 25 | + |
| 26 | +from kaolin.experimental.dash3d.util import StreamingGeometryHelper, GeometryWebSocketHandler |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +def create_server(logdir): |
| 32 | + """ Create the server, including websocket handler through tornado, and flask http server. |
| 33 | +
|
| 34 | + Args: |
| 35 | + logdir (str): directory where Timelapse data is written. |
| 36 | + """ |
| 37 | + # Helper for streaming geometry from the logdir |
| 38 | + helper = StreamingGeometryHelper(logdir) |
| 39 | + |
| 40 | + # Flask for HTTP |
| 41 | + _base_dir = os.path.dirname(__file__) |
| 42 | + _template_dir = os.path.join(_base_dir, 'templates') |
| 43 | + _static_dir = os.path.join(_base_dir, 'static') |
| 44 | + app = Flask('kaolin_dash3d', |
| 45 | + template_folder=_template_dir, |
| 46 | + static_url_path='/static', |
| 47 | + static_folder=_static_dir) |
| 48 | + |
| 49 | + @app.route('/') |
| 50 | + def index(): |
| 51 | + helper.parser.check_for_updates() |
| 52 | + return render_template('home.html', logdir=helper.logdir, |
| 53 | + nmeshes=helper.parser.num_mesh_categories(), |
| 54 | + npointclouds=helper.parser.num_pointcloud_categories()) |
| 55 | + |
| 56 | + # Tornado server to handle websockets |
| 57 | + container = WSGIContainer(app) |
| 58 | + server = Application([ |
| 59 | + (r'/websocket/', GeometryWebSocketHandler, dict(helper=helper)), |
| 60 | + (r'.*', FallbackHandler, dict(fallback=container)) |
| 61 | + ]) |
| 62 | + return server |
| 63 | + |
| 64 | + |
| 65 | +def run_main(): |
| 66 | + aparser = argparse.ArgumentParser( |
| 67 | + description='NVIDIA Kaolin Tensorboard 3d visualizer for USD files generated during training.') |
| 68 | + aparser.add_argument('--logdir', action='store', type=str, required=True, |
| 69 | + help='The vis folder generated by the Timelapse module.') |
| 70 | + aparser.add_argument('--log_level', action='store', type=int, default=logging.INFO, |
| 71 | + help='Logging level, DEBUG: 10, INFO: 20, WARN: 30, ERROR: 40.') |
| 72 | + aparser.add_argument('--port', action='store', default=8080) |
| 73 | + args = aparser.parse_args() |
| 74 | + |
| 75 | + logging.basicConfig(level=args.log_level, |
| 76 | + format='%(asctime)s|%(levelname)8s|%(name)15s| %(message)s', |
| 77 | + handlers=[logging.StreamHandler(sys.stdout)]) |
| 78 | + |
| 79 | + print(f'Dash3D server starting. Go to: http://localhost:{args.port}') |
| 80 | + server = create_server(args.logdir) |
| 81 | + server.listen(args.port) |
| 82 | + IOLoop.instance().start() |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + run_main() |
0 commit comments