版权声明:本文为博主原创文章,未经博主允许不得转载。若有任何问题,请联系QQ:575925154(加好友时,请备注:CSDN) https://blog.csdn.net/Miracle0_0/article/details/82704594
Tip:加载OpenCV自带.xml文件。其实,这些的关键都是定位。注:微调参数。
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 11:18:47 2018
@author: Administrator
"""
import cv2
#尺寸
ds_factor = 1.25
#开启摄像头
cap = cv2.VideoCapture(0)
if cap is None:
raise IOError("Cannot open the webcam!")
#加载配置文件
nose_cascade = cv2.CascadeClassifier("./data/haarcascades/haarcascade_mcs_nose.xml")
if nose_cascade.empty():
raise IOError('Unable to load the nose cascade classifier xml file')
while True:
ret,frame = cap.read()
frame = cv2.resize(frame,None,fx = ds_factor,
fy = ds_factor,
interpolation = cv2.INTER_CUBIC)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#鼻子
nose_rects = nose_cascade.detectMultiScale(gray)
for (x,y,w,h) in nose_rects:
cv2.rectangle(frame,(x,y),(w+x,h+y),(0,0,255),3)
break
cv2.imshow('Nose',frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()