用自己的笔记本摄像头获取实时图像-----进行人脸检测

import cv2 as cv

capture = cv.VideoCapture(0)
face_cascade = cv.CascadeClassifier(’…/data/haarcascade_frontalface_alt.xml’)

while True:

sucess, img = capture.read()
faces = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
    cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv.imshow("face", img)

key = cv.waitKey(1)

if key == 27:
    cv.destroyAllWindows()
    break
elif key == ord('s'):
    cv.imwrite("face.jpg",img)
    break

capture.release()
短短几行代码即可完成一个人脸识别案例,是不是很神奇

你可能感兴趣的:(OpenCV)