snipplets.dev/snipplets/code/Python/opencv/face-detect-realtime.py

30 lines
660 B
Python
Raw Normal View History

2023-09-03 12:44:02 +03:00
#!/usr/bin/env python3
import cv2
camera = cv2.VideoCapture(2)
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
result, image = camera.read()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
face = face_classifier.detectMultiScale(
gray_image, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40)
)
for x, y, w, h in face:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Face Realtime Detector', image)
camera.release()
cv2.destroyAllWindows()