This is the 13th day of my participation in Gwen Challenge
OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.
This article shows how to implement picture smile detection using OpenCV in Python3:
Reference: Opencv4 official documentation: docs.opencv.org/4.2.0/
Preparations:
Download feature classification model: XML Model library: github.com/opencv/open…
1. Go to the haarcascade_smile.xml file and click inside. If you want to try to detect other feature regions, just download the corresponding XML file.
2. To findRaw
, right-click to link (target) save as.
The implementation process
Invoke the model library file
Import the model library file you just downloaded into the program
Code implementation:
smile_cascade = cv2.CascadeClassifier('E:\\demo\\haarcascade_smile.xml')')
Copy the code
To detect the target
The detecMultiScale() function is used to detect the target.
If a smile is detected in the image, we can return the result in the form of x, Y, W, H.
This is because the detection results require some parameters to be used to draw the detection region.
smiles = smile_cascade.detectMultiScale(image, scaleFactor = 1.2, minNeighbors = 53)
Copy the code
Draw target box
Once we have the position parameters for the object to be detected, we can draw the rectangle using the cv2.Rectangle () function.
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(image, (sx, sy), ((sx + sw), (sy + sh)), (0.255.0), 5)
Copy the code
The complete code
import cv2
image = cv2.imread("E:\\demo\\smile.jpg")
smile_cascade = cv2.CascadeClassifier('E:\\demo\\haarcascade_smile.xml')
smiles = smile_cascade.detectMultiScale(image, scaleFactor = 1.2, minNeighbors = 53)
for (sx, sy, sw, sh) in smiles:
cv2.rectangle(image, (sx, sy), ((sx + sw), (sy + sh)), (0.255.0), 5)
cv2.putText(image, 'Smile', (sx, sy - 7), 3.1.2, (0.0.255), 2, cv2.LINE_AA)
cv2.imshow("Smile Detected", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code
Results:
A series of articles will follow this month,
Wonderful article, please pay attention.