Last active
November 16, 2019 15:48
-
-
Save zaru/07a57cbdff321d7309dabb4bfabd3353 to your computer and use it in GitHub Desktop.
すべてが zaru になるサンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Document</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.js"></script> | |
<style type="text/css"> | |
#image { | |
position: relative; | |
} | |
#image img { | |
position: absolute; | |
top: 0px; | |
left: 0px; | |
} | |
.face { | |
} | |
.face img { | |
width: 100%; | |
height: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<input type="file" onchange="previewFile()"><br> | |
<div id="image"> | |
<img src="" alt="Image preview..."> | |
</div> | |
<script> | |
function previewFile() { | |
const preview = document.querySelector('img'); | |
const file = document.querySelector('input[type=file]').files[0]; | |
const reader = new FileReader(); | |
reader.addEventListener("load", function () { | |
const file = reader.result.split(',')[1] | |
preview.src = reader.result; | |
const key = '... your api key ...' | |
const url = `https://vision.googleapis.com/v1/images:annotate?key=${key}` | |
postApi(url, file) | |
}, false); | |
if (file) { | |
reader.readAsDataURL(file); | |
} | |
} | |
function postApi(url, data) { | |
axios.post(url, { | |
"requests": [ | |
{ | |
"image": { | |
"content": data | |
}, | |
"features": [ | |
{ | |
"type": "FACE_DETECTION" | |
} | |
] | |
} | |
] | |
}) | |
.then((res) => { | |
console.log(`statusCode: ${res.statusCode}`) | |
console.log(`statusCode: ${res.data}`) | |
res.data.responses[0].faceAnnotations.forEach(v => { | |
console.log(v.fdBoundingPoly.vertices) | |
const bound = v.fdBoundingPoly.vertices | |
const div = document.createElement('div') | |
div.style.position = 'absolute' | |
div.style.top = `${bound[0].y}.px` | |
div.style.left = `${bound[0].x}.px` | |
div.style.width = `${bound[1].x - bound[0].x}.px` | |
div.style.height = `${bound[3].y - bound[1].y}.px` | |
div.classList.add('face') | |
const img = document.createElement('img') | |
img.src = './zaru-circle.png' | |
img.style.transform = `rotate(${v.rollAngle}deg)` | |
div.appendChild(img) | |
document.getElementById('image').appendChild(div) | |
}) | |
}) | |
.catch((error) => { | |
console.error(error) | |
}) | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment