OpenCV is the most popular computer vision library today, and today we are going to learn how to install and use OpenCV, and how to access our camera. Then let’s take a look at how easy it is to write a face detection program, as simple as a few lines of code.

Before I begin, I assume that you already have some familiarity with Python. Of course, if you don’t think you’re up to it, here are some Python e-books that you can use to learn Python first, so you can understand the next steps better. In addition, an ebook is recommended to learn About OpenCV.

All right, don’t waste time. Let’s get started.

To setup opencv in python environment you will need these things ready ( match the versions to follow along with this Tutorial), first we need to prepare these environments (remember to prepare the version) :

  • Python 2.x
  • OpenCV 2.x
  • Numpy library (this can be downloaded later with PIP)

First of all, to download Python, you can go to the corresponding version on the official website, which may be in MSI format for Windows, PKG format for Mac, or source code for Linux.

After installing Python and Python, open the command line to use the PIP command to install Python packages, as in:

Since OpenCV uses the Numpy library, install the Numpy library by using the PIP install Numpy command. After installation, try importing, if no error is reported, ok:

Then enter the corresponding version on OpenCV’s official website and install it. Try importing:

Try face detection

Everything is ready except the east wind. Let’s write code to detect the face, let’s play Hello World of OpenCV.

Here we are going to use pre-trained XML files. These XML files are difficult to train, but we don’t need to worry about them, so OpenCV has provided many pre-trained classifiers for face detection.

Want to use it to write classifier, we need to haarcascade_frontalface_default classifier in the XML file. The XML from the opencv folder/sources under/data/haarcascades/copy to our project directory, That’s where we’re going to write the program. If there is no opencv folder/sources/data/haarcascades/this directory, you can try to find the opencv folder/share/opencv/haarcascades /. Just find the following files:

Then if we want to load the classifier, this is fine:

detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')Copy the code

And then let’s test the camera,

Cap = cv2.videocapture (0) ret, img = cap.read() cv2.imshow('windowname', img) cv2.waitKey(0)Copy the code

The code above calls your computer’s camera 0 and displays it. Of course, if you have more than one camera, you can try another ID by changing the VideoCapture parameter.

Cap.read (), which retrieves the image from the camera, returns two variables, the first a Boolean value indicating success and the second an image.

The program then displays the image via imshow(). The first parameter is the name of the window, and the second parameter is the image to display, which is our selfie.

The waitKey is used to stop on the image display screen so you can see it clearly, and the parameters can be 10, 100, 1000, etc., in milliseconds, so if you put a 0 here, you’re going to stop. Be warned, if you don’t stay long enough, you may not see the imshow photos.

After running this code, you will see a picture taken by the camera, which is generally yourself.

Next, we will first convert the image to grayscale image,

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)Copy the code

Then began the journey of face detection:

Faces = detector. DetectMultiScale (gray, 1.3, 5)Copy the code

This code waits for a list, each of which has x, y, height, and width. Where, list represents the detected faces, that is, the size of list is the number of faces, and the position of each face in the picture is (x, y, height, width).

In order to make us see the detection results more intuitively, we framed these faces:

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)Copy the code

further

Now we can detect faces through cameras, but what we really need is not a static image, we need a live, dynamic video stream that can be detected. So we add a loop, and then we keep checking, and finally it shows up in a new window.

detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while True: Ret, img = cap. The read () gray. = cv2 cvtColor (img, cv2. COLOR_BGR2GRAY) faces = detector. DetectMultiScale (gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.imshow('frame', img) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()Copy the code

Note that waitKey and ORD are used at the end of the loop to implement the q exit function, which checks every millisecond whether the keyboard has pressed Q. If it has pressed Q, the loop will exit. The next step is to release resources.

conclusion

In this article, we learned how to use OpenCV in Python, which is to write a program that detects faces in code. We reviewed or studied these points:

  • Classifiers using OpenCV
  • Read photos from the camera
  • Change the frame on the picture
  • Display images in a new window
  • Face detection in real time

P.S. did you find XML there is a file called cat face recognition!!

This first

In the original text, please point out if there is anything wrong, pay more attention to fried fish.