cv2. 01_face_dataset-阿里云开发者社区

开发者社区> 云计算> 正文

cv2. 01_face_dataset

简介:


import cv2
import os

cascPath = './data/'
face_detector = cv2.CascadeClassifier(cascPath + 'haarcascade_frontalface_alt.xml')

cam = cv2.VideoCapture(0)
cam.set(3,640)
cam.set(4,480)
# For each person, enter one numeric face id
face_id = input('\n enter user id end press <return> ==> ')
print('\n [INFO] Initializing face capture. Look the camera and wait ...')
#Initialize individual face count
count = 0

def creat_dir(path):

#判断路径是否存在,返回True,或者False
is_exists = os.path.exists(path)

if is_exists:
print('The path is exist')

else:
os.makedirs(path)
print('Create the dir successfully.')

savepath = './face_dataset/'
dirpath = savepath + '{}/'.format(face_id)
creat_dir(dirpath)

while True:
ret,img = cam.read()
# frame = cv2.flip(frame,-1)#旋转屏幕
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=5)

for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
count += 1
# Save the captured image into the datasets folder
cv2.imwrite(dirpath + str(face_id) +'.' + str(count) + '.jpg', gray[y:y+h, x:x+w])
cv2.imshow('img',img)


k = cv2.waitKey(100) & 0xff
if k == 27: #press 'ESC' to quit
break
elif count >= 30: # Take 30 face sample and stop video
break

# Do a bit of cleanup
print('\n [INFO] Exiting Program and cleanup stuff')
cam.release()
cv2.destroyAllWindows()

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章