Skip to content

Instantly share code, notes, and snippets.

@emadehsan
Created January 29, 2020 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emadehsan/302c1bdbae051a1d6145d849c632efd4 to your computer and use it in GitHub Desktop.
Save emadehsan/302c1bdbae051a1d6145d849c632efd4 to your computer and use it in GitHub Desktop.
async function takePhoto(quality) {
// create html elements
const div = document.createElement('div');
const video = document.createElement('video');
video.style.display = 'block';
// request the stream. This will ask for Permission to access
// a connected Camera/Webcam
const stream = await navigator.mediaDevices.getUserMedia({video: true});
// show the HTML elements
document.body.appendChild(div);
div.appendChild(video);
// display the stream
video.srcObject = stream;
await video.play();
// Resize the output (of Colab Notebook Cell) to fit the video element.
google.colab.output
.setIfrmeHeight(document.documentElement.scrollHeight, true);
// capture 5 frames (for test)
for (let i = 0; i < 5; i++) {
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
img = canvas.toDataURL('image/jpeg', quality);
// Call a python function and send this image
google.colab.kernel.invokeFunction('notebook.run_algo', [img], {});
// wait for X miliseconds second, before next capture
await new Promise(resolve => setTimeout(resolve, 250));
}
stream.getVideoTracks()[0].stop(); // stop video stream
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment