gpt4 book ai didi

javascript - 为什么这个 Jsfiddle 不起作用 : tracking. js 人脸检测示例

转载 作者:行者123 更新时间:2023-11-28 15:23:39 24 4
gpt4 key购买 nike

我正在尝试获取 http://trackingjs.com/examples/face_tag_friends.html 的 JSFiddle 示例有效,但悬停效果并不像网站所示那样有效。这是我的 JSFiddle:

https://jsfiddle.net/lolptdr/25yqfyjo/6/

我必须在 raw.githubusercontent.com 上使用代理,并将其更改为 raw.githack.com,以获取 HTML 中引用的外部脚本,以绕过 MIME 类型投诉。没有其他控制台错误,那么还有什么问题吗?

我还可以检查什么才能获得与trackingjs.com网站上显示的相同效果?

window.onload = function() {
var img = document.getElementById('img');
var tracker = new tracking.ObjectTracker('face');
tracking.track(img, tracker);
tracker.on('track', function(event) {
event.data.forEach(function(rect) {
plotRectangle(rect.x, rect.y, rect.width, rect.height);
});
});
var friends = ['Thomas Middleditch', 'Martin Starr', 'Zach Woods'];
var plotRectangle = function(x, y, w, h) {
var rect = document.createElement('div');
var arrow = document.createElement('div');
var input = document.createElement('input');
input.value = friends.pop();
rect.onclick = function name() {
input.select();
};
arrow.classList.add('arrow');
rect.classList.add('rect');
rect.appendChild(input);
rect.appendChild(arrow);
document.getElementById('photo').appendChild(rect);
rect.style.width = w + 'px';
rect.style.height = h + 'px';
rect.style.left = (img.offsetLeft + x) + 'px';
rect.style.top = (img.offsetTop + y) + 'px';
};
};
* {
margin: 0;
padding: 0;
font-family: Helvetica, Arial, sans-serif;
}
.demo-title {
position: absolute;
width: 100%;
background: #2e2f33;
z-index: 2;
padding: .7em 0;
}
.demo-title a {
color: #fff;
border-bottom: 1px dotted #a64ceb;
text-decoration: none;
}
.demo-title p {
color: #fff;
text-align: center;
text-transform: lowercase;
font-size: 15px;
}
.demo-frame {
background: url(frame.png) no-repeat;
width: 854px;
height: 658px;
position: fixed;
top: 50%;
left: 50%;
margin: -329px 0 0 -429px;
padding: 95px 20px 45px 34px;
overflow: hidden;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
}
.demo-container {
width: 100%;
height: 530px;
position: relative;
background: #eee;
overflow: hidden;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
}
.dg.ac {
z-index: 100 !important;
top: 50px !important;
}
/* example's CSS */

#photo:hover .rect {
opacity: .75;
transition: opacity .75s ease-out;
}
.rect:hover * {
opacity: 1;
}
.rect {
border-radius: 2px;
border: 3px solid white;
box-shadow: 0 16px 28px 0 rgba(0, 0, 0, 0.3);
cursor: pointer;
left: -1000px;
opacity: 0;
position: absolute;
top: -1000px;
}
.arrow {
border-bottom: 10px solid white;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
height: 0;
width: 0;
position: absolute;
left: 50%;
margin-left: -5px;
bottom: -12px;
opacity: 0;
}
input {
border: 0px;
bottom: -42px;
color: #a64ceb;
font-size: 15px;
height: 30px;
left: 50%;
margin-left: -90px;
opacity: 0;
outline: none;
position: absolute;
text-align: center;
width: 180px;
transition: opacity .35s ease-out;
}
#img {
position: absolute;
top: 50%;
left: 50%;
margin: -173px 0 0 -300px;
}
<script src="https://raw.githack.com/eduardolundgren/tracking.js/master/build/tracking.js"></script>
<script src="https://raw.githack.com/eduardolundgren/tracking.js/master/build/data/face.js"></script>
<div class="demo-title">
<p><a href="http://trackingjs.com" target="_parent">tracking.js</a> - hover image to see all faces detected</p>
</div>
<div class="demo-frame">
<div class="demo-container"> <span id="photo"><img id="img" src="https://raw.githubusercontent.com/eduardolundgren/tracking.js/master/examples/assets/faces.jpg" /></span>

</div>
</div>

最佳答案

以上所有答案都很好地解决了为什么此方法失败的原因,但这里是 jsfiddle 中使用 flickr 图像的 tracker.js 的工作示例: http://jsfiddle.net/rambutan2000/v5v49bax/

Flickr 似乎在 header 中正确设置了 Access-Control-Allow-Origin。我使用代理 (crossorigin.me) 取得的成功有限。

这是此示例的简化版本: https://trackingjs.com/examples/face_hello_world.html

首先,我必须获取 Tracker 的有效 URL,我使用了此服务: http://rawgit.com 。查看jsfiddle中的“外部资源”。

我基于一个示例,该示例使用 XMLHttpRequest 将图像数据作为缓冲区检索,然后将其加载到 img 元素中。这消除了 img 元素上的一些 CORS 问题,因为它源自代码而不是 URL。其余部分直接从上面引用的 Tracker 示例中摘取。

JS:

// use http://rawgit.com/ to get js urls from github
// use https://crossorigin.me/ to get around CORS for image reference
function _arrayBufferToBase64(buffer) {
var binary = ''
var bytes = new Uint8Array(buffer)
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return window.btoa(binary);
}

window.plot = function(x, y, w, h) {
var $rect = $("<div></div>");
$rect.addClass("rect");
$rect.offset({ top: y, left: x });
$rect.width(w).height(h);
$("#demo-container").append($rect);
};

var imgURL = 'https://c1.staticflickr.com/4/3943/15715482121_d7120a6e0b_z.jpg'; // Works!
//var imgURL = 'https://placeimg.com/640/480/people'; // Does not work
//var imgURL = 'https://crossorigin.me/https://placeimg.com/640/480/people'; // Works!


var oReq = new XMLHttpRequest();
oReq.open("GET", imgURL, true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
var arrayBuffer = oReq.response; // Note: not oReq.responseText
if (arrayBuffer) {
var x = imgURL.split('.');
var ext = x[x.length - 1];
var b64img = _arrayBufferToBase64(arrayBuffer);
$("#img").attr('src', 'data:image/' + ext + ';base64,' + b64img).appendTo($('body'));

var img = document.getElementById('img');
var tracker = new tracking.ObjectTracker(['face']);

tracker.setStepSize(1.7);
tracking.track('#img', tracker);

tracker.on('track', function(event) {
event.data.forEach(function(rect) {
console.log(rect);
window.plot(rect.x, rect.y, rect.width, rect.height);
});
});
}
};

oReq.send(null);

HTML:

<div id="demo-container">
<img id="img" src="" />
</div>

CSS:

.rect {
position:absolute;
border-style: solid;
border-width: 2px;
border-color: blue;
}

#demo-container {
position:absolute;
}

关于javascript - 为什么这个 Jsfiddle 不起作用 : tracking. js 人脸检测示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com