Files
sign-predictor/keypoint_extractor.py

58 lines
1.6 KiB
Python

import mediapipe as mp
import cv2
class KeypointExtractor:
def __init__(self):
self.mp_drawing = mp.solutions.drawing_utils
# hands extractor
self.hands = mp.solutions.hands.Hands(
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
max_num_hands=2
)
# pose extractor
self.pose = mp.solutions.pose.Pose(
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
model_complexity=2
)
def extract(self, image, video):
# load video
pass
def extract_from_frame(self, image):
# Convert the BGR image to RGB and process it with MediaPipe Pose.
hand_results = self.hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw the hand annotations on the image.
draw_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
draw_image.flags.writeable = False
for hand_landmarks in hand_results.multi_hand_landmarks:
self.mp_drawing.draw_landmarks(
draw_image, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS)
pose_results = self.pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
self.mp_drawing.draw_landmarks(
draw_image, pose_results.pose_landmarks, mp.solutions.pose.POSE_CONNECTIONS)
draw_image.flags.writeable = True
draw_image = cv2.cvtColor(draw_image, cv2.COLOR_RGB2BGR)
return draw_image
ke = KeypointExtractor()
image = cv2.imread('data/test_photo.jpg')
image = ke.extract_from_frame(image)
# save image
cv2.imwrite('test_output.jpg', image)