lkcomputervision
is a Python package that provides a convenient interface for performing computer vision tasks using the MediaPipe library. It allows you to easily integrate hand tracking, face detection, face mesh analysis, and body pose estimation into your computer vision applications.
Resources: https://lakshaykumar.tech/devsprint
To install lkcomputervision
, you can use pip:
pip install lkcomputervision
Import and initialize the package
from lkcomputervision import MediaPipeHandler
mediapipe_handler = MediaPipeHandler()
Note: draw
paramater controls the annotation of the frame, by default it is set to true
Hand tracking allows you to detect and track hands in a given frame. You can also visualize the hand landmarks.
# Track hands in a frame
hand_result = mediapipe_handler.trackHands(frame)
# Access hand landmarks
hand_landmarks = hand_result["landmarks"]
# To visualize the hand landmarks, you can use the following code:
hand_frame = hand_result["frame"]
import cv2
from lkcomputervision import MediaPipeHandler
# Initialize the MediaPipeHandler
mp = MediaPipeHandler()
# Capture video from the webcam (you can also specify a video file path)
cap = cv2.VideoCapture(0) # 0 represents the default webcam
while True:
# Read a frame from the video capture
ret, frame = cap.read()
if not ret:
break # Break the loop if we can't read a frame
# Process the frame to track hands
result = mp.trackHands(frame)
# Retrieve the frame with hand landmarks drawn on it
frame_with_landmarks = result["frame"]
# Display the frame with landmarks
cv2.imshow("Hand Tracking", frame_with_landmarks)
# Exit the loop when the user presses the 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the video capture and close the OpenCV windows
cap.release()
cv2.destroyAllWindows()
Face detection enables you to identify and locate faces in a given frame. You can also visualize the detected faces.
# Detect faces in a frame
face_result = mediapipe_handler.detectFace(frame)
# Access face detection information
face_detections = face_result["landmarks"]
# To visualize the detected faces, you can use the following code:
face_frame = face_result["frame"]
Face mesh analysis allows you to analyze detailed facial features and landmarks in a given frame. You can also visualize the facial landmarks.
# Analyze face mesh in a frame
mesh_result = mediapipe_handler.faceMesh(frame)
# Access face mesh landmarks
face_mesh_landmarks = mesh_result["landmarks"]
# To visualize the face mesh landmarks, you can use the following code:
face_mesh_frame = mesh_result["frame"]
Body pose estimation helps you detect and track key body landmarks in a given frame. You can also visualize the body pose landmarks.
# Detect body pose in a frame
pose_result = mediapipe_handler.detectPose(frame)
# Access body pose landmarks
pose_landmarks = pose_result["landmarks"]
# To visualize the body pose landmarks, you can use the following code:
pose_frame = pose_result["frame"]
You can perform all analyses together in a single call and access the results as a dictionary. Please note that due to the potential impact on frame rate, visualization of the results may not be optimal in real-time applications.
# Perform all analyses together
all_detections = mediapipe_handler.detectAll(frame)
# Access all landmarks
hand_landmarks = all_detections["handTracking"]
face_detections = all_detections["detectFace"]
face_mesh_landmarks = all_detections["faceMesh"]
pose_landmarks = all_detections["detectPose"]
We hope you find lkcomputervision
valuable for your computer vision projects. If you have any questions, feedback, or run into any issues, please don't hesitate to reach out. You can contact us at contact@lakshaykumar.tech for inquiries and support. Happy coding!