import cv2 as cvimport numpyimport matplotlib.pyplot as plot# 创建摄像头对象 0 表示第一个摄像头 一般是笔记本的cap = cv.VideoCapture(0)while (1):ret, frame = cap.read()if ret == True:gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)# 3.实例化OpenCV人脸识别的分类器face_cas = cv.CascadeClassifier("haarcascade_frontalface_default.xml")face_cas.load('haarcascade_frontalface_default.xml')# 4.调用识别人脸faceRects = face_cas.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))for faceRect in faceRects:x, y, w, h = faceRect# 框出人脸cv.rectangle(frame, (x, y), (x + h, y + w), (0, 0, 255), 3)cv.imshow("frame", frame)if cv.waitKey(1) & 0xFF == ord('q'):break# 释放摄像头# 释放窗口cap.release()cv.destroyAllWindows()
900字范文,内容丰富有趣,生活中的好帮手!