Skip to content

Commit

Permalink
fixed files so setup.py install and building with setuptools will pro…
Browse files Browse the repository at this point in the history
…perly work, moved callable files into examples folder
  • Loading branch information
JimWest committed Mar 22, 2022
1 parent 4c0e3c2 commit de7a046
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 67 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ dist/**
/.vscode
/Mefamo_MediapipeFaceMocap.egg-info
/mefamo/*.spec
/mefamo/build/mefamo
/mefamo/dist
/examples/*.spec
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ python setup.py install

## Usage

To use MeFaMo, just execute the mefamo.py file:
To use MeFaMo, just execute the mefamo_cli.py file in the examples folder:
```
python mefamo.py
python mefamo_cli.py
```

There's also an experemental GUI (which doesn't look different to the default executable, but uses kivy for future work).
Empty file added examples/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions examples/mefamo_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from mefamo import Mefamo
from argparse import ArgumentParser

if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('--input', default='0',
help='Video source. Can be an integer for webcam or a string for a video file.')
parser.add_argument('--ip', default='127.0.0.1',
help='IP address of the Unreal LiveLink server.')
parser.add_argument('--port', default=11111,
help='Port of the Unreal LiveLink server.')
parser.add_argument('--show_3d', action='store_true',
help='Show the 3d face image (projected into a 2d window')
parser.add_argument('--hide_image', action='store_true',
help='Hide the image window.')
args = parser.parse_args()

print("Starting MeFaMo")
mediapipe_face = Mefamo(args.input, args.ip, args.port, args.show_3d, args.hide_image)
mediapipe_face.start()
File renamed without changes.
1 change: 1 addition & 0 deletions mefamo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .mefamo import Mefamo
31 changes: 6 additions & 25 deletions mefamo/mefamo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import math
import transforms3d
import open3d as o3d
import argparse

from pylivelinkface import PyLiveLinkFace, FaceBlendShape

from utils.drawing import draw_landmark_point, draw_3d_face
from blendshapes.blendshape_calculator import BlendshapeCalculator
from mefamo.utils.drawing import Drawing
from mefamo.blendshapes.blendshape_calculator import BlendshapeCalculator

# taken from: https://github.com/Rassibassi/mediapipeDemos
from custom.face_geometry import ( # isort:skip
from mefamo.custom.face_geometry import ( # isort:skip
PCF,
get_metric_landmarks,
procrustes_landmark_basis,
Expand Down Expand Up @@ -185,7 +184,7 @@ def _process_image(self, image):
pose_transform_mat, metric_landmarks, rotation_vector, translation_vector = calculate_rotation(face_landmarks, self.pcf, image.shape)
# draw a 3d image of the face
if self.show_3d:
face_image_3d = draw_3d_face(metric_landmarks, image)
face_image_3d = Drawing.draw_3d_face(metric_landmarks, image)

# draw the face mesh
drawing_utils.draw_landmarks(
Expand All @@ -206,8 +205,8 @@ def _process_image(self, image):
.get_default_face_mesh_contours_style())

# draw iris points
image = draw_landmark_point(face_landmarks.landmark[468], image, color = (0, 0, 255))
image = draw_landmark_point(face_landmarks.landmark[473], image, color = (0, 255, 0))
image = Drawing.draw_landmark_point(face_landmarks.landmark[468], image, color = (0, 0, 255))
image = Drawing.draw_landmark_point(face_landmarks.landmark[473], image, color = (0, 255, 0))

# calculate and set all the blendshapes
self.blendshape_calulator.calculate_blendshapes(
Expand Down Expand Up @@ -243,21 +242,3 @@ def _process_image(self, image):
self.network_data = self.live_link_face.encode()

return True


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--input', default='0',
help='Video source. Can be an integer for webcam or a string for a video file.')
parser.add_argument('--ip', default='127.0.0.1',
help='IP address of the Unreal LiveLink server.')
parser.add_argument('--port', default=11111,
help='Port of the Unreal LiveLink server.')
parser.add_argument('--show_3d', action='store_true',
help='Show the 3d face image (projected into a 2d window')
parser.add_argument('--hide_image', action='store_true',
help='Hide the image window.')
args = parser.parse_args()

mediapipe_face = Mefamo(args.input, args.ip, args.port, args.show_3d, args.hide_image)
mediapipe_face.start()
82 changes: 42 additions & 40 deletions mefamo/utils/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@
import open3d.visualization.rendering as rendering
import cv2

def draw_landmark_point(landmark, image, color = (255, 0, 0), radius = 5):
try:
image_rows, image_cols, _ = image.shape
keypoint_px = drawing_utils._normalized_to_pixel_coordinates(landmark.x, landmark.y,
image_cols, image_rows)
center_coordinates = (int(keypoint_px[0]), int(keypoint_px[1]))
return cv2.circle(image, center_coordinates, radius, color, 2)
except Exception as e:
print(e)
return image
class Drawing():

def draw_landmark_point(landmark, image, color = (255, 0, 0), radius = 5):
try:
image_rows, image_cols, _ = image.shape
keypoint_px = drawing_utils._normalized_to_pixel_coordinates(landmark.x, landmark.y,
image_cols, image_rows)
center_coordinates = (int(keypoint_px[0]), int(keypoint_px[1]))
return cv2.circle(image, center_coordinates, radius, color, 2)
except Exception as e:
print(e)
return image

def draw_3d_face(landmarks, image):
# create pointcloid with open3d
frame_width, frame_height, channels = image.shape
try:
render = rendering.OffscreenRenderer(frame_width, frame_height)
vector = o3d.utility.Vector3dVector(landmarks[0:3].T)
pcd = o3d.geometry.PointCloud(vector)
#o3d.visualization.draw_geometries([pcd])
yellow = rendering.MaterialRecord()
yellow.base_color = [1.0, 0.75, 0.0, 1.0]
yellow.shader = "defaultLit"
render.scene.add_geometry("pcd", pcd, yellow)
# Optionally set the camera field of view (to zoom in a bit)
vertical_field_of_view = 15.0 # between 5 and 90 degrees
aspect_ratio = frame_width / frame_height # azimuth over elevation
near_plane = 0.1
far_plane = 150
fov_type = o3d.visualization.rendering.Camera.FovType.Vertical
render.scene.camera.set_projection(vertical_field_of_view, aspect_ratio, near_plane, far_plane, fov_type)
def draw_3d_face(landmarks, image):
# create pointcloid with open3d
frame_width, frame_height, channels = image.shape
try:
render = rendering.OffscreenRenderer(frame_width, frame_height)
vector = o3d.utility.Vector3dVector(landmarks[0:3].T)
pcd = o3d.geometry.PointCloud(vector)
#o3d.visualization.draw_geometries([pcd])
yellow = rendering.MaterialRecord()
yellow.base_color = [1.0, 0.75, 0.0, 1.0]
yellow.shader = "defaultLit"
render.scene.add_geometry("pcd", pcd, yellow)
# Optionally set the camera field of view (to zoom in a bit)
vertical_field_of_view = 15.0 # between 5 and 90 degrees
aspect_ratio = frame_width / frame_height # azimuth over elevation
near_plane = 0.1
far_plane = 150
fov_type = o3d.visualization.rendering.Camera.FovType.Vertical
render.scene.camera.set_projection(vertical_field_of_view, aspect_ratio, near_plane, far_plane, fov_type)

# Look at the origin from the front (along the -Z direction, into the screen), with Y as Up.
center = [0, 0, 0] # look_at target
eye = [0, 0, 80] # camera position
up = [0, 1, 0] # camera orientation
render.scene.camera.look_at(center, eye, up)
render.scene.set_background([0, 0, 0, 0])
# Look at the origin from the front (along the -Z direction, into the screen), with Y as Up.
center = [0, 0, 0] # look_at target
eye = [0, 0, 80] # camera position
up = [0, 1, 0] # camera orientation
render.scene.camera.look_at(center, eye, up)
render.scene.set_background([0, 0, 0, 0])

img = render.render_to_image()
return img
except Exception as e:
print(e)

img = render.render_to_image()
return img
except Exception as e:
print(e)

0 comments on commit de7a046

Please sign in to comment.