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()
目录
相关文章
|
存储 弹性计算 运维
阿里云电脑多少钱一台(阿里云电脑收费标准)
阿里云电脑多少钱一台(阿里云电脑收费标准)
1503 0
|
9天前
|
云安全 监控 安全
|
14天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1473 8
|
7天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
489 12
|
19天前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
1178 88
大厂CIO独家分享:AI如何重塑开发者未来十年