In this video lesson we show a simple method to detect faces in a WEB cam frame using openCV and Haar Cascades. We use pretrained models to find, box and track faces in a frame. Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import cv2 print(cv2.__version__) width=640 height=360 cam=cv2.VideoCapture(0,cv2.CAP_DSHOW) cam.set(cv2.CAP_PROP_FRAME_WIDTH, width) cam.set(cv2.CAP_PROP_FRAME_HEIGHT,height) cam.set(cv2.CAP_PROP_FPS, 30) cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc(*'MJPG')) faceCascade=cv2.CascadeClassifier('C:\Users\Valued Customer\Documents\Python\haar\haarcascade_frontalface_default.xml') while True: ignore, frame = cam.read() frameGray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) faces=faceCascade.detectMultiScale(frameGray,1.3,5) for face in faces: x,y,w,h=face cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),3) cv2.imshow('my WEBcam', frame) cv2.moveWindow('my WEBcam',0,0) if cv2.waitKey(1) & 0xff ==ord('q'): break cam.release() |