snipplets.dev/code/Python/opencv/face-detect-realtime.py
2023-09-26 22:05:13 +03:00

35 lines
804 B
Python
Executable File

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