Virtual Reality with Python

Virtual Reality is a term used for computer-generated 3D environments that allow the user to enter and interact with alternate realities.

The users are able to immerse themselves to varying degrees in the computer’s artificial world which may either be simulation of some form of reality or the simulation of complex data.

Creating a 3D Virtual Reality with Python

In this article I will create a 3D virtual reality car using the visualization techniques of python and Machine Learning.

import as cm
def tri_indices(simplices):
    return ([triplet[c] for triplet in simplices] for c in range(3))

def plotly_trisurf(x, y, z, simplices, colormap=cm.RdBu, plot_edges=None):

    tri_vertices=map(lambda index: points3D[index], simplices)
    zmean=[np.mean(tri[:,2]) for tri in tri_vertices ]
    facecolor=[map_z2color(zz,  colormap, min_zmean, max_zmean) for zz in zmean]

    triangles=go.Mesh3d(x=x, y=y, z=z,
                     i=I, j=J, k=K,

    if plot_edges is None: return [triangles]
        lists_coord=[[[T[k%3][c] for k in range(4)]+[ None]   for T in tri_vertices]  for c in range(3)]
        Xe, Ye, Ze=[reduce(lambda x,y: x+y, lists_coord[k]) for k in range(3)]

        lines=go.Scatter3d(x=Xe, y=Ye, z=Ze,
                        line=dict(color= 'rgb(50,50,50)', width=1.5))
        return [triangles, lines]
def map_z2color(zval, colormap, vmin, vmax):
    if vmin>vmax: raise ValueError('incorrect relation between vmin and vmax')
    t=(zval-vmin)/float((vmax-vmin))#normalize val
    R, G, B, alpha=colormap(t)
    return 'rgb('+'{:d}'.format(int(R*255+0.5))+','+'{:d}'.format(int(G*255+0.5))+\
import json
import numpy as np
import matplotlib.pyplot as plt
import plotly.figure_factory as FF
import plotly.graph_objs as go

import numpy as np
from google.colab import files
uploaded = files.upload()

with open('036-CAR01.json') as json_file:
    data = json.load(json_file)
    vertices, triangles = np.array(data['vertices']), np.array(data['faces']) - 1
    x, y, z = vertices[:,0], vertices[:,2], -vertices[:,1]
    car_type = data['car_type']
    graph_data = plotly_trisurf(x,y,z, triangles, colormap=cm.RdBu, plot_edges=None)

    # with no axis
    # with axis
    axis = dict(
        backgroundcolor="rgb(230, 230,230)",
        gridcolor="rgb(255, 255, 255)",
        zerolinecolor="rgb(255, 255, 255)",
    layout = go.Layout(
         title=car_type + ' with noaxis',
         width=800, height=600,
             xaxis=dict(noaxis), yaxis=dict(noaxis), zaxis=dict(noaxis),
#              aspectratio=dict( x=1, y=2, z=0.5)

    fig = go.Figure(data= graph_data, layout=layout)
    layout = go.Layout(
         title=car_type + ' with axis', 
         width=800, height=600,
             xaxis=dict(axis), yaxis=dict(axis), zaxis=dict(axis),
#              aspectratio=dict( x=1, y=2, z=0.5)

    fig = go.Figure(data= graph_data, layout=layout)
Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply

Data Science | Machine Learning | Python | C++ | Coding | Programming | JavaScript