Python crashes for holistic model of mediapipe if I am applying the model to a video.
I tried different steps to solve that issue:
- Using mediapipe in MacOs and Windows
- Use of CPU and GPU delegates
- Use of images instead of videos
- Conversion of the original MOV to MP4 format
However, this did not resolve the issue. I wonder whether this issue is related to the file format (MP4/MOV), but cv2 is able to read that and plotting the numpy array results in the expected image.
Thus, I would highly appreciate your support. You can find an example code attached:
My code:
import cv2
import sys
import os
import glob
import numpy as np
import mediapipe as mp # mediapipe for hand dectection
import plotly.graph_objs as go
from plotly.subplots import make_subplots
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation
import pandas as pd
import plotly.graph_objects as go
import plotly.io as pio
import scipy as sc
from scipy import signal
from mediapipe.tasks import python
BaseOptions =python.BaseOptions
HolisticLandmarker = mp.tasks.vision.HolisticLandmarker
HolisticLandmarkerOptions = mp.tasks.vision.HolisticLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode
# Create a face landmarker instance with the video mode:
options = HolisticLandmarkerOptions(
base_options=BaseOptions(model_asset_path="/data/Christian/MoCap/holistic_landmarker.task"),
min_face_detection_confidence=0.8,
min_face_suppression_threshold=0.8,
min_face_landmarks_confidence=0.8,
min_pose_detection_confidence=0.8,
min_pose_suppression_threshold=0.8,
min_pose_landmarks_confidence=0.8,
min_hand_landmarks_confidence=0.8,
output_face_blendshapes=True,
running_mode=VisionRunningMode.VIDEO)
input_dir = r'/data/video'
video_files = np.array(glob.glob(os.path.join(input_dir, "**",'*mp4'),recursive=True))
# Set mediapipe model
# Iterate through video_file (words)
for video_file in video_files:
# Create a directory for the current word
video_file_path = video_file[:video_file.rfind(os.sep)]
outputName = video_file[video_file.rfind(
os.sep)+1:video_file.rfind('video')]
output_video_path = os.path.join(
output_dir, video_file[video_file.rfind("/")+1:-4])
subject = outputName[outputName.rfind(
"sub-"):outputName.rfind("_ses")]
session = outputName[outputName.rfind(
"ses-"):outputName.find("_", outputName.find("_")+1)]
if not os.path.exists(os.path.join(output_video_path, subject, session)):
os.makedirs(os.path.join(output_video_path, subject,
session, '.video'), exist_ok=True)
os.makedirs(os.path.join(output_video_path, subject,
session, 'motion'), exist_ok=True)
video_path = os.path.join(video_file_path, video_file)
with HolisticLandmarker.create_from_options(options) as holistic:
# The landmarker is initialized. Use it here.
# Read video and calculate duration
cap = cv2.VideoCapture(video_path)
# Saving Video-
fps = int(cap.get(cv2.CAP_PROP_FPS))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(os.path.join(output_video_path, subject, session, '.video', outputName +
'desc-overlayVideo_video.mov'), fourcc, fps, (int(cap.get(3)), int(cap.get(4))))
# Initialize empty list for keypoints
keypoints_list = []
success = 1
# Iterate through frames in the sequence
while success:
# Read video frame
success, frame = cap.read()
frame_timestamp_ms = int(cap.get(cv2.CAP_PROP_POS_MSEC))
# Make detections and draw landmarks
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
results = holistic.detect_for_video(mp_image, frame_timestamp_ms)
Here is the resulting error message:
I0000 00:00:1717520611.283032 68206 gl_context_egl.cc:85] Successfully initialized EGL. Major : 1 Minor: 5
I0000 00:00:1717520611.285679 70364 gl_context.cc:357] GL version: 3.2 (OpenGL ES 3.2 Mesa 23.2.1-1ubuntu3.1~22.04.2), renderer: llvmpipe (LLVM 15.0.7, 256 bits)
Fatal Python error: Aborted
Main thread:
Current thread 0x00007f0e2b8eb440 (most recent call first):
File "/home/christian.schmitz/.conda/envs/MNI-mocap/lib/python3.8/site-packages/mediapipe/python/packet_getter.py", line 61 in get_proto
File "/home/christian.schmitz/.conda/envs/MNI-mocap/lib/python3.8/site-packages/mediapipe/tasks/python/vision/holistic_landmarker.py", line 170 in _build_landmarker_result
File "/home/christian.schmitz/.conda/envs/MNI-mocap/lib/python3.8/site-packages/mediapipe/tasks/python/vision/holistic_landmarker.py", line 535 in detect_for_video
File "/tmp/ipykernel_68206/2139099985.py", line 103 in <module>
Restarting kernel...
New contributor
Christian Schmitz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.