import cv2
import matplotlib.pyplot as plt
import numpy as np
def show(image):
plt.imshow(image)
plt.axis('off')
plt.show()
def imread(image):
image = cv2.imread(image)
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
return image
import os
os.chdir("E:/code/opencv/0.体验课资料/")
from imutils import *
image = imread('face.png')
show(image)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e687582f3e91ebb3e1faa8d243b2c08e.png)
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, minSize=(10, 10), flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in rects:
cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,255), 2)
show(image)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ad8152ad51295194af1c6c740c423ba1.png)
def facedetect(image):
image = imread(image)
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=2, minSize=(10, 10), flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in rects:
cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)
show(image)
facedetect('Solvay.jpg')
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1f32412ca3581366214662ddff424695.png)
facedetect("1.jpg")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c7f7eb16032cfad1389d638852bd2e7c.png)