# 导入cv模块
import cv2 as cv
def face_detect_demo():
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
face_detect = cv.CascadeClassifier('D:/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
# face = face_detect.detectMultiScale(gray)
# 图像 缩放倍数 检测次数 默认值 限定范围【最小,最大】
face = face_detect.detectMultiScale(gray,1.01,5,0,(55,55),(60,60))
for x, y, w, h in face:
result=cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
cv.imshow('result', img)
# 保存结果图像
cv.imwrite('football_result.jpg', result)
# 读取图片
img = cv.imread('football.png')
face_detect_demo()
# 等待
cv.waitKey(0)
# 释放内存
cv.destroyAllWindows()
运行结果