Suck the cat with code! This article is participating in the cat essay Activity.
Eddie Peng is cool, he’s positive, he’s adorable, cats are clingy, they’re cute, they’re adorable, so if you have a cute picture of Eddie Peng, how do we tell the machine whether it’s a cat or a person? With the help of computer vision library, this paper identifies whether the picture describes a person or a cat.
One, foreword
To quickly identify the content of the image, we use two Python libraries, OpencV and Pillow.
1.1 OpenCV
OpenCV stands for Open Source Computer Vision Library, which is a cross-platform Computer Vision Library.
OpenCV can be used to solve problems in the following areas:
- Augmented reality
- Face recognition
- Gesture recognition
- The human-computer interaction
- Gesture recognition
- Motion tracking
- Object recognition
- Image segmentation
- The robot
OpenCV already includes a number of pre-trained classifiers for faces, eyes, smiles, and more. These XML files are stored in the GitHub repository.
Cat Face is one or the other
- haarcascade_frontalcatface_extended.xml
- haarcascade_frontalcatface.xml
human face
haarcascade_frontalface_default.xml
Download two files to the project directory and rename the catface file to catface_detector.xml; The human Face file was renamed to humanface_detector.xml.
1.2 Pillow
The Python Imaging Library is a free, open source add-on Library that can open, manipulate, and save many different image file formats. It was originally called PIL, but was discontinued in 2011. A project called Pillow continues to support image manipulation based on the PIL branch. Python 3 is supported.
1.3 Environment Installation
On a machine with a Python 3 environment installed, run the following command to install the corresponding library.
pip install opencv-python pillow
Copy the code
1.4 Preparing Pictures
To verify the program’s accuracy, let’s download two images, one of Eddie Peng and the cat. This photo of Eddie Peng is named test1.jpg.
This photo of the cat is called test2.jpg.
Second, code practice
2.1 Code Decomposition
We’ll start by creating a Python file called cv.py, which declares to import the two libraries we’ll use in python.
import cv2
from PIL import Image
Copy the code
Then load the project directory under the facial feature model file to the program.
catface_cascade = cv2.CascadeClassifier('catface_detector.xml')
humanface_cascade = cv2.CascadeClassifier('humanface_detector.xml')
Copy the code
Next, let’s first resize the images so that they are the same size. Next, we convert them to grayscale, which is faster for our model. Finally, let’s import the edited ready image into our program so that we can run cat and face detection models
newsize = (600.600)
imgr1 = Image.open("test1.jpg")
imgr1 = imgr1.resize(newsize)
imgr1.save("resized1.jpg")
imgr2 = Image.open("test2.jpg")
imgr2 = imgr2.resize(newsize)
imgr2.save("resized2.jpg")
imgr1 = imgr1.convert('L')
imgr1.save('ready1.jpg')
imgr2 = imgr2.convert('L')
imgr2.save("ready2.jpg")
# Read the input image
img1 = cv2.imread('ready1.jpg')
img2 = cv2.imread('ready2.jpg')
Copy the code
Now it’s time to detect faces. We’re going to run two lines of code. The first one detects the human face in the image. The second line is to detect the cat face in the image.
human_faces = humanface_cascade.detectMultiScale(img1, scaleFactor=1.3, minNeighbors=5, minSize=(75.75))
cat_faces = catface_cascade.detectMultiScale(img2, scaleFactor=1.3, minNeighbors=5, minSize=(75.75))
Copy the code
Next we will draw a rectangle on the detected surface. The rectangles can be different colors, and their thickness levels are adjustable.
for (i, (x, y, w, h)) in enumerate(human_faces):
cv2.rectangle(img1, (x, y), (x+w, y+h), (220.90.230), 3)
cv2.putText(img1, "Human Face - #{}".format(i + 1), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (220.90.230), 2)
for (i, (x, y, w, h)) in enumerate(cat_faces):
cv2.rectangle(img2, (x, y), (x+w, y+h), (0.255.0), 3)
cv2.putText(img2, "Cat Faces - #{}".format(i + 1), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0.0.255), 2)
Copy the code
Finally, we will use the detected face to save the image.
cv2.imwrite("faces_detected1.png", img1)
cv2.imwrite("faces_detected2.png", img2)
Copy the code
Overall our project directory structure is as follows.
2.2 Execution Results
After executing the program, there are many more files in the directory, as shown in the figure below.
Look at the end result.
According to the results, this group of experimental identification results are accurate.
Third, summary
This article is an entry level computer image recognition example, for the whole OpenCV, it is a small test! The process is not complicated, but if you are interested, you can try it out.
In the whole process of practice, OpenCV model file is very important for the identification process, some cats are not successful identification, such as Garfield, silver chiffon layer, you can go to find the appropriate model file.
In addition, due to the limitation of space, this paper did not mention the control experiment, such as exchanging pictures, using cat model files to identify people, using human model files to identify cats, readers to have a try, the process is not rare.
For more accurate identification, train the machine with data sets so that it can give you better predictions. I look forward to your comments.
Boy, haven’t you seen enough? Click on the stone’s home page and take a look at it casually. Maybe there will be a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!