Skip to content

Instantly share code, notes, and snippets.

@LiamHz
Created February 16, 2019 22:19
Show Gist options
  • Save LiamHz/d27dd99dfe4b77f274d8c4ea384aa128 to your computer and use it in GitHub Desktop.
Save LiamHz/d27dd99dfe4b77f274d8c4ea384aa128 to your computer and use it in GitHub Desktop.
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