kalidokit+live2dcubismcore+live2d+face_mesh实现摄像头捕获真人头部表情驱动二次元数字人虚拟人面捕效果代码

代码语言:html

所属分类:其他

代码描述:kalidokit+live2dcubismcore+live2d+face_mesh实现摄像头捕获真人头部表情驱动二次元数字人虚拟人面捕效果代码,结合了mediapipe及pixi-live2d-display等人工智能技术,可以用于数字人直播。

代码标签: kalidokit live2dcubismcore live2d face_mesh 摄像头

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />

   
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/live2dcubismcore.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/live2d.js"></script>
    <!--     PixiJS Renderer -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/pixi.6.5.2.js"></script>
    <!--     PixiJS Live2D Plugin -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/pixi-live2d-display.index.min.js"></script>
    <!--     Mediapipe or Tensorflow.js -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/face_mesh.js"></script>

    <!--     Mediapipe Drawing Tools -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/drawing_utils.js"></script>
    <!--     Mediapipe Camera Tools -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/camera_utils.js"></script>
    <!--     Kalidokit Kinematic Solver -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/kalidokit.umd.js"></script>
<style>
  
h1 {
    font-family: "Jelly";
    font-size: 32px;
    color: white;
    position: absolute;
    top: -12px;
    left: 16px;
    font-weight: normal;
    -webkit-font-feature-settings: "liga";
    font-feature-settings: "liga";
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
h1:hover {
    color: #13a3f3;
}
a,
a:visited,
a:link {
    text-decoration: none;
    color: #222;
    display: inline-block;
}
a:hover,
a:visited:hover,
a:link:hover {
    color: #13a3f3;
    cursor: pointer;
}
.preview {
    display: flex;
    flex-direction: column;
    position: absolute;
    bottom: 16px;
    right: 16px;
    overflow: hidden;
    border-radius: 8px;
    background: #222;
}
video {
    max-width: 400px;
    height: auto;
    transform: scale(-1, 1);
}
.preview canvas {
    transform: scale(-1, 1);
}
body {
    margin: 0;
}
canvas {
    display: block;
}
.guides {
    position: absolute;
    bottom: 0;
    left: 0;
    height: auto;
    width: 100%;
    z-index: 1;
}
nav {
    position: absolute;
    top: 12px;
    right: 16px;
    display: flex;
}
nav img {
    width: 36px;
    height: auto;
    margin-left: 22px;
}
section {
    position: absolute;
    bottom: 0;
    right: 0;
    padding: 32px 0 16px 0;
    display: flex;
    justify-content: center;
    width: 100%;
    background: rgb(0, 0, 0);
    background: linear-gradient(0deg, rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 0) 100%);
    color: white;
    z-index: 2;
    /* border-radius: 40px; */
}
p {
    color: white;
    padding: 8px 16px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue",
        sans-serif;
    margin: 0;
}
.preview a {
    border-radius: 24px;
    margin: 0 4px;
}
.preview a:hover {
    background: #ffffff20;
}
a.current {
    background: #13a3f3;
    pointer-events: none;
}

.linkOut {
    position: absolute;
    bottom: 16px;
    left: 16px;
    display: inline;
    color: black;
    padding: 0;
}

@media only screen and (max-width: 600px) {
    video {
        max-width: 160px;
    }
}

</style>
  </head>
  <body>
    <div class="preview">
      <video class="input_video" width="1280px" height="720px" autoplay muted playsinline></video>
      <canvas class="guides"></canvas>
      <section>
        <a class="current" 
          ><p>Live2D</p></a
        >
        <a><p>VRM</p></a>
      </section>
    </div>

   
    <canvas id="live2d"></canvas>

    <script type="module" >
        const {
  Application,
  live2d: { Live2DModel }
} = PIXI;

// Kalidokit provides a simple easing function
// (linear interpolation) used for animation smoothness
// you can use a more advanced easing function if you want
const {
  Face,
  Vector: { lerp },
  Utils: { clamp }
} = Kalidokit;

// Url to Live2D
const modelUrl =
  "//repo.bfw.wiki/bfwrepo/images/live2dm3/Haru/Haru.model3.json";

let currentModel, facemesh;

const videoElement = document.querySelector(".input_video"),
  guideCanvas = document.querySelector("canvas.guides");

(async function main() {
  // create pixi application
  const app = new PIXI.Application({
    view: document.getElementById("live2d"),
    autoStart: true,
  transparent: true,
    backgroundAlpha: 0,
    resizeTo: window
  });

  // load live2d model
  currentModel = await Live2DModel.from(modelUrl, { autoInteract: false });
  currentModel.scale.set(0.4);
  currentModel.interactive = true;
  currentModel.anchor.set(0.5, 0.5);
  currentModel.position.set(window.innerWidth * 0.5, window.innerHeight * 0.8);

  // Add events to drag model
  currentModel.on("pointerdown", e => {
    currentModel.offsetX = e.data.global.x - currentModel.position.x;
    currentModel.offsetY = e.data.global.y - currentModel.position.y;
    currentModel.dragging = true;
  });
  currentModel.on("pointerup", e => {
    currentModel.dragging = false;
  });
  currentModel.on("pointermove", e => {
    if (currentModel.dragging) {
      currentModel.position.set(
        e.data.global.x - currentModel.offsetX,
        e.data.global.y - currentModel.offsetY
      );
    }
  });

  // Add mousewheel .........完整代码请登录后点击上方下载按钮下载查看

网友评论0