OpenCV实时人脸检测 |
您所在的位置:网站首页 › 人形检测和人脸检测 › OpenCV实时人脸检测 |
介绍一种实现人脸识别最简单的调包方法。 这里的人脸分类原理:使用级联分类器 这里我们只使用opencv封装好的api 。 一、首先在github官网上下载用于识别的分类器 地址:opencv/data at master · opencv/opencv · GitHub 这里的haar和lbp数据文件都是可以使用的,我先下载到本地。 这里的分类器都是可以使用的。我们就选择其中一个来使用。 二、代码如下 1.读取视频 def video_io_demo(): capture = cv.VideoCapture(0) height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) weight = capture.get(cv.CAP_PROP_FRAME_WIDTH) count = capture.get(cv.CAP_PROP_FRAME_COUNT) #帧数 fps = capture.get(cv.CAP_PROP_FPS) #频率:每秒播放几帧 print(height,weight,count,fps) type = 0 while(True): ret,frame = capture.read() if ret is True: cv.imshow("video_input",frame) result = process_frame(frame,type) cv.imshow("result",result) c = cv.waitKey(2) if c > 0: type = np.abs(c)%3 if c==27: break else: break2.加载分类器 face_detector = cv.CascadeClassifier("D:/opencv-data/opencv-master/data/haarcascades/haarcascade_frontalface_alt_tree.xml")这里也可以改变加载的分类器,可以多尝试几种包里的分类器 3.输出边框 def process_frame(frame,type): if type == 0: return detect_face(frame) else: return frame def detect_face(frame): gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY) cv.equalizeHist(gray,gray) faces = face_detector.detectMultiScale(gray,1.02,1,minSize=(200,200),maxSize=(400,400)) for x,y,w,h in faces: cv.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2,8,0) return frame需要注意的地方: faces = face_detector.detectMultiScale(gray,1.02,1,minSize=(100,100),maxSize=(300,300)) 里的参数需要重点关注,用于调整。(二值图, 每次图像尺寸减小的一个比例, 表示每一个目标至少要被检测到几次才会输出, 设置目标的最小检测尺寸, 设置目标的最大检测尺寸。) 调整好目标检测尺寸可以更有效更快的去检测和识别。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |