MediaPipe 简介#
MediaPipe 是一款由 Google 开发并开源的多媒体机器学习模型应用框架。
下面表格是他支持的功能和平台
Android | iOS | C++ | Python | JS | Coral | |
---|---|---|---|---|---|---|
Face Detection | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Face Mesh | ✅ | ✅ | ✅ | ✅ | ✅ | |
Iris | ✅ | ✅ | ✅ | |||
Hands | ✅ | ✅ | ✅ | ✅ | ✅ | |
Pose | ✅ | ✅ | ✅ | ✅ | ✅ | |
Holistic | ✅ | ✅ | ✅ | ✅ | ✅ | |
Selfie Segmentation | ✅ | ✅ | ✅ | ✅ | ✅ | |
Hair Segmentation | ✅ | ✅ | ||||
Object Detection | ✅ | ✅ | ✅ | ✅ | ||
Box Tracking | ✅ | ✅ | ✅ | |||
Instant Motion Tracking | ✅ | |||||
Objectron | ✅ | ✅ | ✅ | ✅ | ||
KNIFT | ✅ | |||||
AutoFlip | ✅ | |||||
MediaSequence | ✅ | |||||
YouTube 8M | ✅ |
MediaPipe 测试#
我这里调用 Pose 的 JS API 来再网页中进行测试
通过 React-Webcam 库来从相机获取视频,再通过 canvas 绘制识别结果
import Webcam from "react-webcam";
import React, { useRef, useEffect, useState } from "react";
import { drawConnectors, drawLandmarks } from "@mediapipe/drawing_utils";
import { Camera } from "@mediapipe/camera_utils";
import { Pose, POSE_CONNECTIONS, POSE_LANDMARKS } from "@mediapipe/pose/pose";
const MPHolistic = () => {
const webcamRef = useRef(null);
const canvasRef = useRef(null);
useEffect(() => {
const pose = new Pose({
locateFile: (file) => {
return `pose/${file}`;
},
});
pose.setOptions({
modelComplexity: 1,
smoothLandmarks: true,
enableSegmentation: true,
smoothSegmentation: true,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5,
});
pose.onResults(onResults);
if (
typeof webcamRef.current !== "undefined" &&
webcamRef.current !== null
) {
const camera = new Camera(webcamRef.current.video, {
onFrame: async () => {
await pose.send({ image: webcamRef.current.video });
// await holistic.send({ image: webcamRef.current.video })
},
width: 1280,
height: 720,
});
camera.start();
}
}, []);
const onResults = async (results) => {
const videoWidth = webcamRef.current.video.videoWidth;
const videoHeight = webcamRef.current.video.videoHeight;
canvasRef.current.width = 1280;
canvasRef.current.height = 720;
const canvasElement = canvasRef.current;
const canvasCtx = canvasElement.getContext("2d");
canvasCtx.save();
canvasCtx.clearRect(0, 0, videoWidth, videoHeight);
canvasCtx.translate(videoWidth, 0)
canvasCtx.scale(-1, 1)
canvasCtx.drawImage(
results.image,
0,
0,
canvasElement.width,
canvasElement.height
);
drawConnectors(canvasCtx, results.poseLandmarks, POSE_CONNECTIONS, { color: "#00FF00", lineWidth: 4 })
drawLandmarks(canvasCtx, results.poseLandmarks, { color: "#FF0000", lineWidth: 2 })
canvasCtx.restore();
};
const videoConstraints = {
width: 1280,
height: 720,
facingMode: "user",
};
return (
<>
<div
style={{
position: "relative",
width: "100%",
height: "100%",
}}
>
<Webcam
audio={false}
mirrored={true}
ref={webcamRef}
style={{
position: "absolute",
marginLeft: "auto",
marginRight: "auto",
left: 0,
right: 0,
textAlign: "center",
zindex: 9,
width: 1280,
height: 720,
}}
videoConstraints={videoConstraints}
/>
<canvas
ref={canvasRef}
style={{
position: "absolute",
marginLeft: "auto",
marginRight: "auto",
left: 0,
right: 0,
textAlign: "center",
zindex: 9,
width: 1280,
height: 720,
}}
></canvas>
</div>
</>
);
};
export default MPHolistic;
效果#
一个测试截图如下
我这里也在本机的网页中测试了一下帧率,大概可以稳定 fps 在 100 左右,识别效果也完全可以接受~