Our First Python Computer Vision Program with OpenCV

Alejandro Gomez
3 min readJul 13, 2023

--

Computer Vision

AI and Computer Science field concerned with computer’s ability to see and extract/understand information from images and videos.

Here’s a lightning speed tutorial to create your first computer vision program.

This tutorial assumes basic knowledge of VSCode and Python.

How Our Program Will Work

  • Create an object instance
  • Convert image to grayscale
  • ~~black box algorithm~~
  • Rectangulate detected face
Black Box Model — Implementation is hidden

Preparing Our Program

  • Open VSCode and create a directory called cv_app
  • Find yourself an image with a face (png file or jpg file) and insert it into our directory
  • Install the OpenCV wrapper for Python
$ pip install opencv-python
  • Create a Python file called main.py

Inside your directory -

Your file tree should look like the below diagram and it should contain an image of your choosing and an empty python file

cv_app/
├─ main.py
├─ dwight.png
dwight.png

So here’s our code:

import cv2


# Load the cascade
face_detection = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)

# Read the input image
img = cv2.imread("dwight.png")

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = face_detection.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)

# Draw rectangles around the faces
for x, y, w, h in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 3)

# Save the output image
cv2.imwrite("face_recognized.png", img)

# OPTIONAL this will create a new window that displays the new face
cv2.imshow("face_recognized.png", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

To run this file from your VSCode terminal, make sure you are in the correct file path and then enter the command:

$ python main.py
face_recognized.png

Nice! We detected a face! A window with the new png will have popped up. Go ahead and enjoy this new feat!

Your file tree will now look like this:

cv_app/
├─ main.py
├─ dwight.png
├─ face_recognized.png

I hope you enjoyed :)

Possible Issues and Debugging

Python Interpretter Issues:

Make sure you have the correct interpretter selected

downloading opencv_python

source: https://github.com/pypa/pip/issues/9023

Navigate to C:/Python311 on your file explorer and right click on the folder itself

Select “Properties” -> Select “Security” tab up top -> Edit permissions -> Select “User”, change permissions, apply changes

PyLint causes errors in VSCode due to importing cv2

source: https://stackoverflow.com/questions/51593147/cv2-python-has-no-imread-member

in your VSCode window, press the following: ctrl + shift + P

In the search bar that pops up, type in: “Preferences: Open User Settings (JSON)”

This opens up a file called settings.json Go to the bottom of it and add this line:

"python.linting.pylintArgs": [
"--generated-members=cv2.*"
],

So your file will look like this. Save the file and save your code again. Run your program.

{    
...

"someSetting": "SomeSetting",
"python.linting.pylintArgs": [
"--generated-members=cv2.*"
],
}

General Practices

If you’ve done the above, go ahead and bring up the search palette in VSCode using ctrl + shift + P and execute the two following:
“Restart Extension Host” and “Restart Window”

Sources for Reference:

https://www.tutorialspoint.com/artificial_intelligence_with_python/artificial_intelligence_with_python_computer_vision.htm

https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml

https://pypi.org/project/opencv-python/

--

--