Created
February 16, 2019 22:19
-
-
Save LiamHz/d27dd99dfe4b77f274d8c4ea384aa128 to your computer and use it in GitHub Desktop.
This file contains 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
from google.colab.output import eval_js | |
from base64 import b64decode | |
VIDEO_HTML = """ | |
<video autoplay | |
width=800 height=600></video> | |
<script> | |
var video = document.querySelector('video') | |
navigator.mediaDevices.getUserMedia({ video: true }) | |
.then(stream=> video.srcObject = stream) | |
var data = new Promise(resolve=>{ | |
video.onclick = ()=>{ | |
var canvas = document.createElement('canvas') | |
var [w,h] = [video.offsetWidth, video.offsetHeight] | |
canvas.width = w | |
canvas.height = h | |
canvas.getContext('2d') | |
.drawImage(video, 0, 0, w, h) | |
video.srcObject.getVideoTracks()[0].stop() | |
video.replaceWith(canvas) | |
resolve(canvas.toDataURL('image/jpeg', %f)) | |
} | |
}) | |
</script> | |
""" | |
def take_photo(filename='photo.jpg', quality=0.8): | |
display(HTML(VIDEO_HTML % quality)) | |
data = eval_js("data") | |
binary = b64decode(data.split(',')[1]) | |
with open(filename, 'wb') as f: | |
f.write(binary) | |
return len(binary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment