add docker compose

master
yutent 2023-09-17 22:17:07 +08:00
commit ac4077ef52
15 changed files with 168405 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
FROM debian:bookworm
RUN rm /etc/apt/sources.list.d/debian.sources
RUN echo "deb http://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list
RUN echo "deb http://mirrors.ustc.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list
RUN apt update
RUN apt install -y python3-full python3-opengl libosmesa6
# 创建虚拟python环境, 要用pip安装依赖, py3之后, 要求在虚拟环境中才能使用pip
RUN mkdir /opt/py
RUN python3 -m venv /opt/py
RUN /opt/py/bin/pip install numpy trimesh pyglet pyrender matplotlib
# 要升级pyopengl到3.1.4或以上
# 否则会报错 ImportError: cannot import name 'OSMesaCreateContextAttribs' from 'OpenGL.osmesa'
RUN /opt/py/bin/pip install --upgrade pyopengl
ENV PYOPENGL_PLATFORM=osmesa

12
docker-compose.yml Normal file
View File

@ -0,0 +1,12 @@
version: "3"
services:
osmesa:
build: .
stdin_open: true
tty: true
volumes:
- .:/app
working_dir: /app
command: /opt/py/bin/python example.py

13
duck.py Normal file
View File

@ -0,0 +1,13 @@
from pyrender import Mesh, Scene, Viewer
from io import BytesIO
import numpy as np
import trimesh
import requests
duck_source = "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Duck/glTF-Binary/Duck.glb"
duck = trimesh.load(BytesIO(requests.get(duck_source).content), file_type='glb')
duckmesh = Mesh.from_trimesh(list(duck.geometry.values())[0])
scene = Scene(ambient_light=np.array([1.0, 1.0, 1.0, 1.0]))
scene.add(duckmesh)
Viewer(scene)

160
example.py Normal file
View File

@ -0,0 +1,160 @@
"""Examples of using pyrender for viewing and offscreen rendering.
"""
import os
os.environ["PYOPENGL_PLATFORM"] = "osmesa"
#os.environ["MUJOCO_GL"] = "osmesa"
import pyglet
pyglet.options['shadow_window'] = False
import numpy as np
import trimesh
from pyrender import PerspectiveCamera,\
DirectionalLight, SpotLight, PointLight,\
MetallicRoughnessMaterial,\
Primitive, Mesh, Node, Scene,\
Viewer, OffscreenRenderer, RenderFlags
#==============================================================================
# Mesh creation
#==============================================================================
#------------------------------------------------------------------------------
# Creating textured meshes from trimeshes
#------------------------------------------------------------------------------
# Fuze trimesh
fuze_trimesh = trimesh.load('./models/fuze.obj')
fuze_mesh = Mesh.from_trimesh(fuze_trimesh)
# Drill trimesh
drill_trimesh = trimesh.load('./models/drill.obj')
drill_mesh = Mesh.from_trimesh(drill_trimesh)
drill_pose = np.eye(4)
drill_pose[0,3] = 0.1
drill_pose[2,3] = -np.min(drill_trimesh.vertices[:,2])
# Wood trimesh
wood_trimesh = trimesh.load('./models/wood.obj')
wood_mesh = Mesh.from_trimesh(wood_trimesh)
# Water bottle trimesh
bottle_gltf = trimesh.load('./models/WaterBottle.glb')
bottle_trimesh = bottle_gltf.geometry[list(bottle_gltf.geometry.keys())[0]]
bottle_mesh = Mesh.from_trimesh(bottle_trimesh)
bottle_pose = np.array([
[1.0, 0.0, 0.0, 0.1],
[0.0, 0.0, -1.0, -0.16],
[0.0, 1.0, 0.0, 0.13],
[0.0, 0.0, 0.0, 1.0],
])
#------------------------------------------------------------------------------
# Creating meshes with per-vertex colors
#------------------------------------------------------------------------------
boxv_trimesh = trimesh.creation.box(extents=0.1*np.ones(3))
boxv_vertex_colors = np.random.uniform(size=(boxv_trimesh.vertices.shape))
boxv_trimesh.visual.vertex_colors = boxv_vertex_colors
boxv_mesh = Mesh.from_trimesh(boxv_trimesh, smooth=False)
#------------------------------------------------------------------------------
# Creating meshes with per-face colors
#------------------------------------------------------------------------------
boxf_trimesh = trimesh.creation.box(extents=0.1*np.ones(3))
boxf_face_colors = np.random.uniform(size=boxf_trimesh.faces.shape)
boxf_trimesh.visual.face_colors = boxf_face_colors
boxf_mesh = Mesh.from_trimesh(boxf_trimesh, smooth=False)
#------------------------------------------------------------------------------
# Creating meshes from point clouds
#------------------------------------------------------------------------------
points = trimesh.creation.icosphere(radius=0.05).vertices
point_colors = np.random.uniform(size=points.shape)
points_mesh = Mesh.from_points(points, colors=point_colors)
#==============================================================================
# Light creation
#==============================================================================
direc_l = DirectionalLight(color=np.ones(3), intensity=1.0)
spot_l = SpotLight(color=np.ones(3), intensity=10.0,
innerConeAngle=np.pi/16, outerConeAngle=np.pi/6)
point_l = PointLight(color=np.ones(3), intensity=10.0)
#==============================================================================
# Camera creation
#==============================================================================
cam = PerspectiveCamera(yfov=(np.pi / 3.0))
cam_pose = np.array([
[0.0, -np.sqrt(2)/2, np.sqrt(2)/2, 0.5],
[1.0, 0.0, 0.0, 0.0],
[0.0, np.sqrt(2)/2, np.sqrt(2)/2, 0.4],
[0.0, 0.0, 0.0, 1.0]
])
#==============================================================================
# Scene creation
#==============================================================================
scene = Scene(ambient_light=np.array([0.02, 0.02, 0.02, 1.0]))
#==============================================================================
# Adding objects to the scene
#==============================================================================
#------------------------------------------------------------------------------
# By manually creating nodes
#------------------------------------------------------------------------------
fuze_node = Node(mesh=fuze_mesh, translation=np.array([0.1, 0.15, -np.min(fuze_trimesh.vertices[:,2])]))
scene.add_node(fuze_node)
boxv_node = Node(mesh=boxv_mesh, translation=np.array([-0.1, 0.10, 0.05]))
scene.add_node(boxv_node)
boxf_node = Node(mesh=boxf_mesh, translation=np.array([-0.1, -0.10, 0.05]))
scene.add_node(boxf_node)
#------------------------------------------------------------------------------
# By using the add() utility function
#------------------------------------------------------------------------------
drill_node = scene.add(drill_mesh, pose=drill_pose)
bottle_node = scene.add(bottle_mesh, pose=bottle_pose)
wood_node = scene.add(wood_mesh)
direc_l_node = scene.add(direc_l, pose=cam_pose)
spot_l_node = scene.add(spot_l, pose=cam_pose)
#==============================================================================
# Using the viewer with a default camera
#==============================================================================
# v = Viewer(scene, shadows=True)
#==============================================================================
# Using the viewer with a pre-specified camera
#==============================================================================
cam_node = scene.add(cam, pose=cam_pose)
# v = Viewer(scene, central_node=drill_node)
#==============================================================================
# Rendering offscreen from that camera
#==============================================================================
r = OffscreenRenderer(viewport_width=640*2, viewport_height=480*2)
color, depth = r.render(scene)
import matplotlib.pyplot as plt
plt.figure()
plt.imshow(color)
plt.show()
#==============================================================================
# Segmask rendering
#==============================================================================
# nm = {node: 20*(i + 1) for i, node in enumerate(scene.mesh_nodes)}
# seg = r.render(scene, RenderFlags.SEG, nm)[0]
# plt.figure()
# plt.imshow(seg)
# plt.show()
#
# r.delete()

BIN
models/WaterBottle.glb Normal file

Binary file not shown.

165473
models/drill.obj Normal file

File diff suppressed because it is too large Load Diff

4
models/drill.obj.mtl Normal file
View File

@ -0,0 +1,4 @@
newmtl material_0
# shader_type beckmann
map_Kd drill_uv.png

BIN
models/drill_uv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 MiB

2688
models/fuze.obj Normal file

File diff suppressed because it is too large Load Diff

14
models/fuze.obj.mtl Normal file
View File

@ -0,0 +1,14 @@
#
# Wavefront material file
# Converted by Meshlab Group
#
newmtl material_0
Ka 0.200000 0.200000 0.200000
Kd 1.000000 1.000000 1.000000
Ks 1.000000 1.000000 1.000000
Tr 1.000000
illum 2
Ns 0.000000
map_Kd fuze_uv.jpg

BIN
models/fuze_uv.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

18
models/wood.obj Normal file
View File

@ -0,0 +1,18 @@
mtllib ./wood.obj.mtl
v -0.300000 -0.300000 0.000000
v 0.300000 -0.300000 0.000000
v -0.300000 0.300000 0.000000
v 0.300000 0.300000 0.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
usemtl material_0
f 1/1/1 2/2/2 4/4/4
f 1/1/1 4/4/4 3/3/3

2
models/wood.obj.mtl Normal file
View File

@ -0,0 +1,2 @@
newmtl material_0
map_Kd wood_uv.png

BIN
models/wood_uv.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 MiB