Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: ('startswith first arg must be bytes or a tuple of bytes, not str', (1, array([0], dtype=uint32))) #109

Open
Frank-Dz opened this issue Apr 17, 2020 · 9 comments

Comments

@Frank-Dz
Copy link

Frank-Dz commented Apr 17, 2020

Hi~
When I used pyrender to generate some results, I met some errors.
My environment is: Ubuntu 16.04, Python3.7, cuda10.0 and with PyOpenGL PyOpenGL_accelerate installed.

import math
import trimesh
import pyrender
import numpy as np
from pyrender.constants import RenderFlags
from lib.models.spin import get_smpl_faces


class WeakPerspectiveCamera(pyrender.Camera):
    def __init__(self,
                 scale,
                 translation,
                 znear=pyrender.camera.DEFAULT_Z_NEAR,
                 zfar=None,
                 name=None):
        super(WeakPerspectiveCamera, self).__init__(
            znear=znear,
            zfar=zfar,
            name=name,
        )
        self.scale = scale
        self.translation = translation

    def get_projection_matrix(self, width=None, height=None):
        P = np.eye(4)
        P[0, 0] = self.scale[0]
        P[1, 1] = self.scale[1]
        P[0, 3] = self.translation[0] * self.scale[0]
        P[1, 3] = -self.translation[1] * self.scale[1]
        P[2, 2] = -1
        return P


class Renderer():
    def __init__(self, resolution=(224,224), orig_img=False, wireframe=False):
        self.resolution = resolution

        self.faces = get_smpl_faces()
        self.orig_img = orig_img
        self.wireframe = wireframe
        self.renderer = pyrender.OffscreenRenderer(
            viewport_width=self.resolution[0],
            viewport_height=self.resolution[1],
            point_size=1.0
        )

        # set the scene
        self.scene = pyrender.Scene(bg_color=[0.0, 0.0, 0.0, 0.0], ambient_light=(0.3, 0.3, 0.3))

        light = pyrender.PointLight(color=[1.0, 1.0, 1.0], intensity=1)

        light_pose = np.eye(4)
        light_pose[:3, 3] = [0, -1, 1]
        self.scene.add(light, pose=light_pose)

        light_pose[:3, 3] = [0, 1, 1]
        self.scene.add(light, pose=light_pose)

        light_pose[:3, 3] = [1, 1, 2]
        self.scene.add(light, pose=light_pose)

    def render(self, img, verts, cam, angle=None, axis=None, mesh_filename=None, color=[1.0, 1.0, 0.9]):

        mesh = trimesh.Trimesh(vertices=verts, faces=self.faces)

        Rx = trimesh.transformations.rotation_matrix(math.radians(180), [1, 0, 0])
        mesh.apply_transform(Rx)

        if mesh_filename is not None:
            mesh.export(mesh_filename)

        if angle and axis:
            R = trimesh.transformations.rotation_matrix(math.radians(angle), axis)
            mesh.apply_transform(R)

        sx, sy, tx, ty = cam

        camera = WeakPerspectiveCamera(
            scale=[sx, sy],
            translation=[tx, ty],
            zfar=1000.
        )

        material = pyrender.MetallicRoughnessMaterial(
            metallicFactor=0.0,
            alphaMode='OPAQUE',
            baseColorFactor=(color[0], color[1], color[2], 1.0)
        )
        mesh = pyrender.Mesh.from_trimesh(mesh, material=material)
        mesh_node = self.scene.add(mesh, 'mesh')
        camera_pose = np.eye(4)
        cam_node = self.scene.add(camera, pose=camera_pose)
        if self.wireframe:
            render_flags = RenderFlags.RGBA | RenderFlags.ALL_WIREFRAME
        else:
            render_flags = RenderFlags.RGBA
        rgb, _ = self.renderer.render(self.scene, flags=render_flags)
        valid_mask = (rgb[:, :, -1] > 0)[:, :, np.newaxis]
        output_img = rgb[:, :, :-1] * valid_mask + (1 - valid_mask) * img
        image = output_img.astype(np.uint8)
        self.scene.remove_node(mesh_node)
        self.scene.remove_node(cam_node)
        return image

But I got the following error:

 File "/home/frank/PycharmProjects/VIBE/lib/utils/renderer.py", line 118, in render
    rgb, _ = self.renderer.render(self.scene, flags=render_flags)
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/pyrender/offscreen.py", line 99, in render
    return self._renderer.render(scene, flags)
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/pyrender/renderer.py", line 121, in render
    self._update_context(scene, flags)
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/pyrender/renderer.py", line 709, in _update_context
    p._add_to_context()
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/pyrender/primitive.py", line 324, in _add_to_context
    self._vaid = glGenVertexArrays(1)
  File "src/latebind.pyx", line 39, in OpenGL_accelerate.latebind.LateBind.__call__
  File "src/wrapper.pyx", line 314, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "src/wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/OpenGL/platform/baseplatform.py", line 401, in __call__
    if self.load():
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/OpenGL/platform/baseplatform.py", line 390, in load
    error_checker = self.error_checker,
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/OpenGL/platform/baseplatform.py", line 148, in constructFunction
    if (not is_core) and not self.checkExtension( extension ):
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/OpenGL/platform/baseplatform.py", line 270, in checkExtension
    result = extensions.ExtensionQuerier.hasExtension( name )
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/OpenGL/extensions.py", line 98, in hasExtension
    result = registered( specifier )
  File "/home/frank/PycharmProjects/VIBE/venv/lib/python3.7/site-packages/OpenGL/extensions.py", line 105, in __call__
    if not specifier.startswith( self.prefix ):
TypeError: ('startswith first arg must be bytes or a tuple of bytes, not str', (1, array([0], dtype=uint32)))

The error begin with

        rgb, _ = self.renderer.render(self.scene, flags=render_flags)

Anyone can help?
Thanks in advance!

@melih-unsal
Copy link

Have you solved the problem? I have also faced this problem.

@Frank-Dz
Copy link
Author

Have you solved the problem? I have also faced this problem.

No. Haven't.

Anyone can help?

@maxerbubba
Copy link

Upgrading to PyOpenGL==3.1.5 helped

@Liamkuo
Copy link

Liamkuo commented Apr 17, 2022

I fixed it with this

@yaokxx
Copy link

yaokxx commented Apr 19, 2022

I fixed it with this

It still do not work under my condition LOL, is there other solution? Or just my misunderstanding that i just change one line in the source code of egl.py, just replace 'GL' with 'OpenGL'?

@HaoZhang990127
Copy link

HaoZhang990127 commented Jan 28, 2023

change os.environ['PYOPENGL_PLATFORM'] = 'osmesa' to os.environ['PYOPENGL_PLATFORM'] = 'egl'
may solve this problem

@imabackstabber
Copy link

I fixed it with this

This works for me when i was trying to visualize human mesh.

@ChristianMilianti
Copy link

ChristianMilianti commented Aug 24, 2023

Upgrading to PyOpenGL==3.1.5 helped

This fixed it for me too, thank you!

@leo-frank
Copy link

I fixed it with this

works for me !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants