-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmediapipe_example.py
More file actions
63 lines (51 loc) · 2.12 KB
/
mediapipe_example.py
File metadata and controls
63 lines (51 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Mediapipe example from official documentation
import time
import cv2
import mediapipe as mp
if __name__ == "__main__":
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose
start_time = time.time()
frame_counter = 0
fps = ""
# For webcam input:
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
with mp_pose.Pose(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
while camera.isOpened():
success, image = camera.read()
if not success:
print("Ignoring empty camera frame.")
continue
# To improve performance, optionally mark the image as not writeable to pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = pose.process(image)
# Draw the pose annotation on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
# Plot pose world landmarks.
# mp_drawing.plot_landmarks(
# results.pose_world_landmarks, mp_pose.POSE_CONNECTIONS)
# Flip the image horizontally for a selfie-view display.
image = cv2.flip(image, 1)
# Calc FPS average over multiple frame
frame_counter += 1
if (time.time() - start_time) > 0.33:
fps = "FPS: {:.2f}".format(frame_counter / (time.time() - start_time))
frame_counter = 0
start_time = time.time()
cv2.putText(image, fps, (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
# Show image
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(5) & 0xFF == 27:
break
camera.release()