mediapipe 测试代码

发布时间:2026/9/28 9:33:59
mediapipe 测试代码 MediaPipe 是一个由 Google 开发的开源跨平台框架用于构建多媒体机器学习ML管道。它主要用于处理和分析音频、视频、传感器数据等。你可以把它想象成一个强大的工具箱里面预装了许多针对常见任务的“机器学习流水线”Pipelines让你可以轻松地在移动设备、桌面端、Web 甚至云端快速实现和部署 ML 应用而无需从零开始构建复杂的模型。俯卧撑_0.zip - 蓝奏云文件大小22.0 Mhttps://wwbpm.lanzoue.com/i6IcD4a3qfla# coding:utf-8 import cv2 import mediapipe as mp import numpy as np # 定义计算角度的函数 def calculate_angle(a, b, c): a np.array(a) b np.array(b) c np.array(c) radians np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2( a[1] - b[1], a[0] - b[0] ) angle np.abs(radians * 180.0 / np.pi) if angle 180.0: angle 360 - angle return angle # 导入 MediaPipe 绘制工具和 Pose 模块 mp_drawing mp.solutions.drawing_utils mp_pose mp.solutions.pose # 初始化 Pose 实例 pose mp_pose.Pose(min_detection_confidence0.5, min_tracking_confidence0.5) # 打开视频如果是 macOS建议使用绝对路径例如 /Users/yourname/Desktop/ff.mp4 cap cv2.VideoCapture(ff.mp4) # 计数器与状态变量 counter 0 stage None # 角度阈值设定针对俯卧撑动作 # 建议的设置更宽松适配你图中的122° # 当人下压手臂弯曲角度【小于 130】度时就触发 down 状态 min_angle 130 # 当人重新把手臂伸直角度【大于 160】度或你视频中她能伸直的极限角度时触发 up 计数 max_angle 160 # ------------------------------ while cap.isOpened(): ret, frame cap.read() # 防崩溃处理视频读取结束或异常时安全退出 if not ret or frame is None: print(视频播放结束或无法读取帧。) break # 获取视频真实宽高 h, w, _ frame.shape # BGR 转换为 RGB image cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image.flags.writeable False # 姿态检测 results pose.process(image) # 转回 BGR 用于 OpenCV 显示 image.flags.writeable True image cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # 获取关键点并进行逻辑处理 try: landmarks results.pose_landmarks.landmark # 改为获取【右肩、右肘、右腕】坐标匹配侧向镜头侧边 shoulder [ landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y, ] elbow [ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y, ] wrist [ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y, ] # 计算右臂角度 angle calculate_angle(shoulder, elbow, wrist) # 在右肘节点附近绘制实时角度数值 elbow_text_pos tuple(np.multiply(elbow, [w, h]).astype(int)) cv2.putText( image, str(int(angle)), elbow_text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA, ) # 核心逻辑改进 # 1. 当右手臂弯曲角度小于 min_angle 时视为“下压状态” (down) if angle min_angle: stage down # 2. 当之前已经是 down 状态且右手臂再次伸直大于 max_angle 时完成一次推起 (up) if angle max_angle and stage down: stage up counter 1 print(f 计数 1当前总数: {counter}) except Exception as e: pass # 绘制顶部状态栏绘制计数与当前阶段 cv2.rectangle(image, (0, 0), (250, 73), (245, 117, 16), -1) cv2.putText( image, COUNTER, (15, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA, ) cv2.putText( image, str(counter), (35, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA, ) cv2.putText( image, STAGE, (135, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1, cv2.LINE_AA, ) cv2.putText( image, str(stage), (130, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA, ) # 绘制骨骼连接线 if results.pose_landmarks: mp_drawing.draw_landmarks( image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS, mp_drawing.DrawingSpec( color(245, 117, 66), thickness2, circle_radius2 ), mp_drawing.DrawingSpec( color(245, 66, 230), thickness2, circle_radius2 ), ) # 显示窗口 cv2.imshow(Mediapipe Feed, image) # 按 q 退出 if cv2.waitKey(1) 0xFF ord(q): break cap.release() cv2.destroyAllWindows()对视频中的人脸进行标记import cv2 import mediapipe as mp import time class FaceMeshDetector(): def __init__(self, staticModeFalse, maxFaces2, minDetectionCon0.5, minTrackCon0.5): self.staticMode staticMode self.maxFaces maxFaces self.minDetectionCon minDetectionCon self.minTrackCon minTrackCon self.mpDraw mp.solutions.drawing_utils self.mpFaceMesh mp.solutions.face_mesh self.faceMesh self.mpFaceMesh.FaceMesh(self.staticMode, self.maxFaces,False, self.minDetectionCon, self.minTrackCon) self.drawSpec self.mpDraw.DrawingSpec(thickness1, circle_radius2) def findFaceMesh(self, img, drawTrue): self.imgRGB cv2.cvtColor(img, cv2.COLOR_BGR2RGB) self.results self.faceMesh.process(self.imgRGB) faces [] if self.results.multi_face_landmarks: for faceLms in self.results.multi_face_landmarks: if draw: self.mpDraw.draw_landmarks(img, faceLms, self.mpFaceMesh.FACEMESH_CONTOURS, self.drawSpec, self.drawSpec) face [] for id, lm in enumerate(faceLms.landmark): ih, iw, ic img.shape x, y int(lm.x * iw), int(lm.y * ih) face.append([x, y]) faces.append(face) return img, faces def main(): # 打开摄像头0表示默认摄像头若有多个摄像头可尝试更改索引 cap cv2.VideoCapture(0) pTime 0 detector FaceMeshDetector(maxFaces2) while True: success, img cap.read() if not success: print(无法获取摄像头画面请检查摄像头是否正常连接) break img, faces detector.findFaceMesh(img) if len(faces)! 0: print(faces[0]) cTime time.time() fps 1 / (cTime - pTime) pTime cTime cv2.putText(img, fFPS: {int(fps)}, (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 3) cv2.imshow(Image, img) if cv2.waitKey(1) 0xFF 27: # 按下ESC键退出循环 break cap.release() # 释放摄像头资源 cv2.destroyAllWindows() # 关闭所有窗口 if __name__ __main__: main()对图片中的人脸标记代码Face Detectionimport cv2 import mediapipe as mp # 初始化MediaPipe人脸检测模型 mp_face_detection mp.solutions.face_detection mp_drawing mp.solutions.drawing_utils # 读取图像文件请将image.jpg替换为你的实际图像路径 image cv2.imread(d:/2.jpg) if image is None: print(无法读取图像文件请检查路径是否正确) exit() # 转换为RGB格式MediaPipe需要RGB输入 image_rgb cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 进行人脸检测 with mp_face_detection.FaceDetection(model_selection0, min_detection_confidence0.5) as face_detection: results face_detection.process(image_rgb) # 绘制检测结果 if results.detections: for detection in results.detections: mp_drawing.draw_detection(image, detection) # 显示结果图像 cv2.imshow(MediaPipe Face Detection, image) # 等待按键后关闭窗口 cv2.waitKey(0) cv2.destroyAllWindows()对图片中的人脸标记代码Face Meshimport cv2 import mediapipe as mp import numpy as np # 初始化 MediaPipe Face Mesh mp_face_mesh mp.solutions.face_mesh mp_drawing mp.solutions.drawing_utils mp_drawing_styles mp.solutions.drawing_styles # 配置 Face Mesh 参数 face_mesh mp_face_mesh.FaceMesh( static_image_modeTrue, max_num_faces10, refine_landmarksTrue, min_detection_confidence0.5) # 读取图像文件请将 image.jpg 替换为你的实际图像路径 image_path d:/2.jpg image cv2.imread(image_path) if image is None: print(无法读取图像文件请检查路径是否正确) exit() # 获取图像尺寸 image_height, image_width, _ image.shape # 转换为 RGB 格式MediaPipe 需要 RGB 输入 image_rgb cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 进行人脸网格检测 results face_mesh.process(image_rgb) # 绘制人脸网格 if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: # 绘制人脸网格连接线 mp_drawing.draw_landmarks( imageimage, landmark_listface_landmarks, connectionsmp_face_mesh.FACEMESH_TESSELATION, landmark_drawing_specNone, connection_drawing_specmp_drawing_styles .get_default_face_mesh_tesselation_style()) # 绘制人脸轮廓 mp_drawing.draw_landmarks( imageimage, landmark_listface_landmarks, connectionsmp_face_mesh.FACEMESH_CONTOURS, landmark_drawing_specNone, connection_drawing_specmp_drawing_styles .get_default_face_mesh_contours_style()) # 绘制眼睛区域 mp_drawing.draw_landmarks( imageimage, landmark_listface_landmarks, connectionsmp_face_mesh.FACEMESH_IRISES, landmark_drawing_specNone, connection_drawing_specmp_drawing_styles .get_default_face_mesh_iris_connections_style()) # 显示结果图像 cv2.imshow(MediaPipe Face Mesh, image) cv2.waitKey(0) cv2.destroyAllWindows() # 保存标注后的图像 output_path 2_face_mesh_output.jpg cv2.imwrite(output_path, image) print(f标注后的人脸图像已保存至: {output_path}) # 释放资源 face_mesh.close()对图片中的人体姿态记代码Pose Estimation (姿态估计) 会自动下载文件import cv2 import mediapipe as mp # 初始化 MediaPipe Pose mp_pose mp.solutions.pose mp_drawing mp.solutions.drawing_utils mp_drawing_styles mp.solutions.drawing_styles # 配置 Pose 参数 pose mp_pose.Pose( static_image_modeTrue, model_complexity2, enable_segmentationFalse, min_detection_confidence0.5) # 读取图像文件请将 image.jpg 替换为你的实际图像路径 image_path d:/pose.jpg image cv2.imread(image_path) if image is None: print(无法读取图像文件请检查路径是否正确) exit() # 获取图像尺寸 image_height, image_width, _ image.shape # 转换为 RGB 格式MediaPipe 需要 RGB 输入 image_rgb cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 进行姿态估计 results pose.process(image_rgb) # 绘制姿态关键点和连接线 if results.pose_landmarks: print(检测到姿态关键点) # 绘制关键点连接线 mp_drawing.draw_landmarks( imageimage, landmark_listresults.pose_landmarks, connectionsmp_pose.POSE_CONNECTIONS, landmark_drawing_specmp_drawing_styles.get_default_pose_landmarks_style()) # 显示结果图像 cv2.imshow(MediaPipe Pose Estimation, image) cv2.waitKey(0) cv2.destroyAllWindows() # 保存标注后的图像 output_path d:/2_pose_output.jpg cv2.imwrite(output_path, image) print(f姿态估计后的图像已保存至: {output_path}) # 释放资源 pose.close() import cv2 import mediapipe as mp import numpy as np # 初始化 MediaPipe Face Mesh mp_face_mesh mp.solutions.face_mesh mp_drawing mp.solutions.drawing_utils mp_drawing_styles mp.solutions.drawing_styles # 配置 Face Mesh 参数 face_mesh mp_face_mesh.FaceMesh( static_image_modeTrue, max_num_faces10, refine_landmarksTrue, min_detection_confidence0.5) # 读取图像文件请将 image.jpg 替换为你的实际图像路径 image_path d:/3.jpg image cv2.imread(image_path) if image is None: print(无法读取图像文件请检查路径是否正确) exit() # 获取图像尺寸 image_height, image_width, _ image.shape # 转换为 RGB 格式MediaPipe 需要 RGB 输入 image_rgb cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # 进行人脸网格检测 results face_mesh.process(image_rgb) # 绘制人脸网格 if results.multi_face_landmarks: for face_landmarks in results.multi_face_landmarks: # 绘制人脸网格连接线 mp_drawing.draw_landmarks( imageimage, landmark_listface_landmarks, connectionsmp_face_mesh.FACEMESH_TESSELATION, landmark_drawing_specNone, connection_drawing_specmp_drawing_styles .get_default_face_mesh_tesselation_style()) # 绘制人脸轮廓 mp_drawing.draw_landmarks( imageimage, landmark_listface_landmarks, connectionsmp_face_mesh.FACEMESH_CONTOURS, landmark_drawing_specNone, connection_drawing_specmp_drawing_styles .get_default_face_mesh_contours_style()) # 绘制眼睛区域 mp_drawing.draw_landmarks( imageimage, landmark_listface_landmarks, connectionsmp_face_mesh.FACEMESH_IRISES, landmark_drawing_specNone, connection_drawing_specmp_drawing_styles .get_default_face_mesh_iris_connections_style()) # 显示结果图像 cv2.imshow(MediaPipe Face Mesh, image) cv2.waitKey(0) cv2.destroyAllWindows() # 保存标注后的图像 output_path d:/2_face_mesh_output.jpg cv2.imwrite(output_path, image) print(f标注后的人脸图像已保存至: {output_path}) # 释放资源 face_mesh.close()BlazePose姿态特征点检测代码图像、视频、数据流BlazePose图像姿态识别首先需要下载模型轻量模型下载标准模型下载重量模型下载或者网盘下载https://wwrm.lanzoue.com/itqUy38ys54d参考https://blog.csdn.net/dulu_LAY/article/details/139042730实现单张姿态识别from mediapipe import solutions from mediapipe.framework.formats import landmark_pb2 import numpy as np import mediapipe as mp import cv2 from mediapipe.tasks import python from mediapipe.tasks.python import vision def draw_landmarks_on_image(rgb_image, detection_result): pose_landmarks_list detection_result.pose_landmarks annotated_image np.copy(rgb_image) print(rgb_image.shape) # Loop through the detected poses to visualize. for idx in range(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(xlandmark.x, ylandmark.y, zlandmark.z) for landmark in pose_landmarks ]) solutions.drawing_utils.draw_landmarks( annotated_image, pose_landmarks_proto, solutions.pose.POSE_CONNECTIONS, solutions.drawing_styles.get_default_pose_landmarks_style()) return annotated_image # STEP 2: Create an PoseLandmarker object. base_options python.BaseOptions(model_asset_pathpose_landmarker_heavy.task) # 需要提前下载好模型 填入模型地址 options vision.PoseLandmarkerOptions( num_poses7, base_optionsbase_options, output_segmentation_masksTrue) detector vision.PoseLandmarker.create_from_options(options) # STEP 3: Load the input image. image mp.Image.create_from_file(f:/999.jpg) # 你的图片路径输入 # STEP 4: Detect pose landmarks from the input image. detection_result detector.detect(image) # STEP 5: Process the detection result. In this case, visualize it. annotated_image draw_landmarks_on_image(image.numpy_view(), detection_result) cv2.imshow(new_image, cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR)) cv2.waitKey(0)BlazePose视频姿态人物姿态识别识别速度挺慢的目前还不清楚win系统下怎么用gpu加速mediapipefrom mediapipe import solutions from mediapipe.tasks import python from mediapipe.framework.formats import landmark_pb2 from mediapipe.tasks.python import vision import mediapipe as mp import cv2 import numpy as np def draw_landmarks_on_image(rgb_image, detection_result): pose_landmarks_list detection_result.pose_landmarks annotated_image np.copy(rgb_image) print(rgb_image.shape) # Loop through the detected poses to visualize. for idx in range(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(xlandmark.x, ylandmark.y, zlandmark.z) for landmark in pose_landmarks ]) solutions.drawing_utils.draw_landmarks( annotated_image, pose_landmarks_proto, solutions.pose.POSE_CONNECTIONS, solutions.drawing_styles.get_default_pose_landmarks_style()) return annotated_image model_path pose_landmarker_heavy.task # 模型 需要自己下载 file_path danceCK.mp4 # 要检测的图像的路径 BaseOptions mp.tasks.BaseOptions PoseLandmarker mp.tasks.vision.PoseLandmarker PoseLandmarkerOptions mp.tasks.vision.PoseLandmarkerOptions VisionRunningMode mp.tasks.vision.RunningMode # Create a pose landmarker instance with the video mode: options PoseLandmarkerOptions( base_optionsBaseOptions(model_asset_pathmodel_path), running_modeVisionRunningMode.VIDEO) with PoseLandmarker.create_from_options(options) as landmarker: cap cv2.VideoCapture(file_path) frame_count cap.get(cv2.CAP_PROP_FRAME_COUNT) fourcc cv2.VideoWriter_fourcc(M, P, 4, v) # 设置视频帧频 fps cap.get(cv2.CAP_PROP_FPS) # 设置视频大小 size (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) # VideoWriter方法是cv2库提供的保存视频方法 # 按照设置的格式来out输出 out cv2.VideoWriter(out.mp4, fourcc, fps, size) count 0 while True: ret, frame cap.read() if ret: mp_image mp.Image(image_formatmp.ImageFormat.SRGB, dataframe) pose_landmarker_result landmarker.detect_for_video(mp_image, count) count int(frame_count) annotated_image draw_landmarks_on_image(mp_image.numpy_view(), pose_landmarker_result) cv2.imshow(new_image, annotated_image) out.write(annotated_image) if cv2.waitKey(1) ord(q): break else: break cap.release() out.release() cv2.destroyAllWindows()MediaPipe 提供的主要预构建解决方案 (Pre-built Solutions)MediaPipe 最受欢迎的是它提供的一系列可以直接使用的模型Face Detection Face Mesh (人脸检测与面部网格):检测人脸并生成包含 468 个关键点的 3D 面部网格可以用于美颜、虚拟贴纸、表情识别等。Hand Tracking Hand Landmarks (手部追踪与手部关键点):实时追踪双手并识别 21 个手部关键点用于手势识别、手语翻译、AR 交互等。Pose Estimation (姿态估计):检测人体并生成包含 33 个关键点的 3D 姿态骨架广泛应用于健身指导、动作捕捉、舞蹈教学等。Object Detection Tracking (物体检测与追踪):检测图像或视频中的物体并进行追踪。Holistic (全身一体化模型):可以同时检测人脸、手部和身体姿态适用于需要综合多模态信息的应用。Iris (虹膜检测):检测眼睛和虹膜估算视线方向。Hair Segmentation (头发分割):精确分割出图像中的人的头发区域用于虚拟染发等。Text Detection (文本检测):检测图像中的文本区域。手关节的检测代码import cv2 #导入OpenCv库 import mediapipe as mp #导入Mediapipe库 import time cap cv2.VideoCapture(0) #0为打开默认摄像头,1为打开你设备列表的第二个摄像头,以此类推; mpHands mp.solutions.hands #使用Mediapipe库的手部姿势估计模型 hands mpHands.Hands(static_image_modeFalse, max_num_hands4, model_complexity1, min_detection_confidence0.5, min_tracking_confidence0.5) #创建手部姿势估计器对象设置参数。 mpDraw mp.solutions.drawing_utils #初始化Mediapipe库绘图工具 handLmsStyle mpDraw.DrawingSpec(color(0, 0, 255), thickness5) handConStyle mpDraw.DrawingSpec(color(0, 255, 0), thickness10) #设置绘制手部关键点和连接线的样式 pTime 0 cTime 0 #用于计算帧率 while True: #无限循环 ret, img cap.read() #读取摄像头的图像帧 img cv2.flip(img, 1) #对img图像进行水平翻转 if ret: imgRGB cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #将图像从BGR格式转换为RGB格式 result hands.process(imgRGB) #使用手部姿势估计器处理图像获取结果 # print(result.multi_hand_landmarks) imgHeight img.shape[0] #获取图像的高度并将其赋值给变量imgHeight(其中[0]表示高度的维度) imgWidth img.shape[1] #其中img是一个图像对象而shape[1]表示图像的宽度 if result.multi_hand_landmarks: #检查是否检测到手部 for handLms in result.multi_hand_landmarks: #遍历检测到的手部 mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle) #绘制手部关键点和连接线 for i, lm in enumerate(handLms.landmark): #遍历每个关键点 xPos int(lm.x * imgWidth) #计算关键点在图像中的x坐标 yPos int(lm.y * imgHeight) #计算关键点在图像中的y坐标 # cv2.putText(img, str(i), (xPos-25, yPos5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), ) if i 4: #绘制特定关键点的标记,如果是特定的关键点在代码中是第5个关键点 cv2.circle(img, (xPos, yPos), 15, (92, 65, 214), cv2.FILLED) print(i, xPos, yPos) #在特定关键点处绘制一个填充的圆 cTime time.time() #获取当前时间 fps 1/(cTime-pTime) #计算帧率 pTime cTime #更新上一帧的时间 cv2.putText(img, fFPS : {int(fps)}, (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3) #在图像上显示帧率 cv2.imshow(img, img) #显示处理后的图像 if cv2.waitKey(1) ord( ): break #如果按下

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询