Python Opencv Pages - 1 2 3
Download Python Project : Py-MediaPipe-FaceMesh.zip - (9.5 KB zip file) Download
Source Code of Python Project - Py-MediaPipe-FaceMesh:
Download this Source Code at python file: Py_MediaPipe_FaceMesh.py - (2.5 KB Python file) download
#Flip the image horizontally for a selfie-view display.
#pip uninstall mediapipe #do this to uninstall your current version
#pip install mediapipe==0.10.9
import
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_face_mesh = mp.solutions.face_mesh
# For webcam input:
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
cap = cv2.
with
max_num_faces=1,
refine_landmarks=
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
success, image = cap.read()
image.flags.writeable =
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_mesh.process(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(
image=image,
landmark_list=face_landmarks,
connections=mp_face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_tesselation_style())
connections=mp_face_mesh.FACEMESH_CONTOURS,
.get_default_face_mesh_contours_style())
connections=mp_face_mesh.FACEMESH_IRISES,
.get_default_face_mesh_iris_connections_style())
cv2.imshow(
cap.release()
Output of Python Project - Py-MediaPipe-FaceMesh:
Download Python Project : Py-Opencv-FHLandmarks.zip - (12.5 KB zip file) Download
Source Code of Python Project -Py-Opencv-FHLandmarks :
Download this Source Code at python file: Py_Opencv_FHLandmarks.py - (3.89 KB Python file) download
# 2- Initializing Holistic model from Mediapipe and Drawing utils for detecting
# and drawing landmarks on the image
mp_holistic = mp.solutions.holistic
# min_detection_confidence: It is used to specify the minimum confidence value with
# which the detection from the person-detection model needs to be considered as successful.
# Can specify a value in [0.0,1.0]. The default value is 0.5.
# min_tracking_confidence: It is used to specify the minimum confidence value with
# which the detection from the landmark-tracking model must be considered as successful.
holistic_model = mp_holistic.Holistic(
min_tracking_confidence=0.5
)
# Code to access landmarks
print
for
# Initializing the drawing utils for drawing the facial landmarks on image
#3- Detecting Face and Hand landmarks from the image. Holistic model processes the image
# and produces landmarks for Face, Left Hand, Right Hand and also detects the Pose of the:
# 3.1- Capture the frames continuously from the camera using OpenCV.
# 3.2- Convert the BGR image to an RGB image and make predictions using initialized holistic model.
# 3.3- The predictions made by the holistic model are saved in the results variable from which we can access the landmarks using results.face_landmarks, results.right_hand_landmarks, results.left_hand_landmarks respectively.
# 3.4- Draw the detected landmarks on the image using the draw_landmarks function from drawing utils.
# 3.5- Display the resulting Image.
# (0) in VideoCapture is used to connect to your computer's default camera
capture = cv2.
# Initializing current time and precious time for calculating the FPS
previousTime = 0
currentTime = 0
while
ret, frame = capture.read()
frame = cv2.resize(frame, (800, 600))
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = holistic_model.process(image)
image,
results.face_landmarks,
mp_holistic.FACEMESH_CONTOURS,
mp_drawing.DrawingSpec(
color=(255,0,255),
thickness=1,
circle_radius=1
),
color=(0,255,255),
results.right_hand_landmarks,
mp_holistic.HAND_CONNECTIONS
results.left_hand_landmarks,
currentTime = time.time()
fps = 1 / (currentTime-previousTime)
previousTime = currentTime
cv2.putText(image,
# When all the process is done
# Release the capture and destroy all windows
capture.release()
cv2.destroyAllWindows()
Output of Python Project -Py-Opencv-FHLandmarks, is Using the Webcam of PC :
Face and Right Hand Landmarks Image 1
Face and Right Hand Landmarks Image 2
Face and Left Hand Landmarks Image 1
Face and Left Hand Landmarks Image 2