realtime face detect

This commit is contained in:
Alexander Popov 2023-09-03 13:09:03 +03:00
parent aafe177204
commit afe0de3109
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
2 changed files with 20 additions and 15 deletions

View File

@ -1,2 +1,2 @@
*.png
haarcascade_frontalface_default.xml
haarcascade_*.xml

View File

@ -2,28 +2,33 @@
import cv2
camera = cv2.VideoCapture(2)
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
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)
)
result, image = camera.read()
for x, y, w, h in faces:
cv2.rectangle(capture, (x, y), (x + w, y + h), (0, 255, 0), 2)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return faces
if __name__ == '__main__':
camera = cv2.VideoCapture(2)
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)
)
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
for x, y, w, h in face:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
result, image = camera.read()
cv2.imshow('Face Realtime Detector', image)
faces = face_detect(image)
cv2.imshow('Face Realtime Detector', image)
camera.release()
cv2.destroyAllWindows()
camera.release()
cv2.destroyAllWindows()