Would it be possible to install OTB in Colab Python?

I would like to use OTB in the Collaboratory Google Python environment, would there be a way to install the OTB in the Colab environment?

Hi

According to Google Colab
You have to install things with pip. pip is not yet supported by OTB, but maybe if you apt install otb and try import otbApplication as otb, it may work

Can you try and make a feedback to us?

Thank you

Hi,

We already did that for educational purposes(“OTB guided tour”) in theses notebooks :
https://github.com/otbformation/virtual-workshop

In that notebook, we download OTB binary archive (7.0, but it can be adapted to more recent versions !) and compile Python bindings from the notebook. We even install some third-parties libraries.

It works very fine, feel free to check it by yourself.

Best regards,

Yannick

It could work but it requires to install build deps + recompile python bindings every time the colab notebook is launched…that may take a while.

Since Colab is Ubuntu 20.04 env by default, it ships with python 3.8.
Only one apt package is missing for the .run script to work. And a symbolic link for the python lib.

So it should work with OTB >= 8.0, without any recompilation.
For OTB <= 7.4, you will need a python 3.5 env, so a virtualenv is required, if you don’t want to recompile python bindings.

Here is a working example, tested on Colab :

!python --version
!sudo apt-get update && sudo apt-get install file
!wget https://www.orfeo-toolbox.org/packages/OTB-8.1.1-Linux64.run
!bash OTB-8.1.1-Linux64.run --target /opt/otb
!ln -s /usr/lib/x86_64-linux-gnu/libpython3.8.so /opt/otb/lib/libpython3.8.so.rh-python38-1.0
import os
import sys

os.environ["OTB_APPLICATION_PATH"] = "/opt/otb/lib/otb/applications"
sys.path.append("/opt/otb/lib/python")

import otbApplication
otbApplication.Registry_GetAvailableApplications()

The env is set within python because you cannot just !source otbenv.profile in the notebook before import.
More env variables (GDAL / PROJ) could be missing, depending on the applications you intend to use.

Else you could check pyotb : it will take care of the env for you.

Cheers
Vincent

Sadly, python bindings are broken in Ubuntu packaging (even with ubuntugis).
The installation is working, but otbApplication.py is missing…
Probably because ubuntu’s default python version is too recent for OTB (Ubuntu 20.04 comes with py38, and OTB7. Ubuntu 22.04 is shipped with py310…)