import cv2
import numpy as np
cv2.namedWindow("test") # Create a window
cap = cv2.VideoCapture(0) #Open camera one
success, frame = cap.read() #Read one frame
print("Camera open operation is: ", success);
color = (255,0,0) #Config the color
classfier = cv2.CascadeClassifier("D:\\project\\learn\\venv\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt.xml") #Make sure this xml file is in the same directory with py file
#Otherwise change it to absolute directory. This xml file can be found in D:\My Documents\Downloads\opencv\sources\data\haarcascades
while success:
success, frame = cap.read()
size = frame.shape[:2] #
image = np.zeros(size, dtype = np.float16) #
image = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #
cv2.equalizeHist(image, image) #
#Below three lines config the minimal image size
divisor = 8
h, w = size
minSize = ((int)(w/divisor), (int)(h/divisor))
faceRects = classfier.detectMultiScale(image, 1.2, 2, cv2.CASCADE_SCALE_IMAGE, minSize) #Face detect
if len(faceRects) > 0:#If face array length > 0
for faceRect in faceRects: #Draw a rectangle for every face
xf, yf, wf, hf = faceRect
x = int((float)(xf))
y = int((float)(yf))
w = int((float)(wf))
h = int((float)(hf))
cv2.rectangle(frame, (x, y), (x + w, y + h), color)
cv2.circle(frame, ((int)(x + 1.2 * w / 4), (int)(y + h / 3)), min((int)(w / 8), (int)(h / 8)), (255, 0, 0))
cv2.circle(frame, ((int)(x + 2.8 * w / 4), (int)(y + h / 3)), min((int)(w / 8), (int)(h / 8)), (255, 0, 0))
#cv2.rectangle(frame, ((int)(x + 3 * w / 8, (int)(y + 3 * h / 4))), ((int)(x + 5 * w / 8), (int)(y + 7 * h / 8)), (255, 0, 0))
cv2.imshow("test", frame) #Display image
key = cv2.waitKey(10)
c = chr(key & 255)
if c in ['q', 'Q', chr(27)]:
break
cv2.destroyWindow("test")
python+opencv人脸识别
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在上一篇文章中,我们学习了如何安装配置OpenCV和Python,然后写了些代码玩玩人脸检测。现在我们要进行下一步...
- 一、关于opencv 下面是来源于百度百科的关于Opencv的介绍 OpenCV是一个基于BSD许可(开源)发行的...
- Haar Cascade人脸识别如何训练Haar分类器原理分析 使用Haar Cascade分类器进行人脸识别 将...