Skip to content

Instantly share code, notes, and snippets.

@choowilson
Created May 24, 2020 08:20
Show Gist options
  • Save choowilson/c6d2e4188a4fac029ec408aab2e69705 to your computer and use it in GitHub Desktop.
Save choowilson/c6d2e4188a4fac029ec408aab2e69705 to your computer and use it in GitHub Desktop.
face alignment webcam
import cv2
import face_alignment
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from skimage import io
import collections
from timeit import default_timer as timer
# Run the 3D face alignment on a test image, without CUDA.
fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, device='cpu', flip_input=True, face_detector='sfd')
# 0 is the representation of your camera, it starts from 0, number 1 is your second connected USB webcam
camera = cv2.VideoCapture(0)
# Videos are actually multiple frames processed frame-by-frame
while camera.isOpened():
start = timer()
_, frame = camera.read()
# frame
# try:
# input_img = io.imread('/home/skymind/Desktop/nicolasCage.jpg')
# except Exception as e:
# print(e)
#
# preds = fa.get_landmarks(input_img)[-1]
preds = fa.get_landmarks(frame)[-1]
#
# # 2D-Plot
# plot_style = dict(marker='o',
# markersize=4,
# linestyle='-',
# lw=2)
#
pred_type = collections.namedtuple('prediction_type', ['slice', 'color'])
# pred_types = {'face': pred_type(slice(0, 17), (0.682, 0.780, 0.909, 0.5)),
# 'eyebrow1': pred_type(slice(17, 22), (1.0, 0.498, 0.055, 0.4)),
# 'eyebrow2': pred_type(slice(22, 27), (1.0, 0.498, 0.055, 0.4)),
# 'nose': pred_type(slice(27, 31), (0.345, 0.239, 0.443, 0.4)),
# 'nostril': pred_type(slice(31, 36), (0.345, 0.239, 0.443, 0.4)),
# 'eye1': pred_type(slice(36, 42), (0.596, 0.875, 0.541, 0.3)),
# 'eye2': pred_type(slice(42, 48), (0.596, 0.875, 0.541, 0.3)),
# 'lips': pred_type(slice(48, 60), (0.596, 0.875, 0.541, 0.3)),
# 'teeth': pred_type(slice(60, 68), (0.596, 0.875, 0.541, 0.4))
# }
pred_types = {'face': pred_type(range(0, 17), (0.682, 0.780, 0.909, 0.5)),
'eyebrow1': pred_type(range(17, 22), (1.0, 0.498, 0.055, 0.4)),
'eyebrow2': pred_type(range(22, 27), (1.0, 0.498, 0.055, 0.4)),
'nose': pred_type(range(27, 31), (0.345, 0.239, 0.443, 0.4)),
'nostril': pred_type(range(31, 36), (0.345, 0.239, 0.443, 0.4)),
'eye1': pred_type(range(36, 42), (0.596, 0.875, 0.541, 0.3)),
'eye2': pred_type(range(42, 48), (0.596, 0.875, 0.541, 0.3)),
'lips': pred_type(range(48, 60), (0.596, 0.875, 0.541, 0.3)),
'teeth': pred_type(range(60, 68), (0.596, 0.875, 0.541, 0.4))
}
# fig = plt.figure(figsize=plt.figaspect(.5))
# ax = fig.add_subplot(1, 2, 1)
# ax.imshow(input_img)
# ax.imshow(frame)
for pred_type in pred_types.values():
# print(pred_type.slice)
# print(len(pred_type.slice))
for i in range(len(pred_type.slice)):
cv2.circle(frame, (preds[pred_type.slice, 0][i], preds[pred_type.slice, 1][i]), 2, (255, 0, 0, 0), -1, 0, 0)
# print(preds[pred_type.slice, 0][0])
# ax.plot(preds[pred_type.slice, 0],
# preds[pred_type.slice, 1],
# color=pred_type.color, **plot_style)
# >> > plot(x, y, color='green', marker='o', linestyle='dashed',
# ...
# linewidth = 2, markersize = 12)
# ax.axis('off')
# 3D-Plot
# ax = fig.add_subplot(1, 2, 2, projection='3d')
# surf = ax.scatter(preds[:, 0] * 1.2,
# preds[:, 1],
# preds[:, 2],
# c='cyan',
# alpha=1.0,
# edgecolor='b')
#
# for pred_type in pred_types.values():
# ax.plot3D(preds[pred_type.slice, 0] * 1.2,
# preds[pred_type.slice, 1],
# preds[pred_type.slice, 2], color='blue')
# ax.view_init(elev=90., azim=90.)
# ax.set_xlim(ax.get_xlim()[::-1])
# plt.show()
cv2.imshow('My webcam stream', frame)
end = timer()
print(f"Seconds: {end - start}s")
k = cv2.waitKey(1)
# if 'Q' is pressed the program will exits
if k == ord('q'):
break
# Release the frames
camera.release()
# Destroy all windows
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment