Face Detection From Webcam in Python Using OpenCV¶
The objective of the program given is to detect object of interest(face) in real time and to keep tracking of the same object.This is a simple example of how to detect face in Python. You can try to use training samples of any other object of your choice to be detected by training the classifier on required objects.
In [2]:
import cv2
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
capture=cv2.VideoCapture(0)
while True:
ret,frame=capture.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5)
for x,y,w,h in faces:
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)
cv2.imshow('Color',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()