conda


Executing a Cron Job Within an Anaconda Environment

For those seeking to automate their Python scripts using cron jobs within an Anaconda environment, there’s a straightforward method to ensure your scripts run under the correct environment. This approach involves leveraging the capabilities of bash and the configuration of your cron environment to recognize and activate the necessary Anaconda environment. Here’s how to set it up:

  1. Export Anaconda Initialization to a Dedicated Configuration File First, isolate the Anaconda initialization snippet from your ~/.bashrc file by copying it to a new file, say ~/.bashrc_for_cron. This step ensures the cron environment can source the necessary configurations to activate Anaconda environments. Make sure that:
  • The file is readable by the user scheduling the cron job.
  • The file is secured against write access from other users to mitigate security risks.
  1. Configure Cron to Use Bash and Source the Anaconda Configuration Edit your crontab configuration by running crontab -e and prepend your cron jobs with two crucial lines:
   SHELL=/bin/bash
   BASH_ENV=~/.bashrc_for_cron

These lines configure cron to execute jobs using bash instead of the default sh shell and to source the Anaconda configurations from ~/.bashrc_for_cron. When invoked by cron, this setup ensures that bash is aware of the necessary environment to activate Anaconda environments.

  1. Activate Anaconda Environment Before Executing Your Script When scheduling your Python script in crontab -e, prefix the command with the conda activate instruction to switch to your desired Anaconda environment. For instance, to run a script at 12:30 AM every day within a specific Anaconda environment, your cron job entry would look like:
   30 0 * * * conda activate opencv_env; python /path/to/script.py

This ensures that the script executes within the context of the specified Anaconda environment, opencv_env, leveraging any dependencies or configurations defined within that environment.

By following these steps, you can seamlessly schedule and run Python scripts under specific Anaconda environments, leveraging cron’s scheduling capabilities while ensuring the correct environment and dependencies for your scripts.


Playing with MASK RCNN on videos .. again

Source code for the implementation that created this video will be uploaded soon.

A first attempt at using a pre-trained implementation of Mask R-CNN on Python 3, Keras, and TensorFlow. The model generates bounding boxes and segmentation masks for each instance of an object in each frame. It’s based on Feature Pyramid Network (FPN) and a ResNet101 backbone.

Setup

Conda / Anaconda

First of all, we installed and activated anaconda on an Ubuntu 20.04LTS desktop. To do so, we installed the following dependencies from the repositories:

sudo apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6;

Then, we downloaded the 64-Bit (x86) Installer from (https://www.anaconda.com/products/individual#linux).

Using a terminal, we followed the instructions here (https://docs.anaconda.com/anaconda/install/linux/) and performed the installation.

Python environment and OpenCV for Python

Following the previous step, we used the commands below to create a virtual environment for our code. We needed python version 3.9 (as highlighted here https://www.anaconda.com/products/individual#linux) and OpenCV for python.

source ~/anaconda3/bin/activate;
conda create --name MaskRNN python=3.9;
conda activate MaskRNN;
pip install numpy opencv-python;

Problems that we did not anticipate

When we tried to execute our code in the virtual environment:

python3 main.py --video="/home/bob/Videos/Live @ Santa Claus Village 2021-11-13 12_12.mp4";

We got the following error:

Traceback (most recent call last):
  File "/home/bob/MaskRCNN/main.py", line 6, in <module>
    from cv2 import cv2
  File "/home/bob/anaconda3/envs/MaskRNN/lib/python3.9/site-packages/cv2/__init__.py", line 180, in <module>
    bootstrap()
  File "/home/bob/anaconda3/envs/MaskRNN/lib/python3.9/site-packages/cv2/__init__.py", line 152, in bootstrap
    native_module = importlib.import_module("cv2")
  File "/home/bob/anaconda3/envs/MaskRNN/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

We realized that we were missing some additional dependencies for OpenCV as our Ubuntu installation was minimal. To fix this issue, we installed the following package from the repositories:

sudo apt-get update;
sudo apt-get install -y python3-opencv;