You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.2 KiB
60 lines
2.2 KiB
import cv2
|
|
import mediapipe as mp
|
|
from DataStreamModule import DataStreamModule
|
|
from IHPEModule import IHPEModule
|
|
|
|
# initialize mediapipe drawing utilities and pose models
|
|
mp_drawing = mp.solutions.drawing_utils
|
|
mp_pose = mp.solutions.pose
|
|
|
|
|
|
# This class implements Human Pose Estimation (HPE) using the mediapipe library
|
|
class HPEModule(IHPEModule):
|
|
|
|
# This method starts HPE using a camera specified by its name
|
|
def startHPEwithCamera(self, camera_name):
|
|
|
|
# initialize data stream and camera object
|
|
data_stream = DataStreamModule()
|
|
cap = data_stream.get_camera_stream(camera_name)
|
|
|
|
# set the window name using the camera name
|
|
window_name = f"Pose Estimation on Camera {camera_name}"
|
|
|
|
# start pose detection
|
|
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
|
while True:
|
|
# get the next frame from the camera
|
|
frame = next(cap)
|
|
|
|
# Recolor image to RGB
|
|
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
image.flags.writeable = False
|
|
|
|
# Make detection
|
|
results = pose.process(image)
|
|
|
|
# Recolor back to BGR
|
|
image.flags.writeable = True
|
|
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
# Extract landmarks
|
|
try:
|
|
landmarks = results.pose_landmarks.landmark
|
|
print(landmarks)
|
|
except:
|
|
pass
|
|
|
|
# Render detections
|
|
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
|
|
mp_drawing.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=2),
|
|
mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
|
|
)
|
|
|
|
cv2.imshow(window_name, image)
|
|
keyCode = cv2.waitKey(1)
|
|
if cv2.getWindowProperty(window_name, cv2.WND_PROP_VISIBLE) < 1:
|
|
break
|
|
|
|
# destroy the window after exiting the loop
|
|
#cv2.destroyWindow(window_name)
|
|
|