本次在视频识别的程度上增添了摄像头实时识别,
区别在于:
-
# v = cv2.VideoCapture('./dzd2.mp4')
-
v = cv2.VideoCapture(0)
import numpy as npimport cv2face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt2.xml')
# v = cv2.VideoCapture('./dzd2.mp4')
v = cv2.VideoCapture(0)
# win = cv2.namedWindow('dzd',cv2.WINDOW_NORMAL)#视频时由一张行图片组成,每一张图片,帧num = 1while True:flag,frame= v.read()if not flag:breakframe = cv2.resize(frame,(640,360))gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)face_zones = face_detector.detectMultiScale(gray,scaleFactor=1.1,
# minNeighbors=3,
# minSize =(60,60),
# maxSize =(110,110))
# print(face_zones)for x,y,w,h in face_zones[:1]:cv2.rectangle(frame,pt1=(x,y),pt2=(x+w,y+h),color= [0,0,255],thickness=2,)
# cv2.circle(frame,center=(x+w//2,y+h//2),radius=w//2,color= [0,0,255],thickness=2,)cv2.imshow('dzd',frame)
# if len(face_zones)>=1:
# f = input('是否保存这张图片?Y/N').lower()
# if f == 'y':
# face = frame[y:y+h,x:x+w]
# cv2.imwrite('./faces/liu/%d.jpg'%(num),face)
# num+=1key =cv2.waitKey(41)if key == ord('y'):face = frame[y+2:y+h-2,x+2:x+w-2]cv2.resize(face,(64,64))cv2.imwrite('./faces/liuwang/%d.jpg'%(num),face)num+=1elif key == ord('q'):breakv.release()#释放视频流
cv2.destroyAllWindows()