transformers.js实现浏览器中离线目标监测识别代码

代码语言:html

所属分类:其他

代码描述:transformers.js实现浏览器中离线目标监测识别代码

代码标签: transformers.js 浏览器 离线 目标 监测 识别 代码

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

<!DOCTYPE html>
<html lang="en">
<link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/normalize.css">
    <meta charset="UTF-8" />
<style>
    html, body {
    margin: 20px;
    font-family:Arial, Helvetica, sans-serif;
}

.container {
    margin: 0 auto;
    width: 400px;
    display: flex;
    flex-direction: column;
    align-items: center;
}


.upload-button {
    border: 2px solid black;
    padding: 8px 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
}

input[type="file"] {
    display: none;
}

.upload-icon {
    width: 30px;
    margin-right: 10px;
}

#image-container {
    margin-top: 20px;
    position: relative;
}

#image-container img {
    width: 400px;
}
  
.bounding-box {
    position: absolute;
    box-sizing: border-box;
}
  
.bounding-box-label {
    position: absolute;
    color: white;
    font-size: 12px;
}
  
</style>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Web Worker Example</title>
  </head>
  <body>
    <main class="container">
      <label class="custom-file-upload">
        <input id="file-upload" type="file" />
        <img class="upload-icon" src="//repo.bfw.wiki/bfwrepo/icon/632819a7db7b3.png" /> Upload image
      </label> 
      <div id="image-container">
      </div>
      <p id="status"></p>
    </main>
    <script  type="module">
        import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.5.3';
env.allowLocalModels = false;

// Returns a radom hex color code, i.e. #CD5C5C
function generateRandomColor(){
    let maxVal = 0xFFFFFF;
    let randomNumber = Math.random() * maxVal; 
    randomNumber = Math.floor(randomNumber);
    randomNumber = randomNumber.toString(16);
    let randColor = randomNumber.padStart(6, 0);   
    return `#${randColor.toUpperCase()}`
}


function getScaledCoordinates(box, img) {
    // Get the original dimensions of the image
    const originalWidth = img.naturalWidth;
    const originalHeight = img.naturalHeight;

    // Get the scaled dimensions of the image
    const scaledWidth = img.offsetWidth; // The image should be exactly 400px wide
    const scaledHeight = img.offsetHeight;

    // Calculate the scaling factor
    const scale = scaledWidth / originalWidth;

    // Scale the box boundaries according to the scaled image
    let ymax = box.ymax * scale;
    let xmax = box.xmax * scale;
    let ymin = box.ymin * scale;
    let xmin = box.xmin * scale;
    
    // Make sure the minimum values are are at least 0 and the maximum values
    // are less than the width/height of the image
.........完整代码请登录后点击上方下载按钮下载查看

网友评论0