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

本文详细指导如何将codepen上的前端项目,特别是涉及外部库和模块的mediapipe人脸关键点检测项目,成功部署到本地运行。文章聚焦于解决依赖引入、j*ascript模块加载、资源路径配置及跨域等常见问题,提供了一份完整的html代码示例,帮助开发者顺利实现codepen项目的本地化调试与开发。
在前端开发中,CodePen等在线代码编辑器为快速原型设计和分享提供了极大便利。然而,当需要将这些在线项目迁移到本地环境进行更深入的开发或调试时,开发者常会遇到一些挑战,尤其是在处理外部库、模块化脚本和资源路径方面。本文将以一个MediaPipe人脸关键点检测项目为例,详细阐述如何解决这些问题,确保项目能在本地顺利运行。
本地化CodePen项目面临的挑战
将CodePen项目直接复制粘贴到本地往往无法正常工作,主要原因包括:
- 外部CSS/JS库的引入方式差异: CodePen通常通过其UI界面或特定的预处理器语法(如SCSS的@use)来管理外部库。在本地,这些库需要通过标准的标签引入CSS文件,或通过<script>标签引入J*aScript文件,且通常需要指向CDN地址。</script>
- J*aScript模块化: 现代J*aScript项目广泛使用ES Modules(import/export语法)。在浏览器环境中,使用import语句的脚本必须通过
- 资源路径问题: 项目中引用的图片、模型文件(如MediaPipe的.task文件)或WebAssembly(WASM)文件,在CodePen上可能通过相对路径或CodePen内部机制访问。本地化时,这些资源需要确保能够被正确访问,通常意味着需要使用完整的CDN路径。
- 跨域资源共享(CORS): 当项目从不同源加载图片并将其绘制到
解决方案:构建一个完整的本地HTML文件
解决上述问题的最直接方法是创建一个独立的HTML文件,将所有必要的HTML结构、CSS样式和J*aScript代码整合进去,并确保所有外部依赖都通过正确的CDN链接引入。
1. HTML结构与元数据
一个标准的HTML文件需要包含、
和标签。中应包含字符集声明、视口设置以及页面标题等基本元数据。<!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可以直接内联到
火龙果写作
用火龙果,轻松写作,通过校对、改写、扩展等功能实现高质量内容生产。
277
查看详情
<head>
<!-- ...其他meta标签... -->
<title>Face Landmarker</title>
<style>
/* 移除 @use "@material"; */
body {
font-family: helvetica, arial, sans-ser
if;
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. 脚本引入 (J*aScript Integration)
J*aScript部分是本地化过程中最容易出错的地方。
- 首先,Material Components Web的J*aScript库也需要通过CDN链接引入。
- 其次,MediaPipe Vision库的导入必须使用
- WASM文件和模型文件的路径也必须是可访问的CDN地址。
- 对于需要进行图像处理的
标签,务必添加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" 属性 -->
@@##@@
</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>
<!-- 核心 J*aScript 代码,使用 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人脸关键点检测的本地化实践的详细内容,更多请关注其它相关文章!
# 越秀区营销推广优化费用
# 自定义
# 移除
# 如何实现
# 复选框
# 背景色
# 是在
# 恩平网站建设推广价格
# 荆州网站建设制作
# 弹出
# 网站推广主要怎么做
# 推广网站怎么找人做推广
# 松原网站seo选哪家
# 沁源网站推广公司
# 怎么优化网站就在易速达
# 浮梁百度seo优化
# 官网关键词营销排名查询
# css
# 将在
# 加载
# 如何在
# ssl
# access
# 浏览器
# seo
# npm
# 处理器
# go
# 前端
# js
# html
# java
# javascript
相关栏目:
【
科技资讯46185 】
【
网络学院92790 】
相关推荐:
MAC如何将整个网页截长图_MAC使用Safari的导出为PDF或第三方工具
TikTok国际版网页端快速入口 TikTok全球版短视频浏览教程
Word2013如何插入视频和音频媒体_Word2013媒体插入的多媒体支持
京东京造J1和网易云音乐氧气真无线有什么不同_国产电商蓝牙耳机音质对比
qq游戏跨平台入口_qq游戏多设备同步登录
Log4j Console Appender性能瓶颈与高并发优化策略
J*aScript Promise链中如何正确终止后续.then执行并处理错误
大麦的“候补”是什么意思 大麦候补购票规则【详解】
抓大鹅解压小游戏 抓大鹅摸鱼解压入口
c++ 命名空间怎么用 c++ namespace使用指南
我的世界官方游戏入口 我的世界官网平台直达链接
J*a应用集成GitHub CLI与API认证指南
Golang如何使用context实现超时取消_Golang context超时取消模式实践
Tailwind CSS line-clamp 布局问题解析与修复指南
QQ邮箱官方网页版登录 QQ邮箱个人邮箱快速访问
从J*aScript对象中精确提取指定属性的教程
python3时间如何用calendar输出?
俄罗斯搜索引擎Yandex指南 附2025年免登录官网入口
XML中包含HTML标签导致解析错误? 正确嵌入非XML数据的两种方法
Win11怎么开启省电模式_Win11电池节电模式自动开启
百度浏览器字体显示异常偏小_百度浏览器字体渲染修复方案
Yandex官网搜索引擎免登录_俄罗斯Yandex一键直达入口
快手赚钱渠道_快手收益来源
C#如何安全地从用户上传的XML文件中读取数据? 验证与清理策略
NetBeans Ant项目:自动化将资源文件复制到dist目录的教程
今日头条怎么同步内容到抖音_今日头条内容同步到抖音教程
sublime怎么格式化代码_sublime代码美化与一键排版插件配置
厨房不锈钢水槽发黑生锈怎么处理_水槽用可乐+锡纸2分钟抛亮如新
J*aScript中管理异步API调用:确保操作顺序与数据一致性
taptap防沉迷怎么解除 taptap解除健康系统限制说明【2025最新】
UE5.7引擎表现爆炸优化无敌!5090跑4K稳定60FPS
小红书网页版入口链接分享 小红书官网直接进
深入理解J*aScript中的B样条曲线与节点向量生成
ACG动漫手机版官网入口 手机ACG动漫APP在线观看正版
解决Python单元测试中Mock异常方法调用计数为零的问题
将HTML动态表格多行数据保存到Google Sheet的教程
没有大陆身份证/银行卡如何实名微信? 亲测有效的几种方法分享
谷歌浏览器一键优化方案_谷歌浏览器直达主页极速不卡版
自定义Bag-of-Words实现:处理带负号的词汇权重
Go调试环境为何无法启动_Go调试器启动失败原因与解决策略
星露谷物语官网入口 星露谷物语游戏官网入口
c++中的const_cast和reinterpret_cast怎么用_c++四种类型转换
解决 Vaadin 8 中大文件音频播放与定位时出现的 IOException
J*a 递归快速排序中静态变量的状态管理与陷阱
Lar*el如何正确地在控制器和模型之间分配逻辑_Lar*el代码职责分离与架构建议
快手官方唯一登录入口 谨防山寨钓鱼网站
LocoySpider如何部署到云服务器_LocoySpider云部署的远程配置
在J*a中如何使用Exception包装底层异常_异常包装与信息传递方法说明
蛙漫安全无毒 官方认证的绿色入口
Win11怎么修改默认浏览器_Windows 11设置Chrome为默认


2025-11-10
浏览次数:次
返回列表
if;
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>