- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpose_estimator.py
More file actions
Latest commit
61 lines (50 loc) · 2.04 KB
/
Copy pathpose_estimator.py
File metadata and controls
61 lines (50 loc) · 2.04 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
importmediapipeasmp
frommediapipe.tasksimportpython
importcv2
importnumpyasnp
frommediapipeimportsolutions
frommediapipe.framework.formatsimportlandmark_pb2
file_name='IMG_2149.mov'
file_name=file_name
model_path='pose_landmarker_full.task'
# https://developers.google.com/mediapipe/solutions/vision/pose_landmarker/python#video
options=python.vision.PoseLandmarkerOptions(
base_options=python.BaseOptions(model_asset_path=model_path),
running_mode=python.vision.RunningMode.VIDEO)
defdraw_landmarks_on_image(rgb_image, detection_result):
pose_landmarks_list=detection_result.pose_landmarks
annotated_image=np.copy(rgb_image)
foridxinrange(len(pose_landmarks_list)):
pose_landmarks=pose_landmarks_list[idx]
# Draw the pose landmarks.
pose_landmarks_proto=landmark_pb2.NormalizedLandmarkList()
pose_landmarks_proto.landmark.extend([
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) forlandmarkinpose_landmarks
])
solutions.drawing_utils.draw_landmarks(
annotated_image,
pose_landmarks_proto,
solutions.pose.POSE_CONNECTIONS,
solutions.drawing_styles.get_default_pose_landmarks_style())
returnannotated_image
withpython.vision.PoseLandmarker.create_from_options(options) aslandmarker:
cap=cv2.VideoCapture(file_name)
fps=cap.get(cv2.CAP_PROP_FPS)
calc_timestamps= [0.0]
if (cap.isOpened()==False):
print("Error opening video stream or file")
while(cap.isOpened()):
ret, frame=cap.read()
ifret==True:
mp_image=mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
calc_timestamps.append(int(calc_timestamps[-1] +1000/fps))
detection_result=landmarker.detect_for_video(mp_image, calc_timestamps[-1])
annotated_image=draw_landmarks_on_image(frame, detection_result)
cv2.imshow('Frame',annotated_image)
# Press Q on keyboard to exit
ifcv2.waitKey(25) &0xFF==ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()