首先需要导入opencv库
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ opencv-python
上代码
#万码学堂Python 人脸识别演示 import cv2.cv2 as cv2 face_model=cv2.CascadeClassifier(r'D:\pythonworks\DAdemo\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml') # 打开摄像头 capcure = cv2.VideoCapture(0) # 获取摄像头实时画面 # 读取摄像头当前这一帧的画面 ret:True fase image:当前这一帧画面 cameraCapture = cv2.VideoCapture(0) ret, image = cameraCapture.read() cv2.namedWindow('WanmaitCamera') while ret: if cv2.waitKey(1) == 27:#esc退出 break cv2.imshow('WanmaitCamera', image) ret, image = cameraCapture.read() # 图片灰度处理 gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # 检查人脸 faces = face_model.detectMultiScale(gray, 1.1, 3, 0,(20,20)) #faces = face_model.detectMultiScale(gray,1.1,3,0,(2,2)) font = cv2.FONT_HERSHEY_SIMPLEX # 标记人脸 for (x, y, w, h) in faces: # 矩形标记 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) # 图像,文字内容, 坐标 ,字体,大小,颜色,字体厚度 cv2.putText(image,'wanmait',(int(x+w/2-60),y),font,1,(255,255,255),2) # 显示图片 cv2.imshow('WanmaitCamera', image) ret, image = cameraCapture.read() cameraCapture.release()
运行效果:
0条评论
点击登录参与评论