OpenCV with Azure Functions

Hello World,

Have you ever used OpenCV in your projects? probably yes if you are on this page

And what about Azure Functions?

if the answer is true to both then what about creating a Python Azure Function

with OpenCV module?

Lest try it out.

The main problem with Azure Function is that in Python it supports only version 3.6

and not 3.7. I’m using the latest version and got into a problem where I compiled the code it got me errors that the version is not supported, too bad but we can override this.

how?

let’s start from step 1 and it’s creating your Azure Function project and the steps are:

Install Python 3.6 version, if you have another version it’s not a problem but don’t add the path to the main directory when installing.

  1. Install Azure Function Tools with NPM

npm install -g azure-functions-core-tools

if you don’t have NPM to go NPM Install site

2. Install Azure CLI from Microsoft

3. Next is the most important part and its the support of version 3.6 even if you have a default other versions

run this command in your CMD and don’t close it

py -3.6 -m venv .venv

.venv\scripts\activate

now 3.6 is your version in the CMD

4. install OpenCV and Numpy 

pip install opencv-python nupy

5. craete your Azure Function project and select Python

func init OpenCVFunction

 

6. go to the project directory and run 

func new

select you preferences for your function, I love HTTP trigger

Now you have a project and you need to make changes to the Python main file

go to the project file __init__.py, this is the file that the function is running.

add the models you need to it.

One important Note about publishing to the web:

Azure Function same as Azure WebApps is installed on drive D of the VM that is running the function in case you select a windows plan. in that case, you will need to change the path to the model location with the next command:

sys.path.append(‘D:/home/site/tools/Lib/site-packages’)

 

Congratulations you have A working Function with Python and OpenCV

Have a look at that output as you start the service with the next command:

func host start

And Voila!

Add a Comment

Your email address will not be published. Required fields are marked *