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()
目录
相关文章
|
6月前
|
存储 编译器 计算机视觉
cv::Mat
cv::Mat
48 3
|
2月前
|
机器学习/深度学习 计算机视觉 Python
cv2.dnn.
【9月更文挑战第13天】
50 12
|
5月前
|
机器学习/深度学习 算法 图形学
shape_predictor_68_face_landmarks
【6月更文挑战第22天】
219 7
|
5月前
get_frontal_face_detector
【6月更文挑战第20天】
95 5
|
6月前
|
计算机视觉
cv2.putText
cv2.putText
441 1
|
6月前
|
计算机视觉 Python
cv2.polylines
cv2.polylines
287 1
|
计算机视觉 Python
PIL.Image.open和cv2.imread的比较与相互转换
PIL.Image.open读入的是RGB顺序,而opencv中cv2.imread读入的是BGR通道顺序 。cv2.imread会显示图片更蓝一些。
461 0
PIL.Image.open和cv2.imread的比较与相互转换
|
计算机视觉
CV之Face Detection:Face Detection人脸检测原理及其常见分类技术
CV之Face Detection:Face Detection人脸检测原理及其常见分类技术
CV之Face Detection:Face Detection人脸检测原理及其常见分类技术
|
计算机视觉
Paper之CV:《One Millisecond Face Alignment with an Ensemble of Regression Trees》的翻译与解读
Paper之CV:《One Millisecond Face Alignment with an Ensemble of Regression Trees》的翻译与解读
Paper之CV:《One Millisecond Face Alignment with an Ensemble of Regression Trees》的翻译与解读