HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<header>
<h1>【DEMO】人工知能を使って認識した人の顔に目線を入れてみる</h1>
</header>
<div class="container">
<p>ディープラーニングライブラリ <a href="https://js.tensorflow.org/" target="_blank">TensorFlow.js</a> と、人体の姿勢推定ができる機械学習モデル <a href="https://github.com/tensorflow/tfjs-models/tree/master/posenet" target="_blank">PoseNet Model</a>をつかって、顔写真に自動的に目線がはいるようにしてみました。<br>最大8人まで認識できます。</p>
</div>
<section class="controller">
<h4>画像データを選ぶ</h4>
<p>端末から画像を選ぶと、目線が描画されます。</p>
<input accept="image/*" id="input_img" type="file">
</section>
<div id="wrap">
<input type="checkbox" id="show_dots"><label for="show_dots">姿勢推定ドット表示</label>
<div class="container">
<div id="result"></div>
</div>
</div>
<section class="notes">
<p>画像が小さかったり、画像の大きさに対して写っている顔が小さい場合は精度が落ちるため、縦幅か横幅が1600px以上の画像をおすすめします。また、寝そべっているなど写真の向きに対して顔が横になっている場合は認識できないようです。<br>処理はすべて端末内で実行され、画像がサーバーに送信・保存されることはありません。</p>
<p>負荷の高い処理を行うため、端末のスペックによっては連続した操作や大きな画像を読み込むなどした場合にブラウザがクラッシュすることがあります。PC以外は画像を縮小する処理を挟んでからTensorFlowに投げているため、PCとスマホで結果が異なる場合があります。</p>
</section>
@keyframes load_ani {
0% {
transform: scale3d(1, 1, 1);
}
50% {
transform: scale3d(-1, 1, 1);
}
100% {
transform: scale3d(1, 1, 1);
}
}
html {
height: 100%;
font-size: 100%;
}
body {
margin: 0;
padding: 0;
height: 100%;
line-height: 1.6;
font-family: "ヒラギノ角ゴ Pro", "Hiragino Kaku Gothic Pro", "メイリオ", "Meiryo", sans-serif;
font-size: 1rem;
}
header {
margin: 0;
padding: 0;
background: #000;
h1 {
margin: 0;
padding: 1em;
font-size: 1em;
color: #ffffff;
}
}
#wrap {
padding: 1em;
border-top: 1px solid #737373;
border-bottom: 1px solid #000;
.container {
padding: 0;
}
}
.container {
margin: 0;
p {
padding: 0 1em;
font-size: 1em;
}
}
.sample {
margin: .5em;
padding: .5em;
background: #EAEAEA;
h4 {
font-size: 1rem;
margin: 0;
padding: 0;
}
&-item {
cursor: pointer;
}
}
body.loading .gallery {
opacity: .5;
pointer-events: none;
}
.gallery {
display: flex;
flex-wrap: wrap;
.current img {
box-shadow: 0 0 5px #0099ca;
}
> * {
display: inline-block;
margin: 0 1%;
width: calc(100% / 5 - 2%);
opacity: .5;
}
.current {
opacity: 1;
}
img {
vertical-align: bottom;
height: auto;
max-width: 100%;
}
}
.controller {
padding: 1em;
h4 {
margin: 0;
padding: 0;
}
p {
font-size: .8em;
margin: 0;
padding: 0;
}
}
.notes {
padding: 1em;
font-size: .8em;
border-top: 1px solid #737373;
}
label {
font-size: .8em;
line-height: 1;
}
#show_dots:checked ~ * .point {
display: block;
}
#result {
position: relative;
display: inline-block;
overflow: hidden;
vertical-align: bottom;
p {
font-size: 0.8em;
color: #454545;
}
&::before {
content: "😃";
position: absolute;
top: 50%;
left: 50%;
margin-top: -1.5rem;
margin-left: -1.5rem;
line-height: 1;
opacity: 0;
display: none;
z-index: 1;
font-size: 3rem;
}
&::after {
content: "";
position: absolute;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
background: #cacaca;
opacity: 0;
display: none;
}
img {
max-width: 100%;
height: auto;
vertical-align: bottom;
}
canvas {
max-width: 100%;
height: auto;
position: absolute;
top: 0;
left: 0;
vertical-align: bottom;
}
.mesen {
position: absolute;
background: #000000;
height: 1em;
transform: translate(-50%, -50%);
z-index: 1;
}
.point {
position: absolute;
z-index: 1;
display: none;
width: .5em;
height: .5em;
}
.point::before {
content: "";
display: block;
width: .5em;
height: .5em;
background: rgba(255, 255, 255, .5);
border-radius: 50%;
transform: translate(-50%, -50%);
opacity: 1;
box-shadow: 0 0 2px #0099ca;
}
.point {
&:hover::before {
background: #0099ca;
}
&::after {
display: none;
width: 4em;
color: #ffffff;
background: #000000;
padding: .3em .5em;
line-height: 1.2;
position: absolute;
left: 10px;
top: -.5em;
opacity: 0;
font-size: .8em;
}
&:hover::after {
display: inline-block;
opacity: .8;
}
}
$colors:(
0: #0000ff,
1: #ff0000,
2: #ffff00,
3: #00ffff,
4: #ff00ff,
5: #ff9933,
6: #99ff33,
7: #9933ff,
8: #33ff99,
9: #3399ff,
);
$parts: "nose", "leftEye", "rightEye", "leftEar", "rightEar", "leftShoulder", "rightShoulder", "leftElbow", "rightElbow", "leftWrist", "rightWrist", "leftHip", "rightHip", "leftKnee", "rightKnee", "leftAnkle", "rightAnkle";
@each $num, $color in $colors {
.pose#{$num} {
&::before {
box-shadow: 0 0 2px $color;
}
&:hover::before {
background: $color;
}
@each $part in $parts {
&.part-#{$part} {
&::after {
content: "pose #{$num}: #{$part}";
}
}
}
}
}
}
body.loading {
.controller {
opacity: .5;
pointer-events: none;
}
#result {
&::before {
display: block;
opacity: 1;
animation: load_ani 2s infinite;
}
&::after {
display: block;
opacity: 1;
}
}
}
(function(){
function mesen(img, points, ctx) {
// 目線を生成する
var r = preview.width / img.width;
var p = {};
for(var q = 0;q < points.length;q++) {
p[points[q].part] = points[q].position;
}
var prop = {
x: ( p.rightEye.x + p.leftEye.x ) / 2, // x座標(両目の中点)
y: ( p.rightEye.y + p.leftEye.y ) / 2, // y座標(両目の中点)
deg: Math.atan2( p.leftEye.y - p.rightEye.y, p.leftEye.x - p.rightEye.x ), // 目線の角度(両目の角度)
dis_ears: Math.sqrt( Math.pow( p.rightEar.x - p.leftEar.x, 2 ) + Math.pow( p.rightEar.y - p.leftEar.y, 2 ) ) / 2, // 横幅(両耳の距離の半分)
dis_eye: Math.sqrt( Math.pow( p.rightEye.x - p.leftEye.x, 2 ) + Math.pow( p.rightEye.y - p.leftEye.y, 2 ) ), // 横幅(両目の距離)
};
// 傾きの補助
if(p.rightEar.x > p.rightEye.x || p.leftEar.x < p.leftEye.x) {
// 横顔(目より耳が顔の内側)のとき角度をゼロにする
// prop.deg = 0;
}
// 目線の始点
prop.x1 = prop.x + Math.cos(prop.deg) * prop.dis_ears * -1;
prop.y1 = prop.y + Math.sin(prop.deg) * prop.dis_ears * -1;
// 目線の終点
prop.x2 = prop.x + Math.cos(prop.deg) * prop.dis_ears;
prop.y2 = prop.y + Math.sin(prop.deg) * prop.dis_ears;
// 目線の描画
ctx.beginPath();
ctx.moveTo(prop.x1, prop.y1);
ctx.lineTo(prop.x2, prop.y2);
ctx.lineWidth = Math.sqrt( Math.pow( prop.x1 - prop.x2, 2 ) + Math.pow( prop.y1 - prop.y2, 2 ) ) / 5; // 目線の横幅の1/5
ctx.closePath();
ctx.stroke();
prop = null;
}
function removeClass(classname, elm) {
[].forEach.call(elm,function(x){
x.classList.remove(classname);
});
}
function img_load(src) {
result.innerHTML = ''; // リセット
document.body.classList.add('loading');
result.appendChild(preview);
var img = new Image();
img.onload = function() {
var ratio = img.width / img.height;
function build() {
cnv.width = img.width;
cnv.height = img.height;
preview.src = img.src;
ctx.drawImage(img, 0, 0, img.width, img.height);
posenet.load().then(function(net) {
return net.estimateMultiplePoses(img, 0.5, flipHorizontal, outputStride, maxPoseDetections)
}).then(function(poses){
for(var i = 0;i < poses.length;i++) {
if(poses[i].score < 0.2) {
// 認識のスコアが低いときは無視する
continue;
}
mesen(img, poses[i].keypoints, ctx);
// ポイントを置く
for(var q = 0;q < poses[i].keypoints.length;q++) {
var point = document.createElement('div');
point.classList.add('point');
point.classList.add('pose' + i);
point.classList.add('part-' + poses[i].keypoints[q].part);
point.style.top = (poses[i].keypoints[q].position.y / img.height * 100) + '%';
point.style.left = (poses[i].keypoints[q].position.x / img.width * 100) + '%';
result.appendChild(point);
}
}
document.body.classList.remove('loading');
loading = false;
poses = null,img = null;
})
}
preview.id = 'preview';
preview.src = img.src;
result.appendChild(cnv);
ctx.drawImage(img, 0, 0, img.width, img.height);
if(resize && img.width * img.height > max_size * max_size) {
// 大きい画像は縮小してから処理する
var image = new Image();
if(ratio > 1) {
// 横長画像は横幅を基準に
cnv.width = max_size;
cnv.height = img.height * (cnv.width / img.width);
} else {
// 縦長画像は縦幅を基準に
cnv.height = max_size;
cnv.width = img.width * (cnv.height / img.height);
}
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, cnv.width, cnv.height);
image.onload = function() {
img = image;
image = null;
build();
}
image.src = cnv.toDataURL();
return;
} else {
build();
}
}
img.src = src;
}
var filereader = new FileReader();
var imageScaleFactor = 0.2;
var flipHorizontal = false;
var outputStride = 16;
var maxPoseDetections = 8;
var preview = new Image();
var result = document.getElementById('result');
var fileElement = document.getElementById('input_img');
var loading = false;
var ua = navigator.userAgent;
var max_size = 800, resize = false;
if (ua.indexOf('iPhone') > 0 || ua.indexOf('Android') > 0 && ua.indexOf('Mobile') > 0 || ua.indexOf('iPad') > 0 || ua.indexOf('Android') > 0) {
// PC以外は max_size でリサイズする
resize = true;
}
var cnv = document.createElement('canvas');
var ctx = cnv.getContext('2d');
fileElement.addEventListener('change', function(){
if (!this.files.length) {
return;
}
if(loading) return;
loading = true;
filereader.onload = function() {
img_load(filereader.result);
}
filereader.readAsDataURL(this.files[0]);
});
[].forEach.call(document.getElementsByClassName('sample-item'), function(x){
x.addEventListener('click', function(){
if(loading) return;
loading = true;
removeClass('current', document.getElementsByClassName('sample-item'));
this.classList.add('current');
var imgs = this.getElementsByTagName('img');
if(imgs.length > 0) {
img_load('./img/' + imgs[0].getAttribute('data-filename'));
}
});
});
})();
Also see: Tab Triggers