Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Created January 9, 2023 03:20
Show Gist options
  • Save victormurcia/c5c5c1b5779a04e33938b04ca63b6154 to your computer and use it in GitHub Desktop.
Save victormurcia/c5c5c1b5779a04e33938b04ca63b6154 to your computer and use it in GitHub Desktop.
detect face using opencv with webcam
def objectTracker1(cascPath):
faceCascade = cv2.CascadeClassifier(cascPath)
cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)
while True: # try to get the first frame
rval, frame = vc.read()
# Capture frame-by-frame
ret, frame = vc.read()
#Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
objects = faceCascade.detectMultiScale(gray, scaleFactor=1.2,
minNeighbors=1, minSize=(40, 40),
flags=cv2.CASCADE_SCALE_IMAGE)
# Draw a rectangle around the faces
for (x, y, w, h) in objects:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("preview", frame)
rval, frame = vc.read()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
break
vc.release()
cv2.destroyWindow("preview")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment