top of page
Search

# SIMULATION OF 3D HUMAN IMAGE FROM VIDEO CONTAINING HUMAN FACE

Updated: Jul 31, 2021

3D modeling has changed the presentation world of architecture designs. It's like pulling out the multi-dimensional images from your brain and painting them on a paper to see how it looks. The impact 3D modeling has, on presenting architectural services is undoubtedly the most transforming things to have happened.

A 3D Image can speak Thousand Words, unlike 2D We can visualize the architecture of the work in every possible aspect. We can get all the possible insights of the work from all possible angle and very cost efficient as compared to 2D.

Now A days 3D is becoming very popular because of its flexibility and availability. Currently we are using 3D simulation in various fields like augmented reality based gaming, Bioinformatics for watching the Interactions of genes, proteins etc. more closely and various angles and in many more fields.

So, Now our Work Simulation of 3D human image from Image is very useful for the 3D printing of Human Images and can be widely used in cinematic fields and medicine fields. So, Let’s Begin. First lets understand how a basic 3D works using motion of an electron around nucleus.

## CODE:

```#Importing required Libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

# ANIMATION FUNCTION
def func(num, dataSet, line, redDots):
# NOTE: there is no .set_data() for 3 dim data...
line.set_data(dataSet[0:2, :num])
line.set_3d_properties(dataSet[2, :num])
redDots.set_data(dataSet[0:2, :num])
redDots.set_3d_properties(dataSet[2, :num])
return line

# THE DATA POINTS
t = np.arange(0, 20, 0.2) # This would be the z-axis ('t' means time here) x = np.cos(t) - 1
y = 1 / 2 * (np.cos(2 * t) - 1)
dataSet = np.array([x, y, t])
numDataPoints = len(t)

# GET SOME MATPLOTLIB OBJECTS
fig = plt.figure()
ax = Axes3D(fig)
redDots = plt.plot(dataSet, dataSet, dataSet, lw=2, c='r', marker='o') # For scatter plot
# NOTE: Can't pass empty arrays into 3d version of plot()
line = plt.plot(dataSet, dataSet, dataSet, lw=2, c='g') # For line plot

# AXES PROPERTIES]
# ax.set_xlim3d([limit0, limit1])
ax.set_xlabel('X(t)')
ax.set_ylabel('Y(t)')
ax.set_zlabel('time')
ax.set_title('Trajectory of electron for E vector along ')

# Creating the Animation object
line_ani = animation.FuncAnimation(fig, func, frames=numDataPoints, fargs=(dataSet, line, redDots), interval=50,
blit=False)

# line_ani.save(r'Animation.mp4')
plt.show()

``` ### Figure-1

Now Let’s go for our work.

1. Python

2. Blender

## Code For Capturing Human Face Video:

```#Import Required Packages
import cv2
import numpy as np
face_classifier =
It is an Object Detection Algorithm used to identify faces in an image or a real time video. The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper “Rapid Object Detection using a Boosted Cascade of Simple Features” published in 2001.

def face_extractor(img):
# Function detects faces and returns the cropped face
# If no face detected, it returns the input image

# gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(img, 1.3, 5)

if faces is ():
return None

# Crop all faces found
for (x, y, w, h) in faces:
x = x - 10
y = y - 10
cropped_face = img[y:y + h + 50, x:x + w + 50]

return cropped_face

# Initialize Webcam
cap = cv2.VideoCapture(0)
count = 0

# Collect 100 samples of your face from webcam input
while True:

if
face_extractor(frame) is not None:
count += 1
face = cv2.resize(face_extractor(frame), (400, 400))

file_name_path = './Amma/' + str(count) + '.jpg'
cv2.imwrite(file_name_path, face)

# Put count on images and display live count
cv2.putText(face, str(count), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Face Cropper', face)
else:
pass

if
cv2.waitKey(1) == 13 or count == 100: # 13 is the Enter Key
break

cap.release()
cv2.destroyAllWindows()
print("Collecting Samples Complete") ```

After Extracting 2D Images from Video Now Let’s Start Simulating 3D Model of our Image:

## Step 1:

Generating .stl format files from 2D Images Using python

```Code:

!pip3 install numpy-stl

import numpy as np
from stl import mesh

# Define the 8 vertices of the cube
vertices = np.array([\
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1],
[-1, -1, +1],
[+1, -1, +1],
[+1, +1, +1],
[-1, +1, +1]])
# Define the 12 triangles composing the cube
faces = np.array([\
[0,3,1],
[1,3,2],
[0,4,7],
[0,7,3],
[4,5,6],
[4,6,7],
[5,1,2],
[5,2,6],
[2,3,6],
[3,7,6],
[0,1,5],
[0,5,4]])

# Create the mesh
mesh1 = mesh.Mesh(np.zeros(faces.shape, dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
print(vertices[f[j],:])
mesh1.vectors[i][j] = vertices[f[j]]

# Write the mesh to file "cube.stl"
mesh1.save('mesh.stl')

```

## Take an Input Image:

```
from PIL import Image
import matplotlib.pyplot as plt
im = Image.open("/content/IMG_20200905_201503.jpg")
plt.imshow(im) ```

## Convert Image into greyscale:

```grey_img = Image.open('/content/IMG_20200905_201503.jpg').convert('L') plt.imshow(grey_img)
Create 2D square surface with 2 triangles:
import numpy as np
from stl import mesh

# Define the 8 vertices of the cube
vertices = np.array([\
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1]])

# Define the 12 triangles composing the cube
faces = np.array([\
[1,2,3],
[3,1,0]
])

# Create the mesh
cube = mesh.Mesh(np.zeros(faces.shape, dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j],:]

# Write the mesh to file "cube.stl"
cube.save('surface.stl')

grey_img = Image.open('/content/IMG_20200905_201503.jpg').convert('L')

max_size=(500,500)
max_height=10
min_height=0

#height=0 for minPix
#height=maxHeight for maxPIx

grey_img.thumbnail(max_size)
imageNp = np.array(grey_img)
maxPix=imageNp.max()
minPix=imageNp.min()

print(imageNp)
(ncols,nrows)=grey_img.size

vertices=np.zeros((nrows,ncols,3))

for x in range(0, ncols):
for y in range(0, nrows):
pixelIntensity = imageNp[y][x]
z = (pixelIntensity * max_height) / maxPix
#print(imageNp[y][x])
vertices[y][x]=(x, y, z)

faces=[]

for x in range(0, ncols - 1):
for y in range(0, nrows - 1):
# create face 1
vertice1 = vertices[y][x]
vertice2 = vertices[y+1][x]
vertice3 = vertices[y+1][x+1]
face1 = np.array([vertice1,vertice2,vertice3])

# create face 2
vertice1 = vertices[y][x]
vertice2 = vertices[y][x+1]
vertice3 = vertices[y+1][x+1]

face2 = np.array([vertice1,vertice2,vertice3])
print(f"number of faces: {len(faces)}")
facesNp = np.array(faces)
# Create the mesh
surface = mesh.Mesh(np.zeros(facesNp.shape, dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
surface.vectors[i][j] = facesNp[i][j]

# Write the mesh to file "cube.stl"
surface.save('surface.stl')
print(surface) ```

## Surface.stl file in blender: 