
本文详细指导如何将codepen上的前端项目,特别是涉及外部库和模块的mediapipe人脸关键点检测项目,成功部署到本地运行。文章聚焦于解决依赖引入、javascript模块加载、资源路径配置及跨域等常见问题,提供了一份完整的html代码示例,帮助开发者顺利实现codepen项目的本地化调试与开发。
在前端开发中,CodePen等在线代码编辑器为快速原型设计和分享提供了极大便利。然而,当需要将这些在线项目迁移到本地环境进行更深入的开发或调试时,开发者常会遇到一些挑战,尤其是在处理外部库、模块化脚本和资源路径方面。本文将以一个MediaPipe人脸关键点检测项目为例,详细阐述如何解决这些问题,确保项目能在本地顺利运行。
将CodePen项目直接复制粘贴到本地往往无法正常工作,主要原因包括:
解决上述问题的最直接方法是创建一个独立的HTML文件,将所有必要的HTML结构、CSS样式和JavaScript代码整合进去,并确保所有外部依赖都通过正确的CDN链接引入。
一个标准的HTML文件需要包含<html>、<head>和<body>标签。<head>中应包含字符集声明、视口设置以及页面标题等基本元数据。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <title>Face Landmarker</title> <!-- 样式和脚本将在此处添加 --> </head> <body> <!-- 页面内容将在此处添加 --> </body> </html>
原CodePen项目可能使用了SCSS的@use "@material";来引入Material Design样式。在本地,我们应移除此SCSS语法,转而通过CDN链接直接引入Material Components Web的CSS文件。项目特有的CSS可以直接内联到<style>标签中。
<head>
<!-- ...其他meta标签... -->
<title>Face Landmarker</title>
<style>
/* 移除 @use "@material"; */
body {
font-family: helvetica, arial, sans-serif;
margin: 2em;
color: #3d3d3d;
--mdc-theme-primary: #007f8b;
--mdc-theme-on-primary: #f1f3f4;
}
h1 {
font-style: italic;
color: #ff6f00;
color: #007f8b;
}
h2 {
clear: both;
}
em {
font-weight: bold;
}
video {
clear: both;
display: block;
transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
}
section {
opacity: 1;
transition: opacity 500ms ease-in-out;
}
header,
footer {
clear: both;
}
.removed {
display: none;
}
.invisible {
opacity: 0.2;
}
.note {
font-style: italic;
font-size: 130%;
}
.videoView,
.detectOnClick,
.blend-shapes {
position: relative;
float: left;
width: 48%;
margin: 2% 1%;
cursor: pointer;
}
.videoView p,
.detectOnClick p {
position: absolute;
padding: 5px;
background-color: #007f8b;
color: #fff;
border: 1px dashed rgba(255, 255, 255, 0.7);
z-index: 2;
font-size: 12px;
margin: 0;
}
.highlighter {
background: rgba(0, 255, 0, 0.25);
border: 1px dashed #fff;
z-index: 1;
position: absolute;
}
.canvas {
z-index: 1;
position: absolute;
pointer-events: none;
}
.output_canvas {
transform: rotateY(180deg);
-webkit-transform: rotateY(180deg);
-moz-transform: rotateY(180deg);
}
.detectOnClick {
z-index: 0;
}
.detectOnClick img {
width: 100%;
}
.blend-shapes-item {
display: flex;
align-items: center;
height: 20px;
}
.blend-shapes-label {
display: flex;
width: 120px;
justify-content: flex-end;
align-items: center;
margin-right: 4px;
}
.blend-shapes-value {
display: flex;
height: 16px;
align-items: center;
background-color: #007f8b;
}
</style>
<!-- 通过CDN链接引入Material Components Web CSS -->
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
<!-- 通过CDN链接引入Material Components Web JS -->
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
</head>JavaScript部分是本地化过程中最容易出错的地方。
<body>
<h1>Face landmark detection using the MediaPipe FaceLandmarker task</h1>
<section id="demos" class="invisible">
<h2>Demo: Detecting Images</h2>
<p><b>Click on an image below</b> to see the key landmarks of the face.</p>
<div class="detectOnClick">
<!-- 注意 crossorigin="anonymous" 属性 -->
<img src="https://storage.googleapis.com/mediapipe-assets/portrait.jpg" width="100%" crossorigin="anonymous" title="Click to get detection!" />
</div>
<div class="blend-shapes">
<ul class="blend-shapes-list" id="image-blend-shapes"></ul>
</div>
<h2>Demo: Webcam continuous face landmarks detection</h2>
<p>Hold your face in front of your webcam to get real-time face landmarker detection.</br>Click <b>enable webcam</b> below and grant access to the webcam if prompted.</p>
<div id="liveView" class="videoView">
<button id="webcamButton" class="mdc-button mdc-button--raised">
<span class="mdc-button__ripple"></span>
<span class="mdc-button__label">ENABLE WEBCAM</span>
</button>
<div style="position: relative;">
<video id="webcam" style="position: abso" autoplay playsinline></video>
<canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px;"></canvas>
</div>
</div>
<div class="blend-shapes">
<ul class="blend-shapes-list" id="video-blend-shapes"></ul>
</div>
</section>
<!-- 核心 JavaScript 代码,使用 type="module" -->
<script type="module">
// 确保 MediaPipe Vision 库从 CDN 正确导入,并指定版本
import vision from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0";
const { FaceLandmarker, FilesetResolver, DrawingUtils } = vision;
const demosSection = document.getElementById("demos");
const imageBlendShapes = document.getElementById("image-blend-shapes");
const videoBlendShapes = document.getElementById("video-blend-shapes");
let faceLandmarker;
let runningMode= "IMAGE" | "VIDEO";
let enableWebcamButton = HTMLButtonElement;
let webcamRunning= Boolean = false;
const videoWidth = 480;
async function runDemo() {
// FilesetResolver 需要指向正确的 WASM 文件路径,通常也是 CDN
const filesetResolver = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm"
);
faceLandmarker = await FaceLandmarker.createFromOptions(filesetResolver, {
baseOptions: {
// 模型资产路径也需要是 CDN 地址
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task`,
delegate: "GPU"
},
outputFaceBlendshapes: true,
runningMode,
numFaces: 1
});
demosSection.classList.remove("以上就是如何在本地运行CodePen项目:MediaPipe人脸关键点检测的本地化实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号