人脸识别python代码

import cv2

# 下载人脸识别模型文件
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# 读取图像
frame = cv2.imread('C:\\Users\\&\\Pictures\\hhh.jpg')

# 转换为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# 使用人脸识别模型检测人脸
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# 在图像中标记人脸
for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# 显示图像
cv2.imshow('Image', frame)
cv2.waitKey(0)
cv2.destroyAllWindows()

效果示意:

你可能感兴趣的:(opencv,计算机视觉,人工智能)