Skip to content

Instantly share code, notes, and snippets.

@jonmatthis
Created May 20, 2022 16:20
Show Gist options
  • Save jonmatthis/501f81458863533c85cdc6d6d5c86101 to your computer and use it in GitHub Desktop.
Save jonmatthis/501f81458863533c85cdc6d6d5c86101 to your computer and use it in GitHub Desktop.
simple script to load in a video (or connect to a webcam), annotate it with `mediapipe` data, and save it out in the same directory as the original video
#to install dependencies:
# pip install opencv-python
# pip install mediapipe
import cv2
import mediapipe as mp
from pathlib import Path
run_mediapipe = True
save_video=True
## use webcam
# camera_number = 0
# cap = cv2.VideoCapture(camera_number)
# use video file
input_video_folder = Path(r"C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data\sesh_2022-04-20_07_41_59_paul_tiktok_ayub_0\SyncedVideos")
input_video_filename = 'sesh_2022-04-20_07_41_59_paul_tiktok_ayub_0_synced_Cam5.mp4'
cap = cv2.VideoCapture(str(input_video_folder / input_video_filename))
video_frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
video_frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
framerate = cap.get(cv2.CAP_PROP_FPS)
### mediapipe stuff
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_holistic = mp.solutions.holistic
body_drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=.2)
hand_drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=.2)
face_drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=.2)
def annotate_image_with_mediapipe_data(image, mediapipe_results):
mp_drawing.draw_landmarks(image=image,
landmark_list=mediapipe_results.face_landmarks,
connections=mp_holistic.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_contours_style())
mp_drawing.draw_landmarks(image=image,
landmark_list=mediapipe_results.face_landmarks,
# connections=mp_holistic.FACEMESH_TESSELATION,
connections=mp_holistic.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_face_mesh_tesselation_style())
mp_drawing.draw_landmarks(image=image,
landmark_list=mediapipe_results.pose_landmarks,
connections=mp_holistic.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles
.get_default_pose_landmarks_style())
mp_drawing.draw_landmarks(
image=image,
landmark_list=mediapipe_results.left_hand_landmarks,
connections=mp_holistic.HAND_CONNECTIONS,
landmark_drawing_spec=None,
connection_drawing_spec=mp_drawing_styles
.get_default_hand_connections_style())
mp_drawing.draw_landmarks(
image=image,
landmark_list=mediapipe_results.right_hand_landmarks,
connections=mp_holistic.HAND_CONNECTIONS,
landmark_drawing_spec = None,
connection_drawing_spec=mp_drawing_styles
.get_default_hand_connections_style())
return image
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
### webcam stuff
if save_video:
writer = cv2.VideoWriter(input_video_filename+'_annotated.mp4',
cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
framerate,
(video_frame_width, video_frame_height))
with mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
model_complexity=2) as holistic:
success = True
while success:
success, raw_image = cap.read()
if not success:
break
if run_mediapipe:
mediapipe_results = holistic.process(raw_image)
annotated_image = annotate_image_with_mediapipe_data(raw_image, mediapipe_results)
else:
annotated_image = raw_image
if save_video:
writer.write(annotated_image)
cv2.imshow(f'Camera: {camera_number}, press Q to quit', annotated_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment