First version of the script.

This commit is contained in:
Nathan Vegdahl 2022-05-30 18:32:31 -07:00
commit b663b6d495
3 changed files with 93 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.mdd
*.blend
*.blend?
videos

63
face_track.py Executable file
View File

@ -0,0 +1,63 @@
#!/usr/bin/env python3
from struct import pack
import numpy as np
import cv2 as cv
import mediapipe as mp
if __name__ == "__main__":
mp_face_mesh = mp.solutions.face_mesh
# Compute the mesh points. One mesh per frame.
meshes = []
point_count = 0
aspect_ratio = 1.0
with mp_face_mesh.FaceMesh(
static_image_mode=False, # Set false for video.
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5) as face_mesh:
i = 1
cap = cv.VideoCapture("videos/BDGFACE 5_12.mp4")
while cap.isOpened():
print("\rReading frame", i, end = "")
ret, image = cap.read()
if not ret:
break
aspect_ratio = image.shape[1] / image.shape[0]
results = face_mesh.process(cv.cvtColor(image, cv.COLOR_BGR2RGB))
if not results.multi_face_landmarks:
meshes += [None]
else:
point_count = len(results.multi_face_landmarks[0].landmark)
meshes += [results.multi_face_landmarks[0].landmark]
i += 1
cap.release()
print("\rRead {} frames. ".format(len(meshes)))
# Write the mdd file.
fps = 24.0 # TODO: get this from the video file.
frame_count = len(meshes)
if frame_count > 0:
with open("test.mdd", 'wb') as mdd:
print("Vert count:", point_count)
mdd.write(pack(">2i", frame_count, point_count))
mdd.write(pack(">%df" % (frame_count), *[frame / fps for frame in range(frame_count)]))
i = 1
for mesh in meshes:
print("\rWriting frame", i, end = "")
if mesh is None:
# Put all vertices at the origin for bogus frames.
for n in range(point_count):
mdd.write(pack(">3f", 0.0, 0.0, 0.0))
else:
for point in mesh:
mdd.write(pack(">3f", point.x, point.y / aspect_ratio, point.z))
i += 1
print()

26
shell.nix Normal file
View File

@ -0,0 +1,26 @@
let
# You can replace "unstable" with any channel name to use that channel.
pkgs = import <unstable> {};
pypkgs = pkgs.python310Packages;
name = "face_track_dev_env";
in pkgs.stdenv.mkDerivation {
inherit name;
nativeBuildInputs = with pkgs; [
pypkgs.python
pypkgs.pip
pypkgs.numpy
pypkgs.opencv4
pypkgs.dlib
];
shellHook = with pkgs; ''
export PYTHON_LIBRARY="${pypkgs.python.libPrefix}"
export PYTHON_LIBPATH="${pypkgs.python}/lib"
export PYTHON_INCLUDE_DIR="${pypkgs.python}/include/${pypkgs.python.libPrefix}"
export PYTHON_VERSION="${pypkgs.python.pythonVersion}"
export PYTHON_NUMPY_PATH="${pypkgs.numpy}/${pypkgs.python.sitePackages}"
export PYTHON_NUMPY_INCLUDE_DIRS="${pypkgs.numpy}/${pypkgs.python.sitePackages}/numpy/core/include"
export PYTHONPATH="$PYTHONPATH:$HOME/.local/lib/python3.10/site-packages"
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
'';
}