首页 > web前端 > js教程 > 正文

如何在本地运行CodePen项目:MediaPipe人脸关键点检测的本地化实践

聖光之護
发布: 2025-11-10 18:22:01
原创
994人浏览过

如何在本地运行CodePen项目:MediaPipe人脸关键点检测的本地化实践

本文详细指导如何将codepen上的前端项目,特别是涉及外部库和模块的mediapipe人脸关键点检测项目,成功部署到本地运行。文章聚焦于解决依赖引入、javascript模块加载、资源路径配置及跨域等常见问题,提供了一份完整的html代码示例,帮助开发者顺利实现codepen项目的本地化调试与开发。

在前端开发中,CodePen等在线代码编辑器为快速原型设计和分享提供了极大便利。然而,当需要将这些在线项目迁移到本地环境进行更深入的开发或调试时,开发者常会遇到一些挑战,尤其是在处理外部库、模块化脚本和资源路径方面。本文将以一个MediaPipe人脸关键点检测项目为例,详细阐述如何解决这些问题,确保项目能在本地顺利运行。

本地化CodePen项目面临的挑战

将CodePen项目直接复制粘贴到本地往往无法正常工作,主要原因包括:

  1. 外部CSS/JS库的引入方式差异: CodePen通常通过其UI界面或特定的预处理器语法(如SCSS的@use)来管理外部库。在本地,这些库需要通过标准的<link>标签引入CSS文件,或通过<script>标签引入JavaScript文件,且通常需要指向CDN地址。
  2. JavaScript模块化: 现代JavaScript项目广泛使用ES Modules(import/export语法)。在浏览器环境中,使用import语句的脚本必须通过<script type="module">标签加载,并且导入路径需要是有效的URL(通常是CDN)。
  3. 资源路径问题: 项目中引用的图片、模型文件(如MediaPipe的.task文件)或WebAssembly(WASM)文件,在CodePen上可能通过相对路径或CodePen内部机制访问。本地化时,这些资源需要确保能够被正确访问,通常意味着需要使用完整的CDN路径。
  4. 跨域资源共享(CORS): 当项目从不同源加载图片并将其绘制到<canvas>元素上进行处理时(例如图像分析),浏览器会触发CORS策略。此时,<img>标签需要添加crossorigin="anonymous"属性,以允许跨域加载并处理图片数据。

解决方案:构建一个完整的本地HTML文件

解决上述问题的最直接方法是创建一个独立的HTML文件,将所有必要的HTML结构、CSS样式和JavaScript代码整合进去,并确保所有外部依赖都通过正确的CDN链接引入。

1. HTML结构与元数据

一个标准的HTML文件需要包含<html>、<head>和<body>标签。<head>中应包含字符集声明、视口设置以及页面标题等基本元数据。

Swapface人脸交换
Swapface人脸交换

一款创建逼真人脸交换的AI换脸工具

Swapface人脸交换 45
查看详情 Swapface人脸交换
<!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>
登录后复制

2. 样式引入 (CSS Integration)

原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>
登录后复制

3. 脚本引入 (JavaScript Integration)

JavaScript部分是本地化过程中最容易出错的地方。

  • 首先,Material Components Web的JavaScript库也需要通过CDN链接引入。
  • 其次,MediaPipe Vision库的导入必须使用<script type="module">标签,并确保import语句指向正确的CDN URL。
  • WASM文件和模型文件的路径也必须是可访问的CDN地址。
  • 对于需要进行图像处理的<img>标签,务必添加crossorigin="anonymous"属性。
<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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号