Install OpenCV 4 in PyCharm

What will we cover?

You want to start you first OpenCV project in PyCharm.

import cv2

And you get.

From PyCharm

You press Install package cv2, but you get.

Error message from PyChar (lower right corner).

What to do? No worries. We will cover that in this survival guide and it is not complex.

Step 1: Understand how PyCharm works with a Virtual Environment

When you create a new project in PyCharm you get promoted by this screen (PyCharm 2020.2).

Creating a project OpenCV in PyCharm

It says Python interpreter: New Virtualenv environment. What does that mean?

Well, it creates a isolated environment to have your project in. Then each project can have it own dependencies and libraries without impacting other projects.

Remember kindergarten? There was only one sandbox, and there was not enough for multiple projects in it. Like building a sand castle, making a river, and what ever you did as kid. The problem was, if you wanted to build a castle while your fellow kindergarten friends wanted to play mountain collapse (you know when a mountain collapses). Then their game would destroy your well engineered 5 feet tall castle. It was the beginning of a riot.

Think of a kindergarten where there is one sandbox for each project you could image. One for castle building. One for mountain collapse. You see. Now everyone can play in their own world or environment.

The virtual environment is like that. You can go crazy in it without destroying other awesome projects you do. Hence, if you feel like making a mountain collapse project, you should not fear it will destroy your well engineered castle project.

Step 2: How does this virtual environment work, and why does it matter for OpenCV?

Good question.

If you follow some manual online you might end up installing OpenCV on your base system and not in the virtual environment in your project.

But where is the virtual environment located. It depends on two things. First, where are your projects located. Second, what is the name of your project.

I used the default location when I installed PyCharm, which is PyCharmProjects in my home folder. Further, in this case I called the project OpenCV.

If I open a command line I can type the following to get to the location.

Command line terminal

Then you will see a folder called venv, which is short for virtual environment. Go into that folder and follow down in the bin (binary) folder.

Command line terminal

Now you are located where you can install the OpenCV library.

Step 3: Installing the OpenCV library in your virtual environment

Use pip is the package manager system for Python. You want to ensure you use the pip from the above library.

./pip install opencv-python
From command line terminal

You might get a bit different output, as I already had the library cached.

Back in PyCharm it will update and look like this.

Back in PyCharm the red line disappeared

Now you are ready for your first test.

Step 4: Testing that OpenCV works

Let’s find a picture.

Castle

Download the above image and save it as Castle.png in your project folder.

import cv2

img = cv2.imread("Castle.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Over the Clouds", img)
cv2.imshow("Over the Clouds - gray", gray)

cv2.waitKey(0)
cv2.destroyAllWindows()

Which should result in something like this.

The end result

Leave a Reply