Skip to content
Snippets Groups Projects
Commit 571a15af authored by Olivier Bertrand's avatar Olivier Bertrand
Browse files

Update rendering example to avoid duplicate code

parent 090816f8
No related branches found
No related tags found
No related merge requests found
......@@ -4,12 +4,39 @@ Run several blender instances if blender is installed
import pkg_resources
import glob
import os
import shutil
import sys
import inspect
import tempfile
blendercomand = 'blender'
def activate_virtualenv(venv_path):
if venv_path is None:
raise NameError('Python is not running within a virtualenv')
filepath = os.path.join(venv_path, 'bin', 'activate_this.py')
with open(filepath, 'r') as f:
exec(f.read(), dict(__file__=filepath))
venv = sys.base_prefix
print(venv)
blendercomand = shutil.which('blender')
worldfile = pkg_resources.resource_filename(
'navipy', 'resources/forest_world.blend')
encoding = 'utf-8'
for demofile in glob.iglob('**/blenddemo*.py', recursive=True):
command = '{} {} --background --python {}'.format(blendercomand,
worldfile,
demofile)
os.system(command)
with tempfile.NamedTemporaryFile() as tfile:
tfile.write('import os \n'.encode(encoding))
for line in inspect.getsourcelines(activate_virtualenv)[0]:
tfile.write(line.encode(encoding))
line = 'activate_virtualenv(\"{}\")\n'.format(venv)
tfile.write(line.encode(encoding))
with open(demofile) as infile:
for line in infile:
tfile.write(line.encode(encoding))
tfile.seek(0)
print(tfile.read())
tfile.seek(0)
command = '{} {} --background --python {}'.format(
blendercomand, worldfile, tfile.name)
os.system(command)
"""
Example on how to use the rendering module
"""
# Import module from blender
import os
import tempfile
environment_name = 'toolbox-navigation'
def activate_virtualenv(name):
"""Activate given virtualenv.
Virtualenv home folder is given by environment variable
``WORKON_HOME`` or ``~/Envs`.
"""
if 'WORKON_HOME' in os.environ:
home = os.environ['WORKON_HOME']
else:
home = os.path.expanduser(os.path.join('~', 'Envs'))
filepath = os.path.join(home, name, 'bin', 'activate_this.py')
with open(filepath, 'r') as f:
exec(f.read(), dict(__file__=filepath))
activate_virtualenv(environment_name)
#
# Import module in virtualenv
#
import numpy as np # noqa: E402
from navipy.rendering.bee_sampling import BeeSampling # noqa: E402
......
import os
proci = 0
nproc = 1
def activate_virtualenv(name):
"""Activate given virtualenv.
Virtualenv home folder is given by environment variable ``WORKON_HOME`` or
``~/Envs`.
"""
if 'WORKON_HOME' in os.environ:
home = os.environ['WORKON_HOME']
else:
home = os.path.expanduser(os.path.join('~', 'Envs'))
filepath = os.path.join(home, name, 'bin', 'activate_this.py')
with open(filepath, 'r') as f:
exec(f.read(), dict(__file__=filepath))
activate_virtualenv('toolbox-navigation')
#
# Import module in virtualenv
#
import numpy as np # noqa: E402
from matplotlib.colors import hsv_to_rgb, rgb_to_hsv # noqa: E402
import matplotlib.pyplot as plt # noqa: E402
from navipy.rendering.cyber_bee import Cyberbee # noqa: E402
import numpy as np
from matplotlib.colors import hsv_to_rgb, rgb_to_hsv
import matplotlib.pyplot as plt
from navipy.rendering.cyber_bee import Cyberbee
# with tempfile.TemporaryDirectory() as folder:
cyberbee = Cyberbee()
......
......@@ -39,14 +39,12 @@ Custom sampling
Rendering classes
-----------------
BeeSampling
~~~~~~~~~~~
.. autoclass:: navipy.rendering.bee_sampling.BeeSampling
:members:
Cyberbee
~~~~~~~~
.. autoclass:: navipy.rendering.cyber_bee.Cyberbee
:members:
......
......@@ -158,7 +158,7 @@ harddrive space, as each image is composed of 4 channels of 180x360 pixels.
if np.any(np.isnan(posorient)):
# warnings.warn('frame_i: {} posorient nans'.format(frame_i))
continue
rowid = dataloger.posid(posorient)
rowid = dataloger.get_posid(posorient)
if dataloger.check_data_validity(rowid):
warnings.warn(
'frame_i: {} data is valid rowid {}'.format(frame_i,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment