This is a step-by-step tutorial to set up a Python environment for the Computer Vision course. This setup will involve installing Python, essential libraries like OpenCV, NumPy, and Matplotlib, and using a virtual environment to keep your project dependencies organized.
Download Python:
Install Python:
Verify the Installation:
python --version
pip
(Python's package installer)pip
typically comes with Python, but if it's not installed, follow these steps:
Check if pip
is installed:
pip --version
If it's not installed, you can install it by following the official instructions.
A virtual environment helps to isolate your Python projects and avoid conflicts between different dependencies.
Install virtualenv
(if not already installed):
pip install virtualenv
Create a new virtual environment:
cd path/to/your/project-folder
cv_env
:
virtualenv cv_env
Activate the virtual environment:
cv_env\Scripts\activate
macOS/Linux:
source cv_env/bin/activate
Once activated, your terminal should show the virtual environment name in parentheses (e.g., (cv_env)
).
You need to install several essential libraries to work on computer vision tasks:
Install OpenCV:
OpenCV is a library for real-time computer vision tasks.
pip install opencv-python
Install NumPy:
NumPy is used for handling arrays, which are crucial for image manipulation in computer vision.
pip install numpy
Install Matplotlib:
Matplotlib is a library for plotting and visualizing images.
pip install matplotlib
Install Scikit-Image:
Scikit-Image provides algorithms for image processing tasks.
pip install scikit-image
Install Jupyter Notebook (Optional but Recommended):
Jupyter Notebook allows you to write and execute Python code in a web-based environment, which is great for experimenting with code.
pip install notebook
Create a Python file (e.g., test_installation.py
) or open a Jupyter Notebook and write the following code to check if everything is installed correctly:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load an image using OpenCV
img = cv2.imread('sample_image.jpg')
# Check if the image was loaded correctly
if img is not None:
print("OpenCV is working correctly!")
else:
print("Error loading the image!")
# Display the image using Matplotlib
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Test Image')
plt.show()
Run this code (python test_installation.py
) in your virtual environment to verify that everything is working. You should see the message “OpenCV is working correctly!” and the image should display in a plot.
When you're done working in your virtual environment, you can deactivate it by running the following command:
deactivate
To keep track of all the libraries you’ve installed, you can generate a requirements.txt
file:
pip freeze > requirements.txt
This file can later be used to install the same dependencies in another environment by running:
pip install -r requirements.txt
Launch Jupyter Notebook:
In your terminal, inside the virtual environment, type:
jupyter notebook
Create a New Notebook: In the web interface, click on New and select Python 3 to create a new notebook.
Start Coding: Write and execute your Python code in the browser. For example, start by importing libraries:
import cv2
import numpy as np
import matplotlib.pyplot as plt
Since the course covers deep learning topics, you will also need libraries like TensorFlow or PyTorch. We will be using PyTorch:
Install PyTorch: Follow instructions from PyTorch's official website to install the appropriate version for your system.
You have now set up a Python environment for your computer vision course. This environment includes essential libraries like OpenCV, NumPy, and Matplotlib. You’ve also created a virtual environment to keep your project dependencies clean and installed Jupyter Notebook for interactive coding. Finally, you have installed PyTorch for deep learning.