赞
踩
工具:Python 3.6
OpenCV 3.3.0
代码:
- import cv2
- cv2.namedWindow("Face")#打开一个窗口
- cap=cv2.VideoCapture(0)#打开一个默认摄像头
- success= cap.read()#读取图像
- color = (60,20,255)#颜色
- faceCas=cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
- count=0#计时器
- while success:
- success, frame = cap.read()
- faceRects = faceCas.detectMultiScale(frame, 1.2, 2)
- for faceRect in faceRects:
- x, y, w, h = faceRect
- print("位置:",x,y)
- print("宽高:",w,h)
- image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转换为灰度
- cv2.rectangle(frame, (x, y), (x + w, y + h), color)#画矩形
- f = cv2.resize(image[y:y + h, x:x + w], (200, 200))# 从灰度图中,扣出脸部,设置固定大小像素
- cv2.imwrite('E://Desktop//Face//'+str(count)+".jpg",f)#存储图片
- count+=1
- cv2.imshow("Face", frame)
- key = cv2.waitKey(1)
- c = chr(key & 0xff)
- if c in ['q', 'Q', chr(27)]:
- break
- cv2.destroyWindow("Face")

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。