import cv2
cap=cv2.VideoCapture(0)
classfier=cv2.CascadeClassifier('D:\\python+selenium\\anaconda3\\pkgs\\libopencv-3.4.2-h20b85fd_0\\Library\\etc\\haarcascades\\haarcascade_frontalface_alt.xml')
count=0
while cap.isOpened():
ret,frame=cap.read()
if not ret:
exit(-1)
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faceRects=classfier.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=3,minSize=(32,32))
if len(faceRects)>0:
for faceRect in faceRects:
x, y, w, h = faceRect
count+=1
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imwrite('data/'+str(count)+'.jpg',gray[y:y+h,x:x+w])
cv2.putText(frame,'123456',(10,10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2)
cv2.imshow('cam',frame)
key=cv2.waitKey(30)&0xff
if key==27:
exit(0)
print('error')
cap.release()
cv2.destroyWindow()