Get More Out of Google Colab

From using the webcam to running JavaScript to rendering HTML to…

Sebastian Theiler
Feb 1 · 5 min read

I’m sure you’ve heard of Google Colab by now. If you haven’t, it’s a free cloud-compute service offered by Google that provides a high-quality code editor right in your browser.

With that out of the way, let’s get into 5 tips and tricks for you to get more out of Google Colab.

Topics:

  1. Uploading and Downloading Files
  2. Making tuning Hyperparameters Easy with Form Fields
  3. Running JavaScript and HTML
  4. Using the Webcam (and turning those images into NP arrays)

Mounting your Google Drive

Mounting your Drive is easy:

This will create an authorization link that provides a code for you to paste into the terminal. After that, your Google Drive has successfully been mounted!

You can print a list of your Drive’s contents with:

!ls drive/My\ Drive

Putting ! before a terminal command allows you to directly run that command through Google Colab (and Jupyter Notebooks)

Real-world Example

Note, this will write files to your drive.

Uploading and Downloading Files

To upload a file (or multiple), run:

And on the other hand, you can download a file using:

That’s all there is to it! Uses are pretty self-explanatory here.

Making tuning Hyperparameters Easy with Form Fields

Google Colab makes that easy for us. They give the following example:

Which will build the following:

One can only imagine how easy it is to tune a model like this:

Source code available here

Running JavaScript and HTML

JavaScript

`display` is a built-in function

Alternatively, we can transform an entire cell into JS using the %%javascript magic.

And if you head into the browser console using F12 or ctrl/cmnd+shift+i then navigating to the ‘Console’ tab, you should see something like:

You can also print directly to the cell terminal by swapping console.log(message) for:

document.querySelector("#output-area").appendChild(document.createTextNode(message));

Simple Python-to-JS Interfacing

js_code = \
'''
let message = {};
console.log(message)
'''.format(9+10)

If you’d like to learn more about JavaScript in Google Colab (including advanced Python-to-JS interfacing), I’d recommend reading the notebook here.

HTML

Just run something like:

And see the results!

Using the Webcam

Which you can call as follows:

I’ve found that this function doesn’t seem to work on Brave Web Browser

If you don’t understand it, that’s perfectly fine — to be honest, my JavaScript knowledge isn’t quite adequate to understand it perfectly. Nevertheless, the function creates a video that will take a picture when the ‘Capture’ button is clicked, and save that picture to a file.

Click “Capture” to save the photo to a file

Converting to a Numpy Array

This would be a lot more efficient if someone could find a way to convert the binary of an image directly to an NP array

Which you then use as follows:

After clicking capture through the previous interface, you will get an NP array you can visualize

Instantly Taking a Photo

This will still have some delay, but won’t wait for user confirmation

Which you can call again as follows:

Uses

Maybe you’ve seen the Teachable Machine experiment Google released a year ago. Now, using free cloud compute and your built-in webcam, you can develop your own copy.

Or maybe you want to build a facial recognition system for yourself? Now you can do it without a powerful computer.

The possibilities are limitless.

Conclusion

I hope you’ve found at least one of these tips and tricks interesting and that you can find a way to incorporate them into some of your future projects.


And as always, until next time,

Happy coding!


The source-code for this article is available here or in Colab here.

This is a great article by ShIvam Rawat about stopping Google Colab from disconnecting

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sebastian Theiler

Written by

I write articles about data science, artificial intelligence, and machine learning

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

More From Medium

More from Analytics Vidhya

More from Analytics Vidhya

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade